This project is read-only.
1
Vote

Formatter ignores NewLine options

description

Run the code below and notice that neither \r\n is removed, nor that \n is being used.
using System;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.MSBuild;

namespace Formatting
{
    class Program
    {
        static void Main(string[] args)
        {

            var unformattedCode = @"using System\r\n; namespace HelloWorld{class Program{static void Main(string[]args){Console.WriteLine(""Hello,World!"");}}}";

            var cu = CSharpSyntaxTree.ParseText(unformattedCode);

            var normalizedNode = cu.GetRoot().NormalizeWhitespace("    ");
            var value = normalizedNode.GetText().ToString();
            if (value.IndexOf("\r\n") >= 0)
            {
                Console.WriteLine("normalized? nope");
            }

            var cw = MSBuildWorkspace.Create();
            var options = cw.Options
                .WithChangedOption(FormattingOptions.NewLine, LanguageNames.CSharp, "\n")
                .WithChangedOption(FormattingOptions.UseTabs, LanguageNames.CSharp, false)
                .WithChangedOption(FormattingOptions.TabSize, LanguageNames.CSharp, 8);

            var formattedNode = Formatter.Format(cu.GetRoot(), cw, options);
            value = formattedNode.GetText().ToString();
            if (value.IndexOf("\r\n") >= 0)
            {
                Console.WriteLine("formatted? nope");
            }

            Console.ReadLine();

        }
    }
}

comments