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


C# SyntaxToken.WithLeadingTrivia方法代码示例

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


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

示例1: FixOpenBraceNextToken

        /// <summary>
        /// Remove the extra new lines on the token which immediately follows an open brace.
        /// </summary>
        private static SyntaxToken FixOpenBraceNextToken(SyntaxToken token)
        {
            if (!token.HasLeadingTrivia)
            {
                return token;
            }

            if (token.Kind() == SyntaxKind.CloseBraceToken &&
                token.LeadingTrivia.All(x => x.IsKind(SyntaxKind.WhitespaceTrivia) || x.IsKind(SyntaxKind.EndOfLineTrivia)))
            {
                // This is an open / close brace combo with no content inbetween.  Just return the 
                // close brace and let the formatter handle the white space issues.  If there was a new line 
                // between the two it will be attached to the open brace and hence maintained. 
                return token.WithLeadingTrivia(SyntaxTriviaList.Empty);
            }

            // Remove all of the new lines at the top
            var triviaList = token.LeadingTrivia;
            var list = new List<SyntaxTrivia>(triviaList.Count);
            var index = MovePastSimpleNewLines(triviaList, 0);

            while (index < triviaList.Count)
            {
                list.Add(triviaList[index]);
                index++;
            }

            var newTriviaList = SyntaxFactory.TriviaList(list);
            return token.WithLeadingTrivia(newTriviaList);
        }
开发者ID:michaelcfanning,项目名称:codeformatter,代码行数:33,代码来源:BraceNewLineRule.cs

示例2: VisitToken

            public override SyntaxToken VisitToken(SyntaxToken token)
            {
                if (_lastTokenEndedInWhitespace)
                {
                    token = token.WithLeadingTrivia(Enumerable.Empty<SyntaxTrivia>());
                }
                else if (token.LeadingTrivia.Count > 0)
                {
                    if (_useElasticTrivia)
                    {
                        token = token.WithLeadingTrivia(SyntaxFactory.ElasticSpace);
                    }
                    else
                    {
                        token = token.WithLeadingTrivia(SyntaxFactory.Space);
                    }
                }

                if (token.TrailingTrivia.Count > 0)
                {
                    if (_useElasticTrivia)
                    {
                        token = token.WithTrailingTrivia(SyntaxFactory.ElasticSpace);
                    }
                    else
                    {
                        token = token.WithTrailingTrivia(SyntaxFactory.Space);
                    }

                    _lastTokenEndedInWhitespace = true;
                }
                else
                {
                    _lastTokenEndedInWhitespace = false;
                }

                if (token.Kind() == SyntaxKind.StringLiteralToken ||
                    token.Kind() == SyntaxKind.InterpolatedStringTextToken)
                {
                    if (s_newlinePattern.IsMatch(token.Text))
                    {
                        var newText = s_newlinePattern.Replace(token.Text, " ");
                        token = SyntaxFactory.Token(
                            token.LeadingTrivia,
                            token.Kind(),
                            newText, newText,
                            token.TrailingTrivia);
                    }
                }
 
                return token;
            }
开发者ID:XieShuquan,项目名称:roslyn,代码行数:52,代码来源:SyntaxNodeExtensions.SingleLineRewriter.cs

示例3: VisitToken

        public override SyntaxToken VisitToken(SyntaxToken token)
        {
            token = base.VisitToken(token);
            if (token.IsMissing)
            {
                return token;
            }

            bool changed;

            do
            {
                changed = false;
                if ((token.HasLeadingTrivia && token.LeadingTrivia.Count >= 2) ||
                    (token.HasTrailingTrivia && token.TrailingTrivia.Count >= 2))
                {
                    var newLeadingTrivia = RemoveBlankLines(token.LeadingTrivia, ref changed);
                    var newTrailingTrivia = RemoveBlankLines(token.TrailingTrivia, ref changed);

                    if (changed)
                    {
                        token = token.WithLeadingTrivia(Syntax.TriviaList(newLeadingTrivia));
                        token = token.WithTrailingTrivia(Syntax.TriviaList(newTrailingTrivia));
                    }
                }
            }
            while (changed);

            return token;
        }
开发者ID:mindraptor,项目名称:MetaProgramming,代码行数:30,代码来源:NewLineCleanup.cs

示例4: AddToken

        // Process a token. and add to the list of triva/tokens we're accumulating.
        public void AddToken(SyntaxToken token, bool isFirst, bool isLast)
        {
            bool isMissing = token.IsMissing;
            if (token.HasLeadingTrivia && (isFirst || isMissing || token.GetLeadingTrivia().TriviaListContainsStructuredTrivia()))
            {
                FinishInProgressTokens();
                AddTrivia(token.GetLeadingTrivia());
                token = ((SyntaxToken)token.WithLeadingTrivia(null));
            }

            ////if (!preserveExistingDiagnostics)
            ////{
            ////    token = token.WithoutDiagnostics();
            ////}

            SyntaxNode trailingTrivia = null;
            if (token.HasTrailingTrivia && (isLast || isMissing || token.GetTrailingTrivia().TriviaListContainsStructuredTrivia()))
            {
                trailingTrivia = token.GetTrailingTrivia();
                token = ((SyntaxToken)token.WithTrailingTrivia(null));
            }

            if (isMissing)
            {
                // Don't add missing tokens to skipped tokens, but preserve their diagnostics.
                ////if (token.ContainsDiagnostics())
                ////{
                ////    // Move diagnostics on missing token to next token.
                ////    if (diagnosticsToAdd != null)
                ////    {
                ////        diagnosticsToAdd = diagnosticsToAdd.Concat(token.GetDiagnostics());
                ////    }
                ////    else
                ////    {
                ////        diagnosticsToAdd = token.GetDiagnostics();
                ////    }

                ////    addDiagnosticsToFirstTokenOnly = true;
                ////}
            }
            else
            {
                skippedTokensBuilder.Add(token);
            }

            if (trailingTrivia != null)
            {
                FinishInProgressTokens();
                AddTrivia(trailingTrivia);
            }

            if (isFirst && addDiagnosticsToFirstTokenOnly)
            {
                FinishInProgressTokens(); // implicitly adds the diagnostics.
            }
        }
开发者ID:akrisiun,项目名称:Microsoft.Language.Xml,代码行数:57,代码来源:SkippedTriviaBuilder.cs

示例5: StripViolatingWhitespace

        private static SyntaxToken StripViolatingWhitespace(SyntaxToken token)
        {
            SyntaxToken result = token;

            var trailingWhitespaceIndex = TriviaHelper.IndexOfTrailingWhitespace(token.LeadingTrivia);
            if (trailingWhitespaceIndex != -1)
            {
                var newTriviaList = SyntaxFactory.TriviaList(token.LeadingTrivia.Take(trailingWhitespaceIndex));
                result = token.WithLeadingTrivia(newTriviaList);
            }

            return result;
        }
开发者ID:hexuefengx,项目名称:StyleCopAnalyzers,代码行数:13,代码来源:SA1518CodeFixProvider.cs

示例6: VisitToken

        public override SyntaxToken VisitToken(SyntaxToken token)
        {
            switch (token.CSharpKind())
            {
                case SyntaxKind.OpenBraceToken:
                    if (token.GetPreviousToken().CSharpKind() == SyntaxKind.CloseParenToken)
                    {
                        return token.WithLeadingTrivia(SyntaxFactory.ElasticLineFeed);
                    }
                    break;
            }

            return token;
        }
开发者ID:MartinJohns,项目名称:RoslynDemo,代码行数:14,代码来源:Program.cs

示例7: VisitToken

            public override SyntaxToken VisitToken(SyntaxToken token)
            {
                if (lastTokenEndedInWhitespace)
                {
                    token = token.WithLeadingTrivia(Enumerable.Empty<SyntaxTrivia>());
                }
                else if (token.LeadingTrivia.Count > 0)
                {
                    token = token.WithLeadingTrivia(SyntaxFactory.Space);
                }

                if (token.TrailingTrivia.Count > 0)
                {
                    token = token.WithTrailingTrivia(SyntaxFactory.Space);
                    lastTokenEndedInWhitespace = true;
                }
                else
                {
                    lastTokenEndedInWhitespace = false;
                }

                return token;
            }
开发者ID:modulexcite,项目名称:pattern-matching-csharp,代码行数:23,代码来源:SyntaxNodeExtensions.SingleLineRewriter.cs

示例8: AddModifiers

        private SyntaxNode AddModifiers(SyntaxNode node, SyntaxToken syntaxToken)
        {
            // Get the first child node.
            SyntaxNode firstNode = node.ChildNodes().FirstOrDefault();

            if (firstNode != null)
            {
                // The leading trivia will be the method comments, if any.
                SyntaxTriviaList leadingTrivia = firstNode.GetLeadingTrivia();

                // Replace it with one without leading trivia.
                node = node.ReplaceNodes(
                    new[] { firstNode },
                    (a, b) => firstNode.WithLeadingTrivia());

                // Add the leading trivia to the new modifier.
                syntaxToken = syntaxToken.WithLeadingTrivia(leadingTrivia);
            }

            return node.AddModifiers(syntaxToken);
        }
开发者ID:grokys,项目名称:StyleCopMagic,代码行数:21,代码来源:SA1400.cs

示例9: GetNewStartTokenWithAddedPragmaAsync

            internal static async Task<SyntaxToken> GetNewStartTokenWithAddedPragmaAsync(
                SyntaxToken startToken,
                TextSpan currentDiagnosticSpan,
                Diagnostic diagnostic,
                AbstractSuppressionCodeFixProvider fixer,
                Func<SyntaxNode, Task<SyntaxNode>> formatNode,
                bool isRemoveSuppression = false)
            {
                var trivia = startToken.LeadingTrivia.ToImmutableArray();
                SyntaxTrivia insertAfterTrivia;
                var index = GetPositionForPragmaInsertion(trivia, currentDiagnosticSpan, fixer, isStartToken: true, triviaAtIndex: out insertAfterTrivia);
                index++;

                bool needsLeadingEOL;
                if (index > 0)
                {
                    needsLeadingEOL = !IsEndOfLineOrHasTrailingEndOfLine(insertAfterTrivia, fixer);
                }
                else if (startToken.FullSpan.Start == 0)
                {
                    needsLeadingEOL = false;
                }
                else
                {
                    needsLeadingEOL = true;
                }

                var pragmaTrivia = !isRemoveSuppression
                    ? await fixer.CreatePragmaDisableDirectiveTriviaAsync(diagnostic, formatNode, needsLeadingEOL, needsTrailingEndOfLine: true).ConfigureAwait(false)
                    : await fixer.CreatePragmaRestoreDirectiveTriviaAsync(diagnostic, formatNode, needsLeadingEOL, needsTrailingEndOfLine: true).ConfigureAwait(false);

                return startToken.WithLeadingTrivia(trivia.InsertRange(index, pragmaTrivia));
            }
开发者ID:Rickinio,项目名称:roslyn,代码行数:33,代码来源:AbstractSuppressionCodeFixProvider.PragmaHelpers.cs

示例10: GetNewEndTokenWithAddedPragmaAsync

            internal static async Task<SyntaxToken> GetNewEndTokenWithAddedPragmaAsync(
                SyntaxToken endToken,
                TextSpan currentDiagnosticSpan,
                Diagnostic diagnostic,
                AbstractSuppressionCodeFixProvider fixer,
                Func<SyntaxNode, Task<SyntaxNode>> formatNode,
                bool isRemoveSuppression = false)
            {
                ImmutableArray<SyntaxTrivia> trivia;
                var isEOF = fixer.IsEndOfFileToken(endToken);
                if (isEOF)
                {
                    trivia = endToken.LeadingTrivia.ToImmutableArray();
                }
                else
                {
                    trivia = endToken.TrailingTrivia.ToImmutableArray();
                }

                SyntaxTrivia insertBeforeTrivia;
                var index = GetPositionForPragmaInsertion(trivia, currentDiagnosticSpan, fixer, isStartToken: false, triviaAtIndex: out insertBeforeTrivia);

                bool needsTrailingEOL;
                if (index < trivia.Length)
                {
                    needsTrailingEOL = !IsEndOfLineOrHasLeadingEndOfLine(insertBeforeTrivia, fixer);
                }
                else if (isEOF)
                {
                    needsTrailingEOL = false;
                }
                else
                {
                    needsTrailingEOL = true;
                }

                var pragmaTrivia = !isRemoveSuppression
                    ? await fixer.CreatePragmaRestoreDirectiveTriviaAsync(diagnostic, formatNode, needsLeadingEndOfLine: true, needsTrailingEndOfLine: needsTrailingEOL).ConfigureAwait(false)
                    : await fixer.CreatePragmaDisableDirectiveTriviaAsync(diagnostic, formatNode, needsLeadingEndOfLine: true, needsTrailingEndOfLine: needsTrailingEOL).ConfigureAwait(false);

                if (isEOF)
                {
                    return endToken.WithLeadingTrivia(trivia.InsertRange(index, pragmaTrivia));
                }
                else
                {
                    return endToken.WithTrailingTrivia(trivia.InsertRange(index, pragmaTrivia));
                };
            }
开发者ID:Rickinio,项目名称:roslyn,代码行数:49,代码来源:AbstractSuppressionCodeFixProvider.PragmaHelpers.cs

示例11: EnsureAndGetBraceTokens

        private static void EnsureAndGetBraceTokens(
            BaseTypeDeclarationSyntax typeDeclaration,
            bool hasMembers,
            out SyntaxToken openBrace,
            out SyntaxToken closeBrace)
        {
            openBrace = EnsureToken(typeDeclaration.OpenBraceToken);
            closeBrace = EnsureToken(typeDeclaration.CloseBraceToken, appendNewLineIfMissing: true);

            if (!hasMembers)
            {
                // Bug 539673: If there are no members, take any trivia that
                // belongs to the end brace and attach it to the opening brace.
                int index = -1;
                var leadingTrivia = closeBrace.LeadingTrivia;
                for (int i = leadingTrivia.Count - 1; i >= 0; i--)
                {
                    if (!leadingTrivia[i].IsWhitespaceOrEndOfLine())
                    {
                        index = i;
                        break;
                    }
                }

                if (index != -1)
                {
                    openBrace = openBrace.WithTrailingTrivia(
                        openBrace.TrailingTrivia.Concat(closeBrace.LeadingTrivia.Take(index + 1)));
                    closeBrace = closeBrace.WithLeadingTrivia(
                        closeBrace.LeadingTrivia.Skip(index + 1));
                }
            }
        }
开发者ID:EkardNT,项目名称:Roslyn,代码行数:33,代码来源:TypeDeclarationSyntaxExtensions.cs

示例12: UpdateTriviaList

 private static SyntaxToken UpdateTriviaList(SyntaxToken token, bool isStartToken, SyntaxTriviaList triviaList, AbstractSuppressionCodeFixProvider fixer)
 {
     return isStartToken || fixer.IsEndOfFileToken(token)
         ? token.WithLeadingTrivia(triviaList)
         : token.WithTrailingTrivia(triviaList);
 }
开发者ID:RoryVL,项目名称:roslyn,代码行数:6,代码来源:AbstractSuppressionCodeFixProvider.RemoveSuppressionCodeAction_Pragma.cs

示例13: VisitToken

            public override SyntaxToken VisitToken(SyntaxToken token)
            {
                SyntaxTriviaList oldTrivia = token.LeadingTrivia;
                SyntaxTriviaList newTrivia = Clean(oldTrivia);

                if (oldTrivia != newTrivia)
                {
                    token = token.WithLeadingTrivia(newTrivia);
                }

                return token;
            }
开发者ID:nicholjy,项目名称:stylize,代码行数:12,代码来源:DuplicateNewLineStyleRule.cs

示例14: VisitToken

            public override SyntaxToken VisitToken(SyntaxToken token)
            {
                if (this.tokensToStrip.Contains(token))
                {
                    this.tokensToStrip.Remove(token);

                    var index = TriviaHelper.IndexOfFirstNonBlankLineTrivia(token.LeadingTrivia);
                    var newLeadingTrivia = (index == -1) ? SyntaxFactory.TriviaList() : SyntaxFactory.TriviaList(token.LeadingTrivia.Skip(index));
                    return token.WithLeadingTrivia(newLeadingTrivia);
                }

                return base.VisitToken(token);
            }
开发者ID:Romanx,项目名称:StyleCopAnalyzers,代码行数:13,代码来源:UsingCodeFixProvider.cs

示例15: GetNewStartToken

            private static SyntaxToken GetNewStartToken(SyntaxToken startToken, Diagnostic diagnostic, AbstractSuppressionCodeFixProvider fixer)
            {
                var trivia = startToken.LeadingTrivia.ToImmutableArray();

                // Insert the #pragma disable directive after all leading new line trivia but before first trivia of any other kind.
                int index;
                SyntaxTrivia firstNonEOLTrivia = trivia.FirstOrDefault(t => !fixer.IsEndOfLine(t));
                if (firstNonEOLTrivia == default(SyntaxTrivia))
                {
                    index = trivia.Length;
                }
                else
                {
                    index = trivia.IndexOf(firstNonEOLTrivia);
                }

                bool needsLeadingEOL;
                if (index > 0)
                {
                    needsLeadingEOL = !fixer.IsEndOfLine(trivia[index - 1]);
                }
                else if (startToken.FullSpan.Start == 0)
                {
                    needsLeadingEOL = false;
                }
                else
                {
                    needsLeadingEOL = true;
                }

                var pragmaWarningTrivia = fixer.CreatePragmaDisableDirectiveTrivia(diagnostic, needsLeadingEOL);

                return startToken.WithLeadingTrivia(trivia.InsertRange(index, pragmaWarningTrivia));
            }
开发者ID:noahfalk,项目名称:roslyn,代码行数:34,代码来源:AbstractSuppressionCodeFixProvider.PragmaWarningCodeAction.cs


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