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


C# SyntaxNode.ReplaceTokens方法代码示例

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


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

示例1: ReplaceTokens

 private SyntaxNode ReplaceTokens(
     SyntaxNode root,
     IEnumerable<SyntaxToken> oldTokens,
     Func<SyntaxToken, SyntaxToken, SyntaxToken> computeReplacementToken)
 {
     return root.ReplaceTokens(oldTokens, (o, n) => computeReplacementToken(o, n));
 }
开发者ID:FreeBSD-DotNet,项目名称:monodevelop,代码行数:7,代码来源:AbstractSyntaxTriviaService.cs

示例2: RestoreTrivia

            public SyntaxNode RestoreTrivia(
                SyntaxNode root,
                AnnotationResolver annotationResolver = null,
                TriviaResolver triviaResolver = null)
            {
                var tokens = RecoverTokensAtEdges(root, annotationResolver);
                var map = CreateOldToNewTokensMap(tokens, triviaResolver);

                return root.ReplaceTokens(map.Keys, (o, n) => map[o]);
            }
开发者ID:GloryChou,项目名称:roslyn,代码行数:10,代码来源:AbstractSyntaxTriviaService.Result.cs

示例3: ReplaceTokens

        private SyntaxNode ReplaceTokens(
            SyntaxNode root,
            IEnumerable<SyntaxToken> oldTokens,
            Func<SyntaxToken, SyntaxToken, SyntaxToken> computeReplacementToken)
        {
            Contract.ThrowIfNull(root);
            Contract.ThrowIfNull(oldTokens);
            Contract.ThrowIfNull(computeReplacementToken);

            return root.ReplaceTokens(oldTokens, (o, n) => computeReplacementToken(o, n));
        }
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:11,代码来源:AbstractSyntaxTriviaService.cs

示例4: FixOpenBraces

        /// <summary>
        /// Fix the new lines around open brace tokens.  An open brace should be followed by content, not a blank
        /// line.  Remove those here.  
        /// </summary>
        private static SyntaxNode FixOpenBraces(SyntaxNode syntaxNode)
        {
            // Look for the open brace tokens that are followed by empty blank lines.  The new lines will
            // be attached to the next nodes, not the open brace token.
            var tokensToReplace = syntaxNode.DescendantTokens().Where((token) =>
                {
                    var nextToken = token.GetNextToken();
                    if (token.Kind() != SyntaxKind.OpenBraceToken || !nextToken.HasLeadingTrivia)
                    {
                        return false;
                    }

                    return IsSimpleNewLine(nextToken.LeadingTrivia, 0);
                }).Select(x => x.GetNextToken());

            return syntaxNode.ReplaceTokens(tokensToReplace, (_, y) => FixOpenBraceNextToken(y));
        }
开发者ID:chuck-mitchell,项目名称:codeformatter,代码行数:21,代码来源:BraceNewLineRule.cs

示例5: ProcessTokenAfterOpenBrace

        SyntaxNode ISyntaxRule.Process(SyntaxNode root)
        {
            var braceTokenNeighbors = root.DescendantTokens()
                .Where(t =>
                    t.IsKind(SyntaxKind.OpenBraceToken)
                    || (t.IsKind(SyntaxKind.CloseBraceToken) && t.GetPreviousToken().IsKind(SyntaxKind.OpenBraceToken))
                )
                .ToDictionary(
                    t => t.IsKind(SyntaxKind.OpenBraceToken) ? t.GetNextToken() : t.GetPreviousToken(),
                    t => t.Kind()
                );

            var processed = root.ReplaceTokens(
                braceTokenNeighbors.Keys,
                (orig, t) => braceTokenNeighbors[orig] == SyntaxKind.OpenBraceToken
                    ? ProcessTokenAfterOpenBrace(t)
                    : ProcessTokenBeforeCloseBrace(t)
            );
            return processed;
        }
开发者ID:madelson,项目名称:MedallionCodeFormatter,代码行数:20,代码来源:BraceNewlineRule.cs

示例6: 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

示例7: StripMultipleBlankLines

        private static SyntaxNode StripMultipleBlankLines(SyntaxNode syntaxRoot)
        {
            var replaceMap = new Dictionary<SyntaxToken, SyntaxToken>();

            var usingDirectives = syntaxRoot.GetAnnotatedNodes(UsingCodeFixAnnotation).Cast<UsingDirectiveSyntax>();

            foreach (var usingDirective in usingDirectives)
            {
                var nextToken = usingDirective.SemicolonToken.GetNextToken(true);

                // start at -1 to compensate for the always present end-of-line.
                var count = -1;

                // count the blanks lines at the end of the using statement.
                foreach (var trivia in usingDirective.SemicolonToken.TrailingTrivia.Reverse())
                {
                    if (!trivia.IsKind(SyntaxKind.EndOfLineTrivia))
                    {
                        break;
                    }

                    count++;
                }

                // count the blank lines at the start of the next token
                foreach (var trivia in nextToken.LeadingTrivia)
                {
                    if (!trivia.IsKind(SyntaxKind.EndOfLineTrivia))
                    {
                        break;
                    }

                    count++;
                }

                if (count > 1)
                {
                    replaceMap[nextToken] = nextToken.WithLeadingTrivia(nextToken.LeadingTrivia.Skip(count - 1));
                }
            }

            var newSyntaxRoot = syntaxRoot.ReplaceTokens(replaceMap.Keys, (original, rewritten) => replaceMap[original]);
            return newSyntaxRoot;
        }
开发者ID:Romanx,项目名称:StyleCopAnalyzers,代码行数:44,代码来源:UsingCodeFixProvider.cs

示例8: 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

示例9: FixCloseBraces

        /// <summary>
        /// Close braces should never have a newline between the last line of content and the 
        /// closing brace.  Also want to remove consecutive new lines before any comments or
        /// #pragma that preceeds the close brace.
        /// </summary>
        private static SyntaxNode FixCloseBraces(SyntaxNode syntaxNode)
        {
            var tokensToReplace = syntaxNode.DescendantTokens().Where((token) =>
                {
                    return
                        token.Kind() == SyntaxKind.CloseBraceToken &&
                        token.HasLeadingTrivia &&
                        (IsSimpleNewLine(token.LeadingTrivia, 0) || IsSimpleNewLine(token.LeadingTrivia, token.LeadingTrivia.Count - 1));
                });

            return syntaxNode.ReplaceTokens(tokensToReplace, (_, y) => FixCloseBraceLeadingTrivia(y));
        }
开发者ID:chuck-mitchell,项目名称:codeformatter,代码行数:17,代码来源:BraceNewLineRule.cs

示例10: 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


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