Roslyn 代码修复:使用单行注释创建方法

问题描述 投票:0回答:1

我是 Roslyn 代码修复的新手,并尝试使用单行注释创建一个格式正确(缩进、换行)的空方法:

之前

public class Bla
{
    public void Blub()
    {
        Console.WriteLine("");
    }
}

之后

public class Bla
{
    public void Blub()
    {
        // I told you so
    }
}

我已经快到了,但我总是漏掉一小部分。这是我的尝试:

v1 - 通过
WithStatements

添加评论

代码:

private static async Task<Document> RemoveContainOfDownMethod(Document document, BaseMethodDeclarationSyntax downMethodDeclaration, CancellationToken cancellationToken)
{
    SyntaxTree? documentSyntaxTree = await document.GetSyntaxTreeAsync(cancellationToken);
    if (documentSyntaxTree == null || downMethodDeclaration.Body == null)
    {
        return document;
    }

    var commentTrivia = SyntaxFactory.Comment("// I told you so.");
    var emptyBlock = SyntaxFactory.Block()
        .WithOpenBraceToken(downMethodDeclaration.Body.OpenBraceToken)
        .WithCloseBraceToken(downMethodDeclaration.Body.CloseBraceToken)
        .WithStatements(SyntaxFactory.SingletonList<StatementSyntax>(
            SyntaxFactory.EmptyStatement().WithLeadingTrivia(commentTrivia)));

    CompilationUnitSyntax newDocument = documentSyntaxTree.GetCompilationUnitRoot(cancellationToken)
        .ReplaceNode(
            downMethodDeclaration.Body,
            emptyBlock
                .WithLeadingTrivia(downMethodDeclaration.Body.GetLeadingTrivia())
                .WithTrailingTrivia(downMethodDeclaration.Body.GetTrailingTrivia()));

    return document.WithSyntaxRoot(newDocument);
}

给予:

public class Bla
{
    public void Blub()
    {
        // I told you so                                        ;
    }
}

缺陷:

  • 尾随分号

v2 - 通过
WithCloseBraceToken

添加评论

代码:

private static async Task<Document> RemoveContainOfDownMethod(Document document, BaseMethodDeclarationSyntax downMethodDeclaration, CancellationToken cancellationToken)
{
    SyntaxTree? documentSyntaxTree = await document.GetSyntaxTreeAsync(cancellationToken);
    if (documentSyntaxTree == null || downMethodDeclaration.Body == null)
    {
        return document;
    }

    SyntaxTrivia commentTrivia = SyntaxFactory.Comment("// I told you so");
    BlockSyntax emptyBlock = SyntaxFactory.Block()
            .WithOpenBraceToken(SyntaxFactory.Token(SyntaxKind.OpenBraceToken))
            .WithCloseBraceToken(SyntaxFactory.Token(SyntaxKind.CloseBraceToken).WithLeadingTrivia(commentTrivia));

    CompilationUnitSyntax newDocument = documentSyntaxTree.GetCompilationUnitRoot(cancellationToken)
        .ReplaceNode(
            downMethodDeclaration.Body,
            emptyBlock
                .WithLeadingTrivia(downMethodDeclaration.Body.GetLeadingTrivia())
                .WithTrailingTrivia(downMethodDeclaration.Body.GetTrailingTrivia()));

    return document.WithSyntaxRoot(newDocument);
}

给予:

public class Bla
{
    public void Blub()
    {
        // I told you so        }
}

缺陷:

  • Blub
  • 的大括号前缺少换行符

v3 通过
WithCloseBraceToken
使用
NormalizeWhitespace

添加评论

代码:

private static async Task<Document> RemoveContainOfDownMethod(Document document, BaseMethodDeclarationSyntax downMethodDeclaration, CancellationToken cancellationToken)
{
    SyntaxTree? documentSyntaxTree = await document.GetSyntaxTreeAsync(cancellationToken);
    if (documentSyntaxTree == null || downMethodDeclaration.Body == null)
    {
        return document;
    }

    SyntaxTrivia commentTrivia = SyntaxFactory.Comment("// I told you so");
    BlockSyntax emptyBlock = SyntaxFactory.Block()
            .WithOpenBraceToken(SyntaxFactory.Token(SyntaxKind.OpenBraceToken))
            .WithCloseBraceToken(SyntaxFactory.Token(SyntaxKind.CloseBraceToken).WithLeadingTrivia(commentTrivia));

    CompilationUnitSyntax newDocument = documentSyntaxTree.GetCompilationUnitRoot(cancellationToken)
        .ReplaceNode(
            downMethodDeclaration.Body,
            emptyBlock
                .WithLeadingTrivia(downMethodDeclaration.Body.GetLeadingTrivia())
                .WithTrailingTrivia(downMethodDeclaration.Body.GetTrailingTrivia()))
        .NormalizeWhitespace();

    return document.WithSyntaxRoot(newDocument);
}

给予:

public class Bla
{
    public void Blub()
    {
    // I told you so
    }
}

缺陷:

  • 评论缩进错误
c# roslyn roslyn-code-analysis
1个回答
0
投票

使用 SharpLab 查看所需的语法树,我们可以看到您在

EndOfLineTrivia
之后缺少
CommentTrivia

您应该将两者都传递给

.WithLeadingTrivia

    SyntaxTrivia commentTrivia = SyntaxFactory.Comment("// I told you so");
    BlockSyntax emptyBlock = SyntaxFactory.Block()
            .WithOpenBraceToken(SyntaxFactory.Token(SyntaxKind.OpenBraceToken))
            .WithCloseBraceToken(SyntaxFactory.Token(SyntaxKind.CloseBraceToken)
                .WithLeadingTrivia(commentTrivia, SyntaxFactory.LineFeed));

Roslyn Quoter 是一个从 C# 代码生成语法树生成代码的好工具。

© www.soinside.com 2019 - 2024. All rights reserved.