代码格式在Roslyn SDK预览
本文关键字:SDK 预览 Roslyn 格式 代码 | 更新日期: 2023-09-27 18:03:17
在较早的版本(Roslyn CTP)中,我使用以下代码来格式化生成的代码,它运行得非常好:
SyntaxNode.Format(FormattingOptions.GetDefaultOptions()).GetFormattedRoot()
在新的Roslyn版本中,它不再这样做,那么在新版本(SDK预览)中,上面的代码的等效是什么?
您可以像这样使用Microsoft.CodeAnalysis.Formatting.Formatter
来格式化SyntaxNodes
(如果您有工作区):
using Microsoft.CodeAnalysis.Formatting;
var formattedResult = Formatter.Format(syntaxNode, workspace);
EDIT:正如Jeroen在评论中所写的,如果你没有工作空间,也不需要特定于工作空间的格式设置,你可以创建一个:
var workspace = MSBuildWorkspace.Create();
Roslyn自CTP以来变化很大。
文档现在在这里:https://roslyn.codeplex.com/
点击https://roslyn.codeplex.com/documentation的链接,点击"Samples and walkthrough",然后打开演示解决方案"FormatSolution -一个在解决方案中格式化所有c#和VB源文件的控制台应用程序"。
不幸的是,我认为它不可能快速获得格式化工作,因为你必须添加代码到一个新的解决方案。
我想这对你会有用的。
var syntaxTree = CSharpSyntaxTree.ParseText(finalProgramText);
var compilationRoot = syntaxTree.GetCompilationUnitRoot();
/*
MSBuildLocator.RegisterDefaults();
var workspace = MSBuildWorkspace.Create();
var sharpTree = CSharpSyntaxTree.ParseText(programText);
var formattedNode = Formatter.Format(sharpTree.GetRoot(), workspace);
var fullText = formattedNode.ToFullString();
*/
var nameSpaces = from n in compilationRoot.DescendantNodes().OfType<UsingDirectiveSyntax>() select n;
var usings = compilationRoot.Usings.AddRange(nameSpaces);
var propertyNodes = (from field in compilationRoot.DescendantNodes().OfType<PropertyDeclarationSyntax>() select field).ToList();
var st = new SyntaxTrivia();
var withoutProperty = compilationRoot.ReplaceNodes(propertyNodes, (syntax, declarationSyntax) => null).NormalizeWhitespace();
var withoutPropText = withoutProperty.GetText().ToString();
var tree = CSharpSyntaxTree.ParseText(withoutPropText);
var root = tree.GetCompilationUnitRoot();
var commentTrivia = from t in root.DescendantTrivia()
where t.IsKind(SyntaxKind.SingleLineCommentTrivia)
|| t.IsKind(SyntaxKind.MultiLineCommentTrivia)
|| t.IsKind(SyntaxKind.EndRegionDirectiveTrivia)
|| t.IsKind(SyntaxKind.RegionDirectiveTrivia)
|| t.IsKind(SyntaxKind.PragmaChecksumDirectiveTrivia)
|| t.IsKind(SyntaxKind.PragmaWarningDirectiveTrivia)
|| t.IsKind(SyntaxKind.PragmaKeyword)
|| t.IsKind(SyntaxKind.EmptyStatement)
|| t.IsKind(SyntaxKind.XmlComment)
|| t.IsKind(SyntaxKind.SingleLineDocumentationCommentTrivia)
|| t.IsKind(SyntaxKind.AttributeList)
select t;
var newRoot = root.ReplaceTrivia(commentTrivia, (t1, t2) => st).NormalizeWhitespace();
var text = newRoot.GetText().ToString();
var attrTree = CSharpSyntaxTree.ParseText(text);
var attrRoot = attrTree.GetCompilationUnitRoot();
var attrList = from a in attrRoot.DescendantNodes().OfType<AttributeListSyntax>() select a;
var methodList = (from m in attrRoot.DescendantNodes().OfType<MethodDeclarationSyntax>()
where m.ExpressionBody != null
select m).ToList();
var normalMethods = (from m in attrRoot.DescendantNodes().OfType<MethodDeclarationSyntax>()
where m.ExpressionBody == null
select m).ToList();
Console.WriteLine(normalMethods.Count);
var withoutAttrList = attrRoot.ReplaceNodes(attrList, (syntax, listSyntax) => null).NormalizeWhitespace()
.GetText().ToString();
var fieldTree = CSharpSyntaxTree.ParseText(withoutAttrList);
var fieldRoot = fieldTree.GetCompilationUnitRoot();
var fieldsList = (from field in fieldRoot.DescendantNodes().OfType<FieldDeclarationSyntax>() select field).ToList();
var withoutFields = fieldRoot.ReplaceNodes(fieldsList, (syntax, declarationSyntax) => null);
var plainTextLines = withoutFields.NormalizeWhitespace().GetText().ToString().Split(''n')
.Where(d => !usings.Any(u => d.Trim(''r', ' ').Equals(u.ToString())) && !string.IsNullOrEmpty(d.Trim(''r', ' '))
&& !Regex.IsMatch(d.Trim(''r', ' '), @"^([;()'[']]+)$")
).Select(s => Regex.Replace(s.TrimEnd(''r', ''n', ' '), "''s+", " ")).ToList();
Console.WriteLine(plainTextLines);