Exemplo n.º 1
0
        public ISymbol GetDeclaredSymbol(SemanticModel semanticModel, SyntaxToken token, CancellationToken cancellationToken)
        {
            var location = token.GetLocation();

            foreach (var ancestor in token.GetAncestors <SyntaxNode>())
            {
                var symbol = semanticModel.GetDeclaredSymbol(ancestor, cancellationToken);

                if (symbol != null)
                {
                    if (symbol.Locations.Contains(location))
                    {
                        return(symbol);
                    }

                    // We found some symbol, but it defined something else. We're not going to have a higher node defining _another_ symbol with this token, so we can stop now.
                    return(null);
                }

                // If we hit an executable statement syntax and didn't find anything yet, we can just stop now -- anything higher would be a member declaration which won't be defined by something inside a statement.
                if (SyntaxFacts.IsExecutableStatement(ancestor))
                {
                    return(null);
                }
            }

            return(null);
        }
        public ISymbol GetDeclaredSymbol(SemanticModel semanticModel, SyntaxToken token, CancellationToken cancellationToken)
        {
            var location = token.GetLocation();
            var q        = from node in token.GetAncestors <SyntaxNode>()
                           let symbol = semanticModel.GetDeclaredSymbol(node, cancellationToken)
                                        where symbol != null && symbol.Locations.Contains(location)
                                        select symbol;

            return(q.FirstOrDefault());
        }