title: C# -> Roslyn -> DocumentEditor.ReplaceNode -> Code indentation and format
body: <p>I have a problem that when I use DocumentEditor.ReplaceNode everything works but the generated code is hard to read.</p>
  
  <p><a href="https://stackoverflow.com/questions/27322543/roslyn-replace-node-and-fix-the-whitespaces">Roslyn - replace node and fix the whitespaces</a></p>
  
  <p>Output looks like this with several strings on the same line:</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Linq;
  using System.Text;
  
  namespace HelloWorld
  {
      class Program
      {
          static void Main(string[] args)
          {
  
              string test5 = @"test symbols \r\n © @ {} [] &lt;&gt; | / \ $£@!\#¤%&amp;/()=?` hello";
              string varTest1 = @"test var hello"; string varTest2 = @"test var hello";
              string test1 = @"test string hello";
              string test2 = @"test String hello"; string test3 = @"test const hello"; string test4 = @"test readonly hello";
              int i = 0;
  
              var i2 = 0;
          }
  
      }
  }
  </code></pre>
  
  <p>I can get a new line by adding <code>{System.Environment.NewLine}</code> to the end of the string and remove all formating but then the code is not indented.</p>
  
  <p>What I have tried:</p>
  
  <p>1:</p>
  
  <pre><code>var newVariable = SyntaxFactory.ParseStatement($"string {variable.Identifier.ValueText} = @\"{value + " hello"}\";").WithAdditionalAnnotations(Formatter.Annotation);
  
  newVariable = newVariable.NormalizeWhitespace();
  </code></pre>
  
  <p>2:</p>
  
  <pre><code>var newVariable = SyntaxFactory.ParseStatement($"string {variable.Identifier.ValueText} = @\"{value + " hello"}\";").WithAdditionalAnnotations(Formatter.Annotation);
  </code></pre>
  
  <p>3:</p>
  
  <pre><code>var newVariable = SyntaxFactory.ParseStatement($"string {variable.Identifier.ValueText} = @\"{value + " hello"}\";").WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation);
  
  newVariable = newVariable.NormalizeWhitespace();
  </code></pre>
  
  <p>4:</p>
  
  <pre><code>var newVariable = SyntaxFactory.ParseStatement($"string {variable.Identifier.ValueText} = @\"{value + " hello"}\";");
  
  newVariable = newVariable.NormalizeWhitespace();
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using Microsoft.CodeAnalysis;
  using Microsoft.CodeAnalysis.CSharp;
  using Microsoft.CodeAnalysis.CSharp.Symbols;
  using Microsoft.CodeAnalysis.CSharp.Syntax;
  using Microsoft.CodeAnalysis.Editing;
  using Microsoft.CodeAnalysis.Formatting;
  using Microsoft.CodeAnalysis.MSBuild;
  using Microsoft.CodeAnalysis.Simplification;
  using Microsoft.CodeAnalysis.Text;
  
  namespace CodeAnalysisApp1
  {
      class Program
      {
          static void Main(string[] args)
          {
              var workspace = new AdhocWorkspace();
              var projectId = ProjectId.CreateNewId();
              var versionStamp = VersionStamp.Create();
              var projectInfo = ProjectInfo.Create(projectId, versionStamp, "NewProject", "projName", LanguageNames.CSharp);
              var newProject = workspace.AddProject(projectInfo);
  
              var sourceText = SourceText.From(
                  @"
                    using System;
                    using System.IO;
                    using System.Linq;
                    using System.Text;
  
                    namespace HelloWorld
                    {
                        class Program
                        {
                            static void Main(string[] args)
                            {
  
                                string test5 = ""test symbols \r\n © @ {} [] &lt;&gt; | / \ $£@!\#¤%&amp;/()=?`""; 
  
                                var varTest1 = ""test var"";
  
                                var varTest2 = ""test var"";
  
                                string test1 = ""test string"";
  
                                String test2 = ""test String"";
  
                                const string test3 = ""test const""; 
  
                                readonly string test4 = ""test readonly""; 
  
                                int i = 0;
  
                                var i2 = 0;
                            }
  
                        }
                    }");
  
              var document = workspace.AddDocument(newProject.Id, "NewFile.cs", sourceText);
              var syntaxRoot = document.GetSyntaxRootAsync().Result;
  
              var root = (CompilationUnitSyntax)syntaxRoot;
  
              var editor = DocumentEditor.CreateAsync(document).Result;
  
  
              var localDeclaration = new LocalDeclarationVirtualizationVisitor();
              localDeclaration.Visit(root);
  
              var localDeclarations = localDeclaration.LocalDeclarations;
  
              foreach (var localDeclarationStatementSyntax in localDeclarations)
              {
                  foreach (VariableDeclaratorSyntax variable in localDeclarationStatementSyntax.Declaration.Variables)
                  {
  
                      var stringKind = variable.Initializer.Value.Kind();
  
                      //Replace string variables
                      if (stringKind == SyntaxKind.StringLiteralExpression)
                      {
                          //Remove " from string
                          var value = variable.Initializer.Value.ToString().Remove(0, 1);
                          value = value.Remove(value.Length - 1, 1);
  
                          var newVariable = SyntaxFactory.ParseStatement($"string {variable.Identifier.ValueText} = @\"{value + " hello"}\";").WithAdditionalAnnotations(Formatter.Annotation, Simplifier.Annotation);
  
                          newVariable = newVariable.NormalizeWhitespace();
  
                          editor.ReplaceNode(variable, newVariable);
  
                          Console.WriteLine($"Key: {variable.Identifier.Value} Value:{variable.Initializer.Value}");
                      }
                  }
              }
  
              var newDocument = editor.GetChangedDocument();
  
              var text = newDocument.GetTextAsync().Result.ToString();
          }
      }
  
      class LocalDeclarationVirtualizationVisitor : CSharpSyntaxRewriter
      {
          public LocalDeclarationVirtualizationVisitor()
          {
              LocalDeclarations = new List&lt;LocalDeclarationStatementSyntax&gt;();
          }
  
          public List&lt;LocalDeclarationStatementSyntax&gt; LocalDeclarations { get; set; }
  
          public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
          {
              node = (LocalDeclarationStatementSyntax)base.VisitLocalDeclarationStatement(node);
              LocalDeclarations.Add(node);
              return node;
          }
      }
  }
  </code></pre>