예제 #1
0
        private static bool IsFirstArgumentInMethod(ILiteralExpression literal)
        {
            var argument       = CSharpArgumentNavigator.GetByValue(literal as ICSharpExpression);
            var argumentsOwner = CSharpArgumentsOwnerNavigator.GetByArgument(argument);

            return(argumentsOwner != null && argumentsOwner.ArgumentsEnumerable.FirstOrDefault() == argument);
        }
        private static bool MattersToShowVar([NotNull] PrefixExpressionContext context)
        {
            if (context.CanBeStatement)
            {
                return(true);
            }

            var withReference = context.ExpressionWithReference;

            if (withReference != null)
            {
                // return SomeLong().var.Expression;
                var outerReference = ReferenceExpressionNavigator.GetByQualifierExpression(withReference);
                if (outerReference != null)
                {
                    return(true);
                }

                // SomeCall(withComplex.Arguments().var, 42);
                var argument = CSharpArgumentNavigator.GetByValue(withReference);
                if (argument != null)
                {
                    return(true);
                }
            }

            // note: what about F(arg.var)?

            return(false);
        }
예제 #3
0
        public static DeclaredElementInstance <IParameter> FindClosureParameter([NotNull] ICSharpExpression expression)
        {
            var containingExpression = expression.GetContainingParenthesizedExpressionStrict();
            var argument             = CSharpArgumentNavigator.GetByValue(containingExpression);

            var parameterInstance = argument?.MatchingParameter;

            if (parameterInstance == null)
            {
                return(null);
            }

            if (parameterInstance?.Expanded != ArgumentsUtil.ExpandedKind.None)
            {
                return(null);
            }

            var parameterDeclaredType = parameterInstance.Type as IDeclaredType;

            var delegateType = parameterDeclaredType?.GetTypeElement() as IDelegate;

            if (delegateType == null)
            {
                return(null);
            }

            return(parameterInstance);
        }
예제 #4
0
        public static IReference FindMethodInvocationByArgument([NotNull] ICSharpExpression expression)
        {
            var containingExpression = expression.GetContainingParenthesizedExpressionStrict();
            var argument             = CSharpArgumentNavigator.GetByValue(containingExpression);

            var invocationExpression = InvocationExpressionNavigator.GetByArgument(argument);

            var invokedReference = invocationExpression?.InvokedExpression.GetOperandThroughParenthesis() as IReferenceExpression;

            if (invokedReference == null)
            {
                return(null);
            }

            var invocationReference = invocationExpression.InvocationExpressionReference;

            var(declaredElement, _, resolveErrorType) = invocationReference.Resolve();
            if (resolveErrorType != ResolveErrorType.OK)
            {
                return(null);
            }
            if (!(declaredElement is IMethod))
            {
                return(null);
            }

            return(invokedReference.Reference);
        }
예제 #5
0
        protected override IExpression GetMvcLiteral(ITreeNode element, out ICSharpArgumentsOwner expression, out string anonymousPropertyName)
        {
            expression            = null;
            anonymousPropertyName = null;

            var csharpExpression = element as ICSharpExpression;

            if (csharpExpression == null)
            {
                return(null);
            }

            if (CSharpArgumentNavigator.GetByValue(csharpExpression) == null && CSharpArgumentNavigator.GetByValue(
                    AnonymousObjectCreationExpressionNavigator.GetByAnonymousInitializer(
                        AnonymousObjectInitializerNavigator.GetByMemberInitializer(
                            AnonymousMemberDeclarationNavigator.GetByExpression(csharpExpression)))) == null)
            {
                return(null);
            }

            if (!csharpExpression.ConstantValue.IsString())
            {
                return(null);
            }

            return(NancyUtil.GetMvcLiteral(csharpExpression, out expression, out anonymousPropertyName));
        }
예제 #6
0
        protected override bool IsAppropriateNode(ITreeNode element)
        {
            var csharpExpression = element as ICSharpExpression;

            if (csharpExpression == null)
            {
                return(false);
            }
            return(AssignmentExpressionNavigator.GetBySource(csharpExpression) != null ||
                   CSharpArgumentNavigator.GetByValue(csharpExpression) != null);
        }
예제 #7
0
        private bool IsSpecificArgumentInSpecificMethod(CSharpCodeCompletionContext context, out ICSharpLiteralExpression stringLiteral,
                                                        Func <IInvocationExpression, bool> methodChecker, Func <IArgumentList, ICSharpArgument, bool> argumentChecker)
        {
            stringLiteral = null;
            var nodeInFile = context.NodeInFile as ITokenNode;

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

            var possibleInvocationExpression = nodeInFile.Parent;

            if (possibleInvocationExpression is ICSharpLiteralExpression literalExpression)
            {
                if (!literalExpression.Literal.IsAnyStringLiteral())
                {
                    return(false);
                }

                var argument     = CSharpArgumentNavigator.GetByValue(literalExpression);
                var argumentList = ArgumentListNavigator.GetByArgument(argument);
                if (argument == null || argumentList == null)
                {
                    return(false);
                }

                if (argumentChecker(argumentList, argument))
                {
                    stringLiteral = literalExpression;
                    possibleInvocationExpression = InvocationExpressionNavigator.GetByArgument(argument);
                }
            }


            if (possibleInvocationExpression is IInvocationExpression invocationExpression)
            {
                if (methodChecker(invocationExpression))
                {
                    return(true);
                }
            }

            stringLiteral = null;
            return(false);
        }
예제 #8
0
        public IReference[] GetReferences(ITreeNode element, IReference[] oldReferences)
        {
            var literal = element as ILiteralExpression;

            if (literal != null && literal.ConstantValue.IsString())
            {
                var argument       = CSharpArgumentNavigator.GetByValue(literal as ICSharpExpression);
                var argumentsOwner = CSharpArgumentsOwnerNavigator.GetByArgument(argument);
                if (argumentsOwner != null && argumentsOwner.ArgumentsEnumerable.FirstOrDefault() != argument)
                {
                    return(EmptyArray <IReference> .Instance);
                }

                var invocationExpression = literal.GetContainingNode <IInvocationExpression>();
                var invocationReference  = invocationExpression?.Reference;
                var invokedMethod        = invocationReference?.Resolve().DeclaredElement as IMethod;
                if (invokedMethod != null &&
                    (invokedMethod.ShortName == "Invoke" ||
                     invokedMethod.ShortName == "InvokeRepeating" ||
                     invokedMethod.ShortName == "CancelInvoke"))
                {
                    var containingType = invokedMethod.GetContainingType();
                    if (containingType != null && Equals(containingType.GetClrName(), MonoBehaviourTypeName))
                    {
                        var inMethod    = literal.GetContainingNode <IMethodDeclaration>();
                        var currentType = inMethod?.DeclaredElement?.GetContainingType();

                        // TODO: Check if currentType is derived from MonoBehaviour?
                        if (currentType != null)
                        {
                            IReference reference = new MonoBehaviourInvokeReference(currentType, literal);

                            return(oldReferences != null && oldReferences.Length == 1 &&
                                   Equals(oldReferences[0], reference)
                                ? oldReferences
                                : new[] { reference });
                        }
                    }
                }
            }

            return(EmptyArray <IReference> .Instance);
        }
        private IDeclaredType[] GetParameterTypes()
        {
            var node         = Reference.GetTreeNode();
            var argument     = CSharpArgumentNavigator.GetByValue(node as ICSharpLiteralExpression);
            var argumentList = ArgumentListNavigator.GetByArgument(argument);
            var invocation   = InvocationExpressionNavigator.GetByArgumentList(argumentList);

            if (invocation?.Reference?.Resolve().DeclaredElement?.ShortName
                .Equals("StartCoroutine") == true && argumentList?.Arguments.Count > 1)
            {
                var secondArgument = argumentList.NotNull("argumentList != null").Arguments[1];
                if (secondArgument.Value?.Type() is IDeclaredType type)
                {
                    return(new[] { type });
                }
            }

            return(new IDeclaredType[] { });
        }
        public IReference[] GetReferences(ITreeNode element, IReference[] oldReferences)
        {
            var literal = element as ILiteralExpression;

            if (literal != null && literal.ConstantValue.IsString())
            {
                var argument       = CSharpArgumentNavigator.GetByValue(literal as ICSharpExpression);
                var argumentsOwner = CSharpArgumentsOwnerNavigator.GetByArgument(argument);
                if (argumentsOwner != null && argumentsOwner.ArgumentsEnumerable.FirstOrDefault() != argument)
                {
                    return(EmptyArray <IReference> .Instance);
                }

                var invocationExpression = literal.GetContainingNode <IInvocationExpression>();
                var invocationReference  = invocationExpression?.Reference;
                var invokedMethod        = invocationReference?.Resolve().DeclaredElement as IMethod;
                if (invokedMethod != null && DoesMethodReferenceFunction(invokedMethod))
                {
                    var containingType = invokedMethod.GetContainingType();
                    if (containingType != null && Equals(containingType.GetClrName(), KnownTypes.MonoBehaviour))
                    {
                        var targetType = invocationExpression.ExtensionQualifier?.GetExpressionType().ToIType()?.GetTypeElement()
                                         ?? literal.GetContainingNode <IMethodDeclaration>()?.DeclaredElement?.GetContainingType();

                        // TODO: Check if currentType is derived from MonoBehaviour?
                        if (targetType != null)
                        {
                            IReference reference = new UnityEventFunctionReference(targetType, literal);

                            return(oldReferences != null && oldReferences.Length == 1 &&
                                   Equals(oldReferences[0], reference)
                                ? oldReferences
                                : new[] { reference });
                        }
                    }
                }
            }

            return(EmptyArray <IReference> .Instance);
        }
예제 #11
0
        public override ReferenceCollection GetReferences(ITreeNode element, ReferenceCollection oldReferences)
        {
            var literal = GetValidStringLiteralExpression(element);

            if (literal == null)
            {
                return(ReferenceCollection.Empty);
            }

            var argument             = CSharpArgumentNavigator.GetByValue(literal);
            var invocationExpression = InvocationExpressionNavigator.GetByArgument(argument);

            if (invocationExpression == null)
            {
                return(ReferenceCollection.Empty);
            }

            // GameObject.AddComponent, GameObject.GetComponent and ScriptableObject.CreateInstance all have the string
            // literal as the first argument
            if (invocationExpression.ArgumentsEnumerable.FirstOrDefault() != argument)
            {
                return(ReferenceCollection.Empty);
            }

            var invocationReference = invocationExpression.Reference;

            if (!(invocationReference?.Resolve().DeclaredElement is IMethod invokedMethod))
            {
                return(ReferenceCollection.Empty);
            }

            var kind          = GetExpectedReferenceKind(invokedMethod);
            var newReferences = kind == ExpectedObjectTypeReferenceKind.None
                ? ReferenceCollection.Empty
                : CreateTypeNameReferences(literal, kind);

            return(ResolveUtil.ReferenceSetsAreEqual(newReferences, oldReferences) ? oldReferences : newReferences);
        }
예제 #12
0
        public static ICSharpStatement CanBeStatement([NotNull] ICSharpExpression expression)
        {
            var argument = CSharpArgumentNavigator.GetByValue(expression);

            if (argument == null || argument.Kind != ParameterKind.VALUE)
            {
                return(null);
            }

            var invocation = InvocationExpressionNavigator.GetByArgument(argument);

            if (invocation == null)
            {
                return(null);
            }

            var referenceExpression = invocation.InvokedExpression as IReferenceExpression;

            if (referenceExpression != null && referenceExpression.QualifierExpression == null)
            {
                var services   = argument.GetSolution().GetComponent <IProjectFileTypeServices>();
                var sourceFile = argument.GetSourceFile();
                if (sourceFile == null)
                {
                    return(null);
                }

                var service = services.TryGetService <IRazorPsiServices>(sourceFile.LanguageType);
                if (service != null && service.IsSpecialMethodInvocation(invocation, RazorMethodType.Write))
                {
                    return(ExpressionStatementNavigator.GetByExpression(invocation));
                }
            }

            return(null);
        }
 private static IArgument GetArgument(ITreeNode treeNode)
 {
     return(CSharpArgumentNavigator.GetByValue(treeNode as ICSharpExpression ?? treeNode.Parent as ICSharpExpression));
 }
 protected override ICSharpArgument Navigate(ICSharpExpression expression)
 {
     return(CSharpArgumentNavigator.GetByValue(expression));
 }