Code Formatting in Roslyn SDK Preview

左心房为你撑大大i 提交于 2019-12-06 20:26:14

问题


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()

With the new Roslyn version it no longer does, so what is the equivalent for the above code in the new version (SDK Preview)?


回答1:


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);

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();



回答2:


Roslyn has changed quite a lot since the CTP.

Documentation is now here: https://roslyn.codeplex.com/

Follow the link to https://roslyn.codeplex.com/documentation, click on "Samples and Walkthroughs", then open up the demo solution "FormatSolution - A console application that formats all C# and VB source files in a solution.".

Unfortunately, I don't think its possible to quickly get formatting working any more, as you have to add the code to a new solution.



来源:https://stackoverflow.com/questions/23625835/code-formatting-in-roslyn-sdk-preview

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