title: Roslyn - how to reliably format whitespace for a class
body: <p>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.</p>
  
  <pre><code>root = root.ReplaceNode(classSyntax, classSyntax.NormalizeWhitespace(elasticTrivia: true));
  syntaxTree = syntaxTree.WithRootAndOptions(root, syntaxTree.Options);
  </code></pre>
  
  <p>Before:</p>
  
  <pre><code>#region MyRegion
  public class MyClass
  {
  
      // Info about MyClass
  
  }
  #endregion
  </code></pre>
  
  <p>After:</p>
  
  <pre><code>#region MyRegion
  public class MyClass
  {
  // Info about MyClass    
  }#endregion
  </code></pre>
  
  <p>Why is the class's closing brace slammed into the #endregion?</p>
  
  <p>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?</p>