问题
I am using Roslyn to modify the syntax of C# files. Using CSharpSyntaxRewriter, it is very easy to find and replace nodes in the syntax tree. However, the generated code is very ugly and won't even parse in all cases because the syntax nodes I create (using SyntaxFactory) lack even a minimal amount of whitespace trivia. Does Roslyn provide some basic formatting functionality to avoid this or do I have to add trivia manually to each node I create?
回答1:
Yes - it is possible, using Microsoft.CodeAnalysis.Formatting.Formatter:
var formattedResult = Formatter.Format(syntaxNode, workspace);
来源:https://stackoverflow.com/questions/33467295/generating-well-formatted-syntax-with-roslyn