Formatter ignores NewLine option
See original GitHub issueRun 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();
}
}
}
Issue Analytics
- State:
- Created 9 years ago
- Comments:17 (14 by maintainers)
Top Results From Across the Web
Java Formatter not allowing new line character?
Java Formatter not allowing new line character? ... Only problem is, the output doesn't have newline characters. Doesn't work if I replace "\n" ......
Read more >a way to ensure formatted code ends with a newline
Is it possible to modify the formatter config file to ensure that the formatter ... Most have options to ignore whitespace (or newlines...
Read more >C# formatting options - .NET
Learn about the code style options for formatting C# code files. ... The new-line options concern the use of new lines to format...
Read more >Line Formats (Comparing and Merging Files)
This format ignores whether the line is incomplete; See Incomplete Lines. ' %L '. stands for the contents of the line, including its...
Read more >Options
Prettier ships with a handful of format options. ... JSX quotes ignore this option – see jsx-single-quote. If the number of quotes outweighs...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So, we simply need to write a 700 line class to support \n. I see.
Another way from above example.