Roslyn - replace node and fix the whitespaces

后端 未结 1 1027
走了就别回头了
走了就别回头了 2021-01-14 02:44

In my program I use Roslyn and I need to replace a node with a new node. For example, if I have code like

public void Foo()
{
   for(var i = 0; i < 5; i++         


        
相关标签:
1条回答
  • 2021-01-14 03:36

    You need to use .WithAdditionalAnnotations(Formatter.Annotation), but only on the specific element you want to format. Here's an example from the NullParameterCheckRefactoring project.

    IfStatementSyntax nullCheckIfStatement = SyntaxFactory.IfStatement(
        SyntaxFactory.Token(SyntaxKind.IfKeyword),
        SyntaxFactory.Token(SyntaxKind.OpenParenToken),
        binaryExpression, 
        SyntaxFactory.Token(SyntaxKind.CloseParenToken), 
        syntaxBlock, null).WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation);
    
    0 讨论(0)
提交回复
热议问题