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


C# SyntaxNode.GetLastToken方法代码示例

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


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

示例1: TreeData

        public TreeData(SyntaxNode root)
        {
            Contract.ThrowIfNull(root);
            _root = root;

            _firstToken = _root.GetFirstToken(includeZeroWidth: true);
            _lastToken = _root.GetLastToken(includeZeroWidth: true);
        }
开发者ID:Rickinio,项目名称:roslyn,代码行数:8,代码来源:TreeData.cs

示例2: AddTypeParameterConstraintClauseOperation

 private void AddTypeParameterConstraintClauseOperation(List<IndentBlockOperation> list, SyntaxNode node)
 {
     var typeParameterConstraintClause = node as TypeParameterConstraintClauseSyntax;
     if (typeParameterConstraintClause != null)
     {
         var declaringNode = typeParameterConstraintClause.Parent;
         var baseToken = declaringNode.GetFirstToken();
         AddIndentBlockOperation(list, baseToken, node.GetFirstToken(), node.GetLastToken());
     }
 }
开发者ID:jkotas,项目名称:roslyn,代码行数:10,代码来源:IndentBlockFormattingRule.cs

示例3: AddChangeSignatureIndentOperation

        private void AddChangeSignatureIndentOperation(List<IndentBlockOperation> list, SyntaxNode node)
        {
            if (node.Parent != null)
            {
                var baseToken = node.Parent.GetFirstToken();
                var startToken = node.GetFirstToken();
                var endToken = node.GetLastToken();
                var span = CommonFormattingHelpers.GetSpanIncludingTrailingAndLeadingTriviaOfAdjacentTokens(startToken, endToken);
                span = TextSpan.FromBounds(Math.Max(baseToken.Span.End, span.Start), span.End);

                list.Add(FormattingOperations.CreateRelativeIndentBlockOperation(baseToken, startToken, endToken, span, indentationDelta: 1, option: IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine));
            }
        }
开发者ID:Rickinio,项目名称:roslyn,代码行数:13,代码来源:ChangeSignatureFormattingRule.cs

示例4: GetLastInlineMethodBlockToken

        public static SyntaxToken GetLastInlineMethodBlockToken(SyntaxNode node)
        {
            var lastToken = node.GetLastToken(includeZeroWidth: true);
            if (lastToken.Kind() == SyntaxKind.None)
            {
                return default(SyntaxToken);
            }

            // If the next token is a semicolon, and we aren't in the initializer of a for-loop, use that token as the end.

            SyntaxToken nextToken = lastToken.GetNextToken(includeSkipped: true);
            if (nextToken.Kind() != SyntaxKind.None && nextToken.Kind() == SyntaxKind.SemicolonToken)
            {
                var forStatement = nextToken.GetAncestor<ForStatementSyntax>();
                if (forStatement != null && forStatement.FirstSemicolonToken == nextToken)
                {
                    return default(SyntaxToken);
                }

                lastToken = nextToken;
            }

            return lastToken;
        }
开发者ID:GloryChou,项目名称:roslyn,代码行数:24,代码来源:CSharpOutliningHelpers.cs

示例5: CreateRegion

 public static OutliningSpan CreateRegion(SyntaxNode node, SyntaxToken syntaxToken, string bannerText, bool autoCollapse)
 {
     return CreateRegion(
         node,
         syntaxToken,
         node.GetLastToken(),
         bannerText,
         autoCollapse);
 }
开发者ID:GloryChou,项目名称:roslyn,代码行数:9,代码来源:CSharpOutliningHelpers.cs

示例6: AddSpecificNodesSuppressOperations

        private void AddSpecificNodesSuppressOperations(List<SuppressOperation> list, SyntaxNode node)
        {
            var ifStatementNode = node as IfStatementSyntax;
            if (ifStatementNode != null)
            {
                AddSuppressWrappingIfOnSingleLineOperation(list, ifStatementNode.IfKeyword, ifStatementNode.Statement.GetLastToken(includeZeroWidth: true));

                if (ifStatementNode.Else != null)
                {
                    AddSuppressWrappingIfOnSingleLineOperation(list, ifStatementNode.Else.ElseKeyword, ifStatementNode.Else.Statement.GetLastToken(includeZeroWidth: true));
                }

                return;
            }

            var constructorInitializerNode = node as ConstructorInitializerSyntax;
            if (constructorInitializerNode != null)
            {
                var constructorDeclarationNode = constructorInitializerNode.Parent as ConstructorDeclarationSyntax;
                if (constructorDeclarationNode?.Body != null)
                {
                    AddSuppressWrappingIfOnSingleLineOperation(list, constructorInitializerNode.ColonToken, constructorDeclarationNode.Body.CloseBraceToken);
                }

                return;
            }

            var whileStatementNode = node as DoStatementSyntax;
            if (whileStatementNode != null)
            {
                AddSuppressWrappingIfOnSingleLineOperation(list, whileStatementNode.GetFirstToken(includeZeroWidth: true), whileStatementNode.Statement.GetLastToken(includeZeroWidth: true));
                return;
            }

            var memberDeclNode = node as MemberDeclarationSyntax;
            if (memberDeclNode != null)
            {
                // Attempt to keep the part of a member that follows hte attributes on a single
                // line if that's how it's currently written.
                var tokens = memberDeclNode.GetFirstAndLastMemberDeclarationTokensAfterAttributes();
                AddSuppressWrappingIfOnSingleLineOperation(list, tokens.Item1, tokens.Item2);
                
                var attributes = memberDeclNode.GetAttributes();
                if (attributes.Count > 0)
                {
                    // Also, If the member is on single line with its attributes on it, then keep 
                    // it on a single line.  This is for code like the following:
                    //
                    //      [Import] public int Field1;
                    //      [Import] public int Field2;
                    AddSuppressWrappingIfOnSingleLineOperation(list,
                        node.GetFirstToken(includeZeroWidth: true),
                        node.GetLastToken(includeZeroWidth: true));
                }

                var propertyDeclNode = node as PropertyDeclarationSyntax;
                if (propertyDeclNode?.Initializer != null && propertyDeclNode?.AccessorList != null)
                {
                    AddSuppressWrappingIfOnSingleLineOperation(list, tokens.Item1, propertyDeclNode.AccessorList.GetLastToken());
                }

                return;
            }

            var accessorDeclNode = node as AccessorDeclarationSyntax;
            if (accessorDeclNode != null)
            {
                AddSuppressWrappingIfOnSingleLineOperation(list, accessorDeclNode.Keyword, accessorDeclNode.GetLastToken(includeZeroWidth: true));
                return;
            }

            var switchSection = node as SwitchSectionSyntax;
            if (switchSection != null)
            {
                if (switchSection.Labels.Count < 2)
                {
                    AddSuppressWrappingIfOnSingleLineOperation(list, switchSection.GetFirstToken(includeZeroWidth: true), switchSection.GetLastToken(includeZeroWidth: true));
                    return;
                }
                else
                {
                    // Add Separate suppression for each Label and for the last label, add the <> 
                    for (int i = 0; i < switchSection.Labels.Count - 1; ++i)
                    {
                        if (switchSection.Labels[i] != null)
                        {
                            AddSuppressWrappingIfOnSingleLineOperation(list, switchSection.Labels[i].GetFirstToken(includeZeroWidth: true), switchSection.Labels[i].GetLastToken(includeZeroWidth: true));
                        }
                    }

                    // For the last label add the rest of the statements of the switch
                    if (switchSection.Labels[switchSection.Labels.Count - 1] != null)
                    {
                        AddSuppressWrappingIfOnSingleLineOperation(list, switchSection.Labels[switchSection.Labels.Count - 1].GetFirstToken(includeZeroWidth: true), switchSection.GetLastToken(includeZeroWidth: true));
                    }

                    return;
                }
            }

//.........这里部分代码省略.........
开发者ID:Rickinio,项目名称:roslyn,代码行数:101,代码来源:SuppressFormattingRule.cs

示例7: AddInitializerSuppressOperations

        private void AddInitializerSuppressOperations(List<SuppressOperation> list, SyntaxNode parent, IEnumerable<SyntaxNode> items)
        {
            // make creation node itself to not break into multiple line, if it is on same line
            AddSuppressWrappingIfOnSingleLineOperation(list, parent.GetFirstToken(includeZeroWidth: true), parent.GetLastToken(includeZeroWidth: true));

            // make each initializer expression to not break into multiple line if it is on same line
            foreach (var item in items)
            {
                var firstToken = item.GetFirstToken(includeZeroWidth: true);
                var lastToken = item.GetLastToken(includeZeroWidth: true);

                if (!firstToken.Equals(lastToken))
                {
                    AddSuppressWrappingIfOnSingleLineOperation(list, firstToken, lastToken);
                }
            }
        }
开发者ID:Rickinio,项目名称:roslyn,代码行数:17,代码来源:SuppressFormattingRule.cs

示例8: CreateBlockSpan

 public static BlockSpan? CreateBlockSpan(
     SyntaxNode node, SyntaxToken syntaxToken, 
     string bannerText, bool autoCollapse,
     string type, bool isCollapsible)
 {
     return CreateBlockSpan(
         node,
         syntaxToken,
         node.GetLastToken(),
         bannerText,
         autoCollapse,
         type,
         isCollapsible);
 }
开发者ID:jkotas,项目名称:roslyn,代码行数:14,代码来源:CSharpStructureHelpers.cs

示例9: TryGetLastToken

        /// <summary>
        /// get last token of the given using/field/statement/expression bodied member if one exists
        /// </summary>
        private static bool TryGetLastToken(SourceText text, int position, SyntaxNode owningNode, out SyntaxToken lastToken)
        {
            lastToken = owningNode.GetLastToken(includeZeroWidth: true);

            // last token must be on the same line as the caret
            var line = text.Lines.GetLineFromPosition(position);
            var locatedAtTheEndOfLine = LocatedAtTheEndOfLine(line, lastToken);
            if (!locatedAtTheEndOfLine && text.Lines.IndexOf(lastToken.Span.End) != line.LineNumber)
            {
                return false;
            }

            // if we already have last semicolon, we don't need to do anything
            if (!lastToken.IsMissing && lastToken.Kind() == SyntaxKind.SemicolonToken)
            {
                return false;
            }

            return true;
        }
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:23,代码来源:AutomaticLineEnderCommandHandler.cs

示例10: FindTokenOnRightOfPosition

        /// <summary>
        /// Find closest token (including one in structured trivia) right of given position
        /// </summary>
        private SyntaxToken FindTokenOnRightOfPosition(ISyntaxFactsService syntaxFactsService, SyntaxNode root, int position)
        {
            var token = syntaxFactsService.FindTokenOnRightOfPosition(root, position, includeSkipped: true, includeDirectives: true, includeDocumentationComments: true);
            if (token.RawKind == 0)
            {
                return root.GetLastToken(includeZeroWidth: true, includeSkipped: true, includeDirectives: true, includeDocumentationComments: true);
            }

            return token;
        }
开发者ID:riversky,项目名称:roslyn,代码行数:13,代码来源:AbstractCodeCleanerService.cs

示例11: GetNonOverlappingSpans

        /// <summary>
        /// make sure annotations are positioned outside of any spans. if not, merge two adjacent spans to one
        /// </summary>
        private IEnumerable<TextSpan> GetNonOverlappingSpans(ISyntaxFactsService syntaxFactsService, SyntaxNode root, IEnumerable<TextSpan> spans, CancellationToken cancellationToken)
        {
            // create interval tree for spans
            var intervalTree = SimpleIntervalTree.Create(TextSpanIntervalIntrospector.Instance, spans);

            // find tokens that are outside of spans
            var tokenSpans = new List<TextSpan>();
            foreach (var span in spans)
            {
                cancellationToken.ThrowIfCancellationRequested();

                SyntaxToken startToken;
                SyntaxToken endToken;
                TextSpan spanAlignedToTokens = GetSpanAlignedToTokens(syntaxFactsService, root, span, out startToken, out endToken);

                SyntaxToken previousToken = startToken.GetPreviousToken(includeZeroWidth: true, includeSkipped: true, includeDirectives: true, includeDocumentationComments: true);
                SyntaxToken nextToken = endToken.GetNextToken(includeZeroWidth: true, includeSkipped: true, includeDirectives: true, includeDocumentationComments: true);

                // make sure previous and next token we found are not overlap with any existing spans. if it does, merge two spans
                previousToken = (previousToken.RawKind == 0) ? root.GetFirstToken(includeZeroWidth: true) : previousToken;
                var start = intervalTree.GetOverlappingIntervals(previousToken.SpanStart, previousToken.Span.Length).Any() ?
                    previousToken.SpanStart : startToken.SpanStart;

                nextToken = (nextToken.RawKind == 0) ? root.GetLastToken(includeZeroWidth: true) : nextToken;
                var end = intervalTree.GetOverlappingIntervals(nextToken.SpanStart, nextToken.Span.Length).Any() ?
                    nextToken.Span.End : endToken.Span.End;

                tokenSpans.Add(TextSpan.FromBounds(start, end));
            }

            return tokenSpans.ToNormalizedSpans();
        }
开发者ID:riversky,项目名称:roslyn,代码行数:35,代码来源:AbstractCodeCleanerService.cs

示例12: AnnotateNodeForTextSpans

        /// <summary>
        /// inject annotations to the node so that it can re-calculate spans for each code cleaners after each tree transformation
        /// </summary>
        private ValueTuple<SyntaxNode, List<ValueTuple<SyntaxAnnotation, SyntaxAnnotation>>> AnnotateNodeForTextSpans(
            ISyntaxFactsService syntaxFactsService, SyntaxNode root, IEnumerable<TextSpan> spans, CancellationToken cancellationToken)
        {
            // get spans where tokens around the spans are not overlapping with spans
            var nonOverlappingSpans = GetNonOverlappingSpans(syntaxFactsService, root, spans, cancellationToken);

            // build token annotation map
            var tokenAnnotationMap = new Dictionary<SyntaxToken, List<SyntaxAnnotation>>();
            var annotations = new List<ValueTuple<SyntaxAnnotation, SyntaxAnnotation>>();
            foreach (var span in nonOverlappingSpans)
            {
                cancellationToken.ThrowIfCancellationRequested();

                SyntaxToken previousToken;
                SyntaxToken startToken;
                SyntaxToken endToken;
                SyntaxToken nextToken;
                GetTokensAroundSpan(root, span, out previousToken, out startToken, out endToken, out nextToken);

                // create marker to insert
                SpanMarker startMarker = new SpanMarker(type: (previousToken.RawKind == 0) ? SpanMarkerType.BeginningOfFile : SpanMarkerType.Normal,
                                                        oppositeMarkerType: (nextToken.RawKind == 0) ? SpanMarkerType.EndOfFile : SpanMarkerType.Normal);

                SpanMarker endMarker = new SpanMarker(type: (nextToken.RawKind == 0) ? SpanMarkerType.EndOfFile : SpanMarkerType.Normal,
                                                      oppositeMarkerType: (previousToken.RawKind == 0) ? SpanMarkerType.BeginningOfFile : SpanMarkerType.Normal);

                // set proper tokens
                previousToken = (previousToken.RawKind == 0) ? root.GetFirstToken(includeZeroWidth: true) : previousToken;
                nextToken = (nextToken.RawKind == 0) ? root.GetLastToken(includeZeroWidth: true) : nextToken;

                // build token to marker map
                tokenAnnotationMap.GetOrAdd(previousToken, _ => new List<SyntaxAnnotation>()).Add(startMarker.Annotation);
                tokenAnnotationMap.GetOrAdd(nextToken, _ => new List<SyntaxAnnotation>()).Add(endMarker.Annotation);

                // remember markers
                annotations.Add(new ValueTuple<SyntaxAnnotation, SyntaxAnnotation>(startMarker.Annotation, endMarker.Annotation));
            }

            // do a quick check
            // if, after all merges, spans are merged into one span that covers whole tree, return right away
            if (CleanupWholeNode(annotations))
            {
                // this will indicate that no annotation is needed
                return default(ValueTuple<SyntaxNode, List<ValueTuple<SyntaxAnnotation, SyntaxAnnotation>>>);
            }

            // inject annotations
            var newNode = InjectAnnotations(root, tokenAnnotationMap);
            return ValueTuple.Create(newNode, annotations);
        }
开发者ID:riversky,项目名称:roslyn,代码行数:53,代码来源:AbstractCodeCleanerService.cs

示例13: SetAlignmentBlockOperation

        private void SetAlignmentBlockOperation(List<IndentBlockOperation> list, SyntaxNode baseNode, SyntaxNode body)
        {
            var option = IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine;

            var baseToken = baseNode.GetFirstToken(includeZeroWidth: true);
            var firstToken = body.GetFirstToken(includeZeroWidth: true);
            var lastToken = body.GetLastToken(includeZeroWidth: true);

            SetAlignmentBlockOperation(list, baseToken, firstToken, lastToken, option);
        }
开发者ID:jkotas,项目名称:roslyn,代码行数:10,代码来源:IndentBlockFormattingRule.cs

示例14: AddIndentBlockOperations

            private static void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node)
            {
                // only add indent block operation if the base token is the first token on line
                var text = node.SyntaxTree.GetText();
                var baseToken = node.Parent.GetFirstToken(includeZeroWidth: true);

                list.Add(FormattingOperations.CreateRelativeIndentBlockOperation(
                    baseToken,
                    node.GetFirstToken(includeZeroWidth: true).GetNextToken(includeZeroWidth: true),
                    node.GetLastToken(includeZeroWidth: true).GetPreviousToken(includeZeroWidth: true),
                    indentationDelta: 1,
                    option: IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine));
            }
开发者ID:SoumikMukherjeeDOTNET,项目名称:roslyn,代码行数:13,代码来源:CSharpIndentationService.cs

示例15: IsInAnonymousFunctionOrQuery

        internal static bool IsInAnonymousFunctionOrQuery(int position, SyntaxNode lambdaExpressionOrQueryNode)
        {
            Debug.Assert(lambdaExpressionOrQueryNode.IsAnonymousFunction() || lambdaExpressionOrQueryNode.IsQuery());

            SyntaxToken firstIncluded;
            CSharpSyntaxNode body;

            switch (lambdaExpressionOrQueryNode.Kind())
            {
                case SyntaxKind.SimpleLambdaExpression:
                    SimpleLambdaExpressionSyntax simple = (SimpleLambdaExpressionSyntax)lambdaExpressionOrQueryNode;
                    firstIncluded = simple.Parameter.Identifier;
                    body = simple.Body;
                    break;

                case SyntaxKind.ParenthesizedLambdaExpression:
                    ParenthesizedLambdaExpressionSyntax parenthesized = (ParenthesizedLambdaExpressionSyntax)lambdaExpressionOrQueryNode;
                    firstIncluded = parenthesized.ParameterList.OpenParenToken;
                    body = parenthesized.Body;
                    break;

                case SyntaxKind.AnonymousMethodExpression:
                    AnonymousMethodExpressionSyntax anon = (AnonymousMethodExpressionSyntax)lambdaExpressionOrQueryNode;
                    firstIncluded = anon.DelegateKeyword;
                    body = anon.Block;
                    break;

                default:
                    // OK, so we have some kind of query clause.  They all start with a keyword token, so we'll skip that.
                    firstIncluded = lambdaExpressionOrQueryNode.GetFirstToken().GetNextToken();
                    return IsBetweenTokens(position, firstIncluded, lambdaExpressionOrQueryNode.GetLastToken().GetNextToken());
            }

            var bodyStatement = body as StatementSyntax;
            var firstExcluded = bodyStatement != null ?
                GetFirstExcludedToken(bodyStatement) :
                (SyntaxToken)SyntaxNavigator.Instance.GetNextToken(body, predicate: null, stepInto: null);

            return IsBetweenTokens(position, firstIncluded, firstExcluded);
        }
开发者ID:tvsonar,项目名称:roslyn,代码行数:40,代码来源:LookupPosition.cs


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