Roslyn - how to reliably format spaces for a class

I need to format the class so that it can be easily read by humans. I have a syntax tree, the root of CompilationUnitSyntax and ClassDeclarationSyntax. I will format the spaces as follows.

root = root.ReplaceNode(classSyntax, classSyntax.NormalizeWhitespace(elasticTrivia: true)); syntaxTree = syntaxTree.WithRootAndOptions(root, syntaxTree.Options); 

Before:

 #region MyRegion public class MyClass { // Info about MyClass } #endregion 

After:

 #region MyRegion public class MyClass { // Info about MyClass }#endregion 

Why did the class closing bracket slam shut in #endregion?

If I run NormalizeWhitespace again in the After text, #endregion will be moved back to its own line. Then an additional call to NormalizeWhitespace brings it back again. What's happening?

+5
source share
1 answer

This sounds like an error, possibly related to https://github.com/dotnet/roslyn/issues/1066 .

+1
source

All Articles