Пример #1
1
        private IMethodSymbol GetMethodDefinitionSymbol(SemanticModel semanticModel, SyntaxToken token)
        {
            var argument = token.AncestorAndSelf<ArgumentSyntax>();
            if (argument != null)
            {
                var invocation = argument.AncestorAndSelf<InvocationExpressionSyntax>();
                if (invocation == null)
                {
                    return null;
                }

                return semanticModel.GetSymbolInfo(invocation, this.cancellationToken).Symbol as IMethodSymbol;
            }

            var parameter = token.AncestorAndSelf<ParameterSyntax>();
            if (parameter != null)
            {
                var parameterList = parameter.AncestorAndSelf<ParameterListSyntax>();
                if (parameterList == null)
                {
                    // doesn't support lambda
                    return null;
                }

                var definitionNode = parameterList.Parent;
                return semanticModel.GetDeclaredSymbol(definitionNode, this.cancellationToken) as IMethodSymbol;
            }

            return null;
        }
        public static bool OnArgumentOrParameterWithoutRefOut(this SyntaxTree tree, int position)
        {
            SyntaxToken token = tree.GetRoot().FindToken(position);

            if (token.Kind() == SyntaxKind.None)
            {
                return(false);
            }

            ArgumentSyntax argument = token.AncestorAndSelf <ArgumentSyntax>();

            if (argument != null && argument.Span.IntersectsWith(position))
            {
                if (argument.RefOrOutKeyword.Kind() != SyntaxKind.None)
                {
                    return(false);
                }

                return(true);
            }

            ParameterSyntax parameter = token.AncestorAndSelf <ParameterSyntax>();

            if (parameter != null && parameter.Span.IntersectsWith(position))
            {
                if (parameter.Modifiers.Any(m => m.Kind() == SyntaxKind.OutKeyword || m.Kind() == SyntaxKind.RefKeyword))
                {
                    return(false);
                }

                return(true);
            }

            return(false);
        }
        public static bool OnArgumentOrParameter(this SyntaxTree tree, int position)
        {
            SyntaxToken token = tree.GetRoot().FindToken(position);

            if (token.Kind() == SyntaxKind.None)
            {
                return(false);
            }

            ArgumentSyntax argument = token.AncestorAndSelf <ArgumentSyntax>();

            if (argument != null && argument.Span.IntersectsWith(position))
            {
                return(true);
            }

            ParameterSyntax parameter = token.AncestorAndSelf <ParameterSyntax>();

            if (parameter != null && parameter.Span.IntersectsWith(position))
            {
                return(true);
            }

            return(false);
        }
Пример #4
0
        private IMethodSymbol GetMethodDefinitionSymbol(SemanticModel semanticModel, SyntaxToken token)
        {
            var argument = token.AncestorAndSelf <ArgumentSyntax>();

            if (argument != null)
            {
                var invocation = argument.AncestorAndSelf <InvocationExpressionSyntax>();
                if (invocation == null)
                {
                    return(null);
                }

                return(semanticModel.GetSymbolInfo(invocation, this.cancellationToken).Symbol as IMethodSymbol);
            }

            var parameter = token.AncestorAndSelf <ParameterSyntax>();

            if (parameter != null)
            {
                var parameterList = parameter.AncestorAndSelf <ParameterListSyntax>();
                if (parameterList == null)
                {
                    // doesn't support lambda
                    return(null);
                }

                var definitionNode = parameterList.Parent;
                return(semanticModel.GetDeclaredSymbol(definitionNode, this.cancellationToken) as IMethodSymbol);
            }

            return(null);
        }
Пример #5
0
        private int GetParameterIndex(IMethodSymbol methodSymbol, SyntaxToken token)
        {
            var argument = token.AncestorAndSelf <ArgumentSyntax>();

            if (argument != null)
            {
                // name parameter?
                if (argument.NameColon != null)
                {
                    var symbol = methodSymbol.Parameters.FirstOrDefault(p => p.Name == argument.NameColon.Name.Identifier.ValueText);
                    if (symbol == null)
                    {
                        // named parameter is used but can't find one?
                        return(-1);
                    }

                    return(symbol.Ordinal);
                }

                // positional argument
                var list = argument.Parent as ArgumentListSyntax;
                for (int i = 0; i < list.Arguments.Count; i++)
                {
                    var arg = list.Arguments[i];

                    // malformed call
                    if (arg.NameColon != null)
                    {
                        return(-1);
                    }

                    if (arg == argument)
                    {
                        return(i);
                    }
                }

                return(-1);
            }

            var parameter = token.AncestorAndSelf <ParameterSyntax>();

            if (parameter != null)
            {
                var parameterList = parameter.AncestorAndSelf <ParameterListSyntax>();
                return(parameterList.Parameters.IndexOf(parameter));
            }

            return(-1);
        }
Пример #6
0
        private int GetParameterIndex(IMethodSymbol methodSymbol, SyntaxToken token)
        {
            var argument = token.AncestorAndSelf<ArgumentSyntax>();
            if (argument != null)
            {
                // name parameter?
                if (argument.NameColon != null)
                {
                    var symbol = methodSymbol.Parameters.FirstOrDefault(p => p.Name == argument.NameColon.Name.Identifier.ValueText);
                    if (symbol == null)
                    {
                        // named parameter is used but can't find one?
                        return -1;
                    }

                    return symbol.Ordinal;
                }

                // positional argument
                var list = argument.Parent as ArgumentListSyntax;
                for (int i = 0; i < list.Arguments.Count; i++)
                {
                    var arg = list.Arguments[i];

                    // malformed call
                    if (arg.NameColon != null)
                    {
                        return -1;
                    }

                    if (arg == argument)
                    {
                        return i;
                    }
                }

                return -1;
            }

            var parameter = token.AncestorAndSelf<ParameterSyntax>();
            if (parameter != null)
            {
                var parameterList = parameter.AncestorAndSelf<ParameterListSyntax>();
                return parameterList.Parameters.IndexOf(parameter);
            }

            return -1;
        }