问题描述
在早期版本 (Roslyn CTP) 中,我使用以下代码来格式化生成的代码,并且运行良好:
In an earlier version (Roslyn CTP), I was using following code to format my generated code and it was working perfectly fine:
SyntaxNode.Format(FormattingOptions.GetDefaultOptions()).GetFormattedRoot()
新的 Roslyn 版本不再如此,那么新版本(SDK 预览版)中上述代码的等效项是什么?
With the new Roslyn version it no longer does, so what is the equivalent for the above code in the new version (SDK Preview)?
推荐答案
您可以像这样使用 Microsoft.CodeAnalysis.Formatting.Formatter
格式化 SyntaxNodes
(如果您有工作区):
You can format SyntaxNodes
using the Microsoft.CodeAnalysis.Formatting.Formatter
like this (if you have a workspace):
using Microsoft.CodeAnalysis.Formatting;
var formattedResult = Formatter.Format(syntaxNode, workspace);
编辑:正如 Jeroen 在评论中所写,如果您没有工作区并且不需要特定于工作区的格式设置,则可以创建一个:
EDIT: As Jeroen wrote in a comment, if you don't have a workspace and don't need workspace-specific formatting settings, you can just create one:
var workspace = MSBuildWorkspace.Create();
这篇关于Roslyn SDK 预览版中的代码格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!