Created
June 18, 2019 18:35
-
-
Save lostmsu/2e727f2d563e2efb4aa2f6530f7b0fdf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace FormattingTest | |
{ | |
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using Microsoft.CodeAnalysis; | |
using Microsoft.CodeAnalysis.CSharp; | |
using Microsoft.CodeAnalysis.Formatting; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string sourceCode = File.ReadAllText(args[0]); | |
var ast = CSharpSyntaxTree.ParseText(sourceCode); | |
var unformatted = //new WhitespaceRemover().Visit( | |
ast.GetRoot() | |
//) | |
; | |
var workspace = new AdhocWorkspace(); | |
workspace.Options = workspace.Options | |
.WithChangedOption(FormattingOptions.NewLine, LanguageNames.CSharp, "\n"); | |
var stopwatch = Stopwatch.StartNew(); | |
var reformatted = Formatter.Format(unformatted, workspace, workspace.Options); | |
string newCode = reformatted.GetText().ToString(); | |
Console.WriteLine($"formatting: {stopwatch.ElapsedMilliseconds / 1000}s"); | |
} | |
} | |
public class WhitespaceRemover : CSharpSyntaxRewriter | |
{ | |
public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia) => default; | |
public override SyntaxNode Visit(SyntaxNode node) => node == null ? null : base.Visit(node).WithoutTrivia(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment