private bool MatchChain([NotNull] IInvocationExpression invocation)
        {
            myCallsCount = 0;

            while (true)
            {
                if (!MatchInvocation(invocation, checkValidity: true))
                {
                    break;
                }

                myCallsCount = myCallsCount + 1;

                var lambdaParameterName = ExtractLambdaParameterName(invocation);
                if (lambdaParameterName != null)
                {
                    myExistingLambdaParameterName = lambdaParameterName;
                }

                var innerInvocation = invocation.GetInnerInvocation();
                if (innerInvocation == null)
                {
                    break;
                }

                invocation = innerInvocation;
            }

            return(myCallsCount >= 2);
        }
    public sealed override bool IsAvailable(IUserDataHolder cache)
    {
      myOuterInvocation = null;
      myExistingLambdaParameterName = null;

      var topLevelNode = myProvider.GetTopLevelNode();
      if (topLevelNode == null) return false;

      myOuterInvocation = FindChain(topLevelNode);
      return myOuterInvocation != null;
    }
        private static bool IsReferencedExcept(
            [NotNull] string name, [NotNull] IStatement statement, [CanBeNull] ICSharpIdentifier exception)
        {
            foreach (var identifier in statement.Descendants <ICSharpIdentifier>())
            {
                if (identifier != exception && name == identifier.Name)
                {
                    return(true);
                }
            }

            return(false);
        }
        public sealed override bool IsAvailable(IUserDataHolder cache)
        {
            myOuterInvocation             = null;
            myExistingLambdaParameterName = null;

            var topLevelNode = myProvider.GetTopLevelNode();

            if (topLevelNode == null)
            {
                return(false);
            }

            myOuterInvocation = FindChain(topLevelNode);
            return(myOuterInvocation != null);
        }
        private static void RenameUsages(
            [NotNull] IDeclaredElement declaredElement, [NotNull] ITreeNode scope, [NotNull] ICSharpIdentifier name)
        {
            foreach (var referenceExpression in scope.Descendants <IReferenceExpression>())
            {
                var currentElement = referenceExpression.Reference.Resolve().DeclaredElement;
                if (currentElement == null)
                {
                    continue;
                }

                if (currentElement.Equals(declaredElement))
                {
                    referenceExpression.SetNameIdentifier(name);
                }
            }
        }
        private static ICSharpIdentifier FindLastInvocationUse([NotNull] IStatement statement, [NotNull] string name)
        {
            ICSharpIdentifier lastInvocationUse = null;

            foreach (var invocation in statement.Descendants <IInvocationExpression>())
            {
                var currentInvocationTarget = ExtractInvocationTargetName(invocation);

                if (name == currentInvocationTarget?.Name)
                {
                    lastInvocationUse = currentInvocationTarget;
                    break;
                }
            }

            return(lastInvocationUse);
        }
Example #7
0
        private static bool ShouldComplete(ITreeNode nodeInFile, ICSharpIdentifier identifier)
        {
            var methodDeclaration = identifier.GetContainingNode <IMethodDeclaration>();

            if (methodDeclaration != null)
            {
                // Don't complete in the parameter list
                if (nodeInFile.GetContainingNode <IFormalParameterList>() != null)
                {
                    return(false);
                }

                // Don't complete in the attribute list
                if (nodeInFile.GetContainingNode <IAttributeSectionList>() != null)
                {
                    return(false);
                }

                // Don't complete if there is a preceding [SerializeField] attribute
                if (HasSerializedFieldAttribute(methodDeclaration))
                {
                    return(false);
                }

                // Check the whole text of the declaration - if it ends (or even starts) with "__" (which is the
                // completion marker) then we have an incomplete method declaration and we're good to complete at this
                // position
                var declarationText = methodDeclaration.GetText();
                if (declarationText.StartsWith("__") || declarationText.EndsWith("__") ||
                    declarationText.Contains(SyntheticComments.CodeCompletionIdentifierToken))
                {
                    return(true);
                }

                if (identifier == methodDeclaration.NameIdentifier)
                {
                    return(true);
                }

                // E.g. `OnAni{caret} [SerializeField]` causes the parser to treat the next construct's attribute as an
                // array specifier to the type usage we're typing. (If there's already a type, then the parser thinks
                // it's a property). So, if we have an array rank, check to see if the token following the `[` is an
                // identifier. If so, it's likely it should be an attribute instead, so allow completion.
                var typeUsage = identifier.GetContainingNode <IArrayTypeUsage>();
                if (typeUsage != null)
                {
                    var arrayRanks = typeUsage.ArrayRanks;
                    if (arrayRanks.Count > 0)
                    {
                        var lbracket = arrayRanks[0].LBracket;
                        var next     = lbracket.GetNextMeaningfulSibling();
                        if (next == null)
                        {
                            next = lbracket.GetNextMeaningfulToken();
                            if (next != null && next.NodeType == CSharpTokenType.IDENTIFIER)
                            {
                                return(true);
                            }
                        }
                    }
                }

                return(false);
            }

            // E.g. `public void OnAni{caret} [SerializeField]` causes the parser to
            // treat this as a field declaration, using the brackets as an array specifier
            var fieldDeclaration = identifier.GetContainingNode <IFieldDeclaration>();

            if (fieldDeclaration?.LBracket != null)
            {
                if (fieldDeclaration.FixedBufferSizeExpression == null ||
                    !fieldDeclaration.FixedBufferSizeExpression.IsConstantValue())
                {
                    if (HasSerializedFieldAttribute(fieldDeclaration))
                    {
                        return(false);
                    }

                    return(identifier == fieldDeclaration.NameIdentifier);
                }
            }

            return(false);
        }
Example #8
0
 public ICSharpIdentifier SetNameIdentifier(ICSharpIdentifier param)
 {
     return(_classDeclaration.SetNameIdentifier(param));
 }
        private ILambdaExpression ExtractLambda([NotNull] IInvocationExpression invocation, ICSharpIdentifier lambdaParameterName)
        {
            var argument = invocation.Arguments[0];

            var lambda = argument.Value as ILambdaExpression;

            if (lambda != null)
            {
                var parameterName  = lambda.ParameterDeclarations[0].DeclaredName;
                var bodyExpression = lambda.BodyExpression;

                if (parameterName == lambdaParameterName.Name)
                {
                    return(lambda);
                }

                var declaredParameter = lambda.ParameterDeclarations[0];

                RenameUsages(declaredParameter.DeclaredElement, bodyExpression, lambdaParameterName);

                declaredParameter.SetNameIdentifier(lambdaParameterName);

                return(lambda);
            }

            var methodGroup = (IReferenceExpression)argument.Value;

            return((ILambdaExpression)Factory.CreateExpression("$0 => $1($0)", lambdaParameterName, methodGroup));
        }
    private bool MatchChain([NotNull] IInvocationExpression invocation)
    {
      myCallsCount = 0;

      while (true)
      {
        if (!MatchInvocation(invocation, checkValidity: true)) break;

        myCallsCount = myCallsCount + 1;

        var lambdaParameterName = ExtractLambdaParameterName(invocation);
        if (lambdaParameterName != null)
        {
          myExistingLambdaParameterName = lambdaParameterName;
        }

        var innerInvocation = invocation.GetInnerInvocation();
        if (innerInvocation == null) break;

        invocation = innerInvocation;
      }

      return myCallsCount >= 2;
    }
    private ILambdaExpression ExtractLambda([NotNull] IInvocationExpression invocation, ICSharpIdentifier lambdaParameterName)
    {
      var argument = invocation.Arguments[0];

      var lambda = argument.Value as ILambdaExpression;
      if (lambda != null)
      {
        var parameterName = lambda.ParameterDeclarations[0].DeclaredName;
        var bodyExpression = lambda.BodyExpression;

        if (parameterName == lambdaParameterName.Name)
        {
          return lambda;
        }

        var declaredParameter = lambda.ParameterDeclarations[0];

        RenameUsages(declaredParameter.DeclaredElement, bodyExpression, lambdaParameterName);

        declaredParameter.SetNameIdentifier(lambdaParameterName);

        return lambda;
      }

      var methodGroup = (IReferenceExpression)argument.Value;

      return (ILambdaExpression) Factory.CreateExpression("$0 => $1($0)", lambdaParameterName, methodGroup);
    }