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


C# SyntaxToken.WithoutTrailingWhitespace方法代码示例

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


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

示例1: GetTransformedDocumentAsync

        private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxToken token)
        {
            Dictionary<SyntaxToken, SyntaxToken> replacements = new Dictionary<SyntaxToken, SyntaxToken>();

            // check for a following space
            bool missingFollowingSpace = true;
            if (token.HasTrailingTrivia)
            {
                if (token.TrailingTrivia.First().IsKind(SyntaxKind.WhitespaceTrivia))
                {
                    missingFollowingSpace = false;
                }
                else if (token.TrailingTrivia.First().IsKind(SyntaxKind.EndOfLineTrivia))
                {
                    missingFollowingSpace = false;
                }
            }
            else
            {
                SyntaxToken nextToken = token.GetNextToken();
                if (nextToken.IsKind(SyntaxKind.CommaToken) || nextToken.IsKind(SyntaxKind.GreaterThanToken) || nextToken.IsKind(SyntaxKind.CloseBracketToken))
                {
                    // make an exception for things like typeof(Func<,>), typeof(Func<,,>), and int[,]
                    missingFollowingSpace = false;
                }
            }

            if (!token.IsFirstInLine())
            {
                SyntaxToken precedingToken = token.GetPreviousToken();
                if (precedingToken.TrailingTrivia.Any(SyntaxKind.WhitespaceTrivia))
                {
                    SyntaxToken corrected = precedingToken.WithoutTrailingWhitespace().WithoutFormatting();
                    replacements[precedingToken] = corrected;
                }
            }

            if (missingFollowingSpace)
            {
                SyntaxToken intermediate = token.WithoutTrailingWhitespace();
                SyntaxToken corrected =
                    intermediate
                    .WithTrailingTrivia(intermediate.TrailingTrivia.Insert(0, SyntaxFactory.Space));
                replacements[token] = corrected;
            }

            var transformed = root.ReplaceTokens(replacements.Keys, (original, maybeRewritten) => replacements[original]);
            return Task.FromResult(document.WithSyntaxRoot(transformed));
        }
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:49,代码来源:SA1001CodeFixProvider.cs

示例2: GetTransformedDocumentAsync

        private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxToken token)
        {
            bool isAddingSpace = true;
            switch (token.Kind())
            {
            case SyntaxKind.NewKeyword:
                {
                    SyntaxToken nextToken = token.GetNextToken();
                    if (nextToken.IsKind(SyntaxKind.OpenBracketToken) || nextToken.IsKind(SyntaxKind.OpenParenToken))
                    {
                        isAddingSpace = false;
                    }
                }

                break;

            case SyntaxKind.ReturnKeyword:
            case SyntaxKind.ThrowKeyword:
                {
                    SyntaxToken nextToken = token.GetNextToken();
                    if (nextToken.IsKind(SyntaxKind.SemicolonToken))
                    {
                        isAddingSpace = false;
                    }
                }

                break;

            case SyntaxKind.CheckedKeyword:
            case SyntaxKind.UncheckedKeyword:
                isAddingSpace = token.GetNextToken().IsKind(SyntaxKind.OpenBraceToken);
                break;

            case SyntaxKind.DefaultKeyword:
            case SyntaxKind.NameOfKeyword:
            case SyntaxKind.SizeOfKeyword:
            case SyntaxKind.TypeOfKeyword:
                isAddingSpace = false;
                break;

            case SyntaxKind.IdentifierToken:
                if (token.Text == "nameof")
                {
                    // SA1000 would only have been reported for a nameof expression. No need to verify.
                    goto case SyntaxKind.NameOfKeyword;
                }

                return Task.FromResult(document);

            default:
                break;
            }

            if (isAddingSpace == token.HasTrailingTrivia)
            {
                return Task.FromResult(document);
            }

            if (isAddingSpace)
            {
                SyntaxTrivia whitespace = SyntaxFactory.Space;
                SyntaxToken corrected = token.WithTrailingTrivia(token.TrailingTrivia.Insert(0, whitespace));
                Document updatedDocument = document.WithSyntaxRoot(root.ReplaceToken(token, corrected));
                return Task.FromResult(updatedDocument);
            }
            else
            {
                SyntaxToken corrected = token.WithoutTrailingWhitespace().WithoutFormatting();
                Document updatedDocument = document.WithSyntaxRoot(root.ReplaceToken(token, corrected));
                return Task.FromResult(updatedDocument);
            }
        }
开发者ID:nukefusion,项目名称:StyleCopAnalyzers,代码行数:72,代码来源:SA1000CodeFixProvider.cs

示例3: GetTransformedDocumentAsync

        private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxToken token)
        {
            bool precededBySpace;
            bool followsSpecialCharacter;

            Dictionary<SyntaxToken, SyntaxToken> replacements = new Dictionary<SyntaxToken, SyntaxToken>();

            if (!token.IsFirstInLine())
            {
                SyntaxToken precedingToken = token.GetPreviousToken();
                precededBySpace = precedingToken.TrailingTrivia.Any(SyntaxKind.WhitespaceTrivia);

                followsSpecialCharacter =
                    precedingToken.IsKind(SyntaxKind.OpenBracketToken)
                    || precedingToken.IsKind(SyntaxKind.OpenParenToken)
                    || precedingToken.IsKind(SyntaxKind.CloseParenToken);

                if (followsSpecialCharacter && precededBySpace)
                {
                    SyntaxToken correctedPreceding = precedingToken.WithoutTrailingWhitespace().WithoutFormatting();
                    replacements.Add(precedingToken, correctedPreceding);
                }
                else if (!followsSpecialCharacter && !precededBySpace)
                {
                    SyntaxToken correctedPreceding = precedingToken.WithoutTrailingWhitespace();
                    SyntaxTrivia whitespace = SyntaxFactory.ElasticSpace;
                    correctedPreceding =
                        correctedPreceding
                        .WithTrailingTrivia(correctedPreceding.TrailingTrivia.Add(whitespace))
                        .WithoutFormatting();
                    replacements.Add(precedingToken, correctedPreceding);
                }
            }

            if (token.TrailingTrivia.Any(SyntaxKind.WhitespaceTrivia) || token.TrailingTrivia.Any(SyntaxKind.EndOfLineTrivia))
            {
                SyntaxToken corrected = token.WithoutTrailingWhitespace(removeEndOfLineTrivia: true).WithoutFormatting();
                replacements.Add(token, corrected);
            }

            var transformed = root.ReplaceTokens(replacements.Keys, (original, maybeRewritten) => replacements[original]);
            Document updatedDocument = document.WithSyntaxRoot(transformed);

            return Task.FromResult(updatedDocument);
        }
开发者ID:nukefusion,项目名称:StyleCopAnalyzers,代码行数:45,代码来源:SA1022CodeFixProvider.cs

示例4: GetTransformedDocumentAsync

        private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxToken token)
        {
            Dictionary<SyntaxToken, SyntaxToken> replacements = new Dictionary<SyntaxToken, SyntaxToken>();

            if (!token.IsFirstInLine())
            {
                SyntaxToken precedingToken = token.GetPreviousToken();
                if (precedingToken.TrailingTrivia.Any(SyntaxKind.WhitespaceTrivia))
                {
                    SyntaxToken corrected = precedingToken.WithoutTrailingWhitespace().WithoutFormatting();
                    replacements[precedingToken] = corrected;
                }
            }

            if (!token.TrailingTrivia.Any(SyntaxKind.EndOfLineTrivia) && token.TrailingTrivia.Any(SyntaxKind.WhitespaceTrivia))
            {
                bool ignoreTrailingWhitespace;
                SyntaxToken nextToken = token.GetNextToken();
                switch (nextToken.Kind())
                {
                case SyntaxKind.CloseBracketToken:
                case SyntaxKind.OpenParenToken:
                case SyntaxKind.CommaToken:
                case SyntaxKind.SemicolonToken:
                // TODO: "certain types of operator symbols"
                case SyntaxKind.DotToken:
                case SyntaxKind.OpenBracketToken:
                case SyntaxKind.CloseParenToken:
                    ignoreTrailingWhitespace = true;
                    break;

                case SyntaxKind.GreaterThanToken:
                    ignoreTrailingWhitespace = nextToken.Parent.IsKind(SyntaxKind.TypeArgumentList);
                    break;

                case SyntaxKind.QuestionToken:
                    ignoreTrailingWhitespace = nextToken.Parent.IsKind(SyntaxKind.ConditionalAccessExpression);
                    break;

                case SyntaxKind.IdentifierToken:
                    ignoreTrailingWhitespace = token.Parent.IsKind(SyntaxKind.ArrayRankSpecifier);
                    break;

                default:
                    ignoreTrailingWhitespace = false;
                    break;
                }

                if (!ignoreTrailingWhitespace)
                {
                    SyntaxToken corrected = token.WithoutTrailingWhitespace().WithoutFormatting();
                    replacements[token] = corrected;
                }
            }

            if (replacements.Count == 0)
            {
                return Task.FromResult(document);
            }

            var transformed = root.ReplaceTokens(replacements.Keys, (original, maybeRewritten) => replacements[original]);
            Document updatedDocument = document.WithSyntaxRoot(transformed);

            return Task.FromResult(updatedDocument);
        }
开发者ID:nukefusion,项目名称:StyleCopAnalyzers,代码行数:65,代码来源:SA1011CodeFixProvider.cs


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