Generating well-formatted syntax with Roslyn

你离开我真会死。 提交于 2019-12-22 06:55:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!