我是 Roslyn 代码修复的新手,并尝试使用单行注释创建一个格式正确(缩进、换行)的空方法:
之前:
public class Bla
{
public void Blub()
{
Console.WriteLine("");
}
}
之后:
public class Bla
{
public void Blub()
{
// I told you so
}
}
我已经快到了,但我总是漏掉一小部分。这是我的尝试:
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 ;
}
}
缺陷:
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
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
}
}
缺陷:
使用 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# 代码生成语法树生成代码的好工具。