DGreen
DGreen

Reputation: 1027

Roslyn - how to reliably format whitespace for a class

I need to format a class to ensure it's easily human readable. I have the syntax tree, the root CompilationUnitSyntax and the ClassDeclarationSyntax. I format the whitespace 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 is the class's closing brace slammed into the #endregion?

If I run NormalizeWhitespace once more on the 'After' text, #endregion is moved back down onto its own line. Then a further call to NormalizeWhitespace moves it back up again. What is going on?

Upvotes: 5

Views: 1146

Answers (1)

Kevin Pilch
Kevin Pilch

Reputation: 11615

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

Upvotes: 1

Related Questions