当前位置: 首页>>代码示例>>C#>>正文


C# SyntaxToken.GetAncestors方法代码示例

本文整理汇总了C#中SyntaxToken.GetAncestors方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxToken.GetAncestors方法的具体用法?C# SyntaxToken.GetAncestors怎么用?C# SyntaxToken.GetAncestors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SyntaxToken的用法示例。


在下文中一共展示了SyntaxToken.GetAncestors方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetDeclaredSymbol

        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();
        }
开发者ID:RoryVL,项目名称:roslyn,代码行数:10,代码来源:CSharpSemanticFactsService.cs

示例2: VisitToken

            public override SyntaxToken VisitToken(SyntaxToken token)
            {
                var shouldCheckTrivia = _stringAndCommentTextSpans.Contains(token.Span);
                _isProcessingTrivia += shouldCheckTrivia ? 1 : 0;
                var newToken = base.VisitToken(token);
                _isProcessingTrivia -= shouldCheckTrivia ? 1 : 0;

                // Handle Alias annotations
                newToken = UpdateAliasAnnotation(newToken);

                // Rename matches in strings and comments
                newToken = RenameWithinToken(token, newToken);

                // We don't want to annotate XmlName with RenameActionAnnotation
                if (newToken.Parent.IsKind(SyntaxKind.XmlName))
                {
                    return newToken;
                }

                bool isRenameLocation = IsRenameLocation(token);

                // if this is a reference location, or the identifier token's name could possibly
                // be a conflict, we need to process this token
                var isOldText = token.ValueText == _originalText;
                var tokenNeedsConflictCheck =
                    isRenameLocation ||
                    token.ValueText == _replacementText ||
                    isOldText ||
                    _possibleNameConflicts.Contains(token.ValueText);

                if (tokenNeedsConflictCheck)
                {
                    newToken = RenameAndAnnotateAsync(token, newToken, isRenameLocation, isOldText).WaitAndGetResult(_cancellationToken);

                    if (!_isProcessingComplexifiedSpans)
                    {
                        _invocationExpressionsNeedingConflictChecks.AddRange(token.GetAncestors<InvocationExpressionSyntax>());
                    }
                }

                return newToken;
            }
开发者ID:nemec,项目名称:roslyn,代码行数:42,代码来源:CSharpRenameRewriterLanguageService.cs

示例3: SimplifyIdentifierToken

        private SyntaxToken SimplifyIdentifierToken(
            SyntaxToken token,
            SemanticModel semanticModel,
            OptionSet optionSet,
            CancellationToken cancellationToken)
        {
            var unescapedIdentifier = token.ValueText;

            var enclosingXmlNameAttr = token.GetAncestors(n => n is XmlNameAttributeSyntax).FirstOrDefault();

            // always escape keywords
            if (SyntaxFacts.GetKeywordKind(unescapedIdentifier) != SyntaxKind.None && enclosingXmlNameAttr == null)
            {
                return CreateNewIdentifierTokenFromToken(token, escape: true);
            }

            // Escape the Await Identifier if within the Single Line Lambda & Multi Line Context
            // and async method

            var parent = token.Parent;

            if (SyntaxFacts.GetContextualKeywordKind(unescapedIdentifier) == SyntaxKind.AwaitKeyword)
            {
                var enclosingLambdaExpression = parent.GetAncestorsOrThis(n => (n is SimpleLambdaExpressionSyntax || n is ParenthesizedLambdaExpressionSyntax)).FirstOrDefault();
                if (enclosingLambdaExpression != null)
                {
                    if (enclosingLambdaExpression is SimpleLambdaExpressionSyntax)
                    {
                        if (((SimpleLambdaExpressionSyntax)enclosingLambdaExpression).AsyncKeyword.Kind() == SyntaxKind.AsyncKeyword)
                        {
                            return token;
                        }
                    }

                    if (enclosingLambdaExpression is ParenthesizedLambdaExpressionSyntax)
                    {
                        if (((ParenthesizedLambdaExpressionSyntax)enclosingLambdaExpression).AsyncKeyword.Kind() == SyntaxKind.AsyncKeyword)
                        {
                            return token;
                        }
                    }
                }

                var enclosingMethodBlock = parent.GetAncestorsOrThis(n => n is MethodDeclarationSyntax).FirstOrDefault();

                if (enclosingMethodBlock != null && ((MethodDeclarationSyntax)enclosingMethodBlock).Modifiers.Any(n => n.Kind() == SyntaxKind.AsyncKeyword))
                {
                    return token;
                }
            }

            // within a query all contextual query keywords need to be escaped, even if they appear in a non query context.
            if (token.GetAncestors(n => n is QueryExpressionSyntax).Any())
            {
                switch (SyntaxFacts.GetContextualKeywordKind(unescapedIdentifier))
                {
                    case SyntaxKind.FromKeyword:
                    case SyntaxKind.WhereKeyword:
                    case SyntaxKind.SelectKeyword:
                    case SyntaxKind.GroupKeyword:
                    case SyntaxKind.IntoKeyword:
                    case SyntaxKind.OrderByKeyword:
                    case SyntaxKind.JoinKeyword:
                    case SyntaxKind.LetKeyword:
                    case SyntaxKind.InKeyword:
                    case SyntaxKind.OnKeyword:
                    case SyntaxKind.EqualsKeyword:
                    case SyntaxKind.ByKeyword:
                    case SyntaxKind.AscendingKeyword:
                    case SyntaxKind.DescendingKeyword:
                        return CreateNewIdentifierTokenFromToken(token, escape: true);
                }
            }

            var result = token.Kind() == SyntaxKind.IdentifierToken ? CreateNewIdentifierTokenFromToken(token, escape: false) : token;

            // we can't remove the escaping if this would change the semantic. This can happen in cases
            // where there are two attribute declarations one with and and one without the attribute
            // suffix.
            if (SyntaxFacts.IsAttributeName(parent))
            {
                var expression = (SimpleNameSyntax)parent;
                var newExpression = expression.WithIdentifier(result);
                var speculationAnalyzer = new SpeculationAnalyzer(expression, newExpression, semanticModel, cancellationToken);
                if (speculationAnalyzer.ReplacementChangesSemantics())
                {
                    return CreateNewIdentifierTokenFromToken(token, escape: true);
                }
            }

            // TODO: handle crefs and param names of xml doc comments.
            // crefs have the same escaping rules than csharp, param names do not allow escaping in Dev11, but 
            // we may want to change that for Roslyn (Bug 17984, " Could treat '@' specially in <param>, <typeparam>, etc")

            return result;
        }
开发者ID:GloryChou,项目名称:roslyn,代码行数:96,代码来源:CSharpEscapingReducer.cs

示例4: GetExpansionTarget

        private static SyntaxNode GetExpansionTarget(SyntaxToken token)
        {
            // get the directly enclosing statement
            var enclosingStatement = token.GetAncestors(n => n is StatementSyntax).FirstOrDefault();

            // System.Func<int, int> myFunc = arg => X;
            SyntaxNode possibleLambdaExpression = enclosingStatement == null
                ? token.GetAncestors(n => n is SimpleLambdaExpressionSyntax || n is ParenthesizedLambdaExpressionSyntax).FirstOrDefault()
                : null;
            if (possibleLambdaExpression != null)
            {
                var lambdaExpression = ((LambdaExpressionSyntax)possibleLambdaExpression);
                if (lambdaExpression.Body is ExpressionSyntax)
                {
                    return lambdaExpression.Body;
                }
            }

            // int M() => X;
            var possibleArrowExpressionClause = enclosingStatement == null
                ? token.GetAncestors<ArrowExpressionClauseSyntax>().FirstOrDefault()
                : null;
            if (possibleArrowExpressionClause != null)
            {
                return possibleArrowExpressionClause.Expression;
            }

            var enclosingNameMemberCrefOrnull = token.GetAncestors(n => n is NameMemberCrefSyntax).LastOrDefault();
            if (enclosingNameMemberCrefOrnull != null)
            {
                if (token.Parent is TypeSyntax && token.Parent.Parent is TypeSyntax)
                {
                    enclosingNameMemberCrefOrnull = null;
                }
            }

            var enclosingXmlNameAttr = token.GetAncestors(n => n is XmlNameAttributeSyntax).FirstOrDefault();
            if (enclosingXmlNameAttr != null)
            {
                return null;
            }

            var enclosingInitializer = token.GetAncestors<EqualsValueClauseSyntax>().FirstOrDefault();
            if (enclosingStatement == null && enclosingInitializer != null && enclosingInitializer.Parent is VariableDeclaratorSyntax)
            {
                return enclosingInitializer.Value;
            }

            var attributeSyntax = token.GetAncestor<AttributeSyntax>();
            if (attributeSyntax != null)
            {
                return attributeSyntax;
            }

            // there seems to be no statement above this one. Let's see if we can at least get an SimpleNameSyntax
            return enclosingStatement ?? enclosingNameMemberCrefOrnull ?? token.GetAncestors(n => n is SimpleNameSyntax).FirstOrDefault();
        }
开发者ID:nemec,项目名称:roslyn,代码行数:57,代码来源:CSharpRenameRewriterLanguageService.cs

示例5: GetExpansionTarget

        private static SyntaxNode GetExpansionTarget(SyntaxToken token)
        {
            // get the directly enclosing statement
            var enclosingStatement = token.GetAncestors(n => n is StatementSyntax).FirstOrDefault();

            // see if there's an enclosing lambda expression
            SyntaxNode possibleLambdaExpression = enclosingStatement == null
                ? token.GetAncestors(n => n is SimpleLambdaExpressionSyntax || n is ParenthesizedLambdaExpressionSyntax).FirstOrDefault()
                : null;

            var enclosingNameMemberCrefOrnull = token.GetAncestors(n => n is NameMemberCrefSyntax).LastOrDefault();
            if (enclosingNameMemberCrefOrnull != null)
            {
                if (token.Parent is TypeSyntax && token.Parent.Parent is TypeSyntax)
                {
                    enclosingNameMemberCrefOrnull = null;
                }
            }

            var enclosingXmlNameAttr = token.GetAncestors(n => n is XmlNameAttributeSyntax).FirstOrDefault();
            if (enclosingXmlNameAttr != null)
            {
                return null;
            }

            var enclosingInitializer = token.GetAncestors<EqualsValueClauseSyntax>().FirstOrDefault();
            if (enclosingStatement == null && enclosingInitializer != null && enclosingInitializer.Parent is VariableDeclaratorSyntax)
            {
                return enclosingInitializer.Value;
            }

            var attributeSyntax = token.GetAncestor<AttributeSyntax>();
            if (attributeSyntax != null)
            {
                return attributeSyntax;
            }

            // there seems to be no statement above this one. Let's see if we can at least get an SimpleNameSyntax
            return enclosingStatement ?? enclosingNameMemberCrefOrnull ?? token.GetAncestors(n => n is SimpleNameSyntax).FirstOrDefault();
        }
开发者ID:ehsansajjad465,项目名称:roslyn,代码行数:40,代码来源:CSharpRenameRewriterLanguageService.cs


注:本文中的SyntaxToken.GetAncestors方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。