✅ Is there a way to programmatically formatt a file

Im writing a transpiler to C# (in C#) and I would like to formatt the output files after I write them to C# does anybody know how to do this? Roslyn does not work:
private async void FormatCode(string path)
{

string code = File.ReadAllText(path);
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
var root = await syntaxTree.GetRootAsync();
var formattedRoot = Formatter.Format(root, Formatter.Annotation, new AdhocWorkspace());
string formattedCode = formattedRoot.ToFullString();
File.WriteAllText(path, formattedCode);
}
private async void FormatCode(string path)
{

string code = File.ReadAllText(path);
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
var root = await syntaxTree.GetRootAsync();
var formattedRoot = Formatter.Format(root, Formatter.Annotation, new AdhocWorkspace());
string formattedCode = formattedRoot.ToFullString();
File.WriteAllText(path, formattedCode);
}
SOLUTION FOUND by SleepWellPupper
14 Replies
Humoroussix2799
Humoroussix2799OP8mo ago
The above results in no change to the src code
Angius
Angius8mo ago
Code formatting is not a concern of Roslyn or any other part of the toolchain It's an IDE concern So you'll probably have to either find some code formatting library Or... just write formatted code in the first place Also, you got an async void there Bad
The Fog from Human Resources
If you use any decent editor you should be able to format the entire document with a hotkey After all how code is written and how its designed is mostly a choice of the user Its the conventions people willingly stick to Not my post, but can you explain why it's bad? :plink:
Angius
Angius8mo ago
It's fire and forget An async Task method returns something, a task It can be awaited, canceled, scheduled, deferred in execution, anything
The Fog from Human Resources
Aaah I see
Angius
Angius8mo ago
async void means nothing gets returned Can't await nothing, can't cancel nothing
The Fog from Human Resources
I see Thank uuu
Angius
Angius8mo ago
Anytime :Ok:
Humoroussix2799
Humoroussix2799OP8mo ago
Yea the transpiler is built for people who don't know how to code (Its a data scripting language) In theory they should be able to look at the code without it looking like hieroglyphs or never look at the code, the goal of the language is to kinda point people in the right direction on how to actually code, knowing the command CTRL + K + CTRL + D may seem obvious to people who code but to someone who has never coded they would probably just think that coding is supposed to look like minified code on one line.
SleepWellPupper
SleepWellPupper8mo ago
https://stackoverflow.com/questions/33467295/generating-well-formatted-syntax-with-roslyn/74412674#74412674
var source = CSharpSyntaxTree.ParseText(mySourceText)
.GetRoot(t)
.NormalizeWhitespace()
.SyntaxTree
.GetText(t)
.ToString();
var source = CSharpSyntaxTree.ParseText(mySourceText)
.GetRoot(t)
.NormalizeWhitespace()
.SyntaxTree
.GetText(t)
.ToString();
Stack Overflow
Generating well-formatted syntax with Roslyn
I am using Roslyn to modify the syntax of C# files. Using CSharpSyntaxRewriter, it is very easy to find and replace nodes in the syntax tree. However, the generated code is very ugly and won't even...
SleepWellPupper
SleepWellPupper8mo ago
There are limits to what can be done using this approach, it'll do a rudimentary job though. You have to do the legwork manually via your transpiler output.
Humoroussix2799
Humoroussix2799OP8mo ago
Thank you that worked!🙂
SleepWellPupper
SleepWellPupper8mo ago
$close
MODiX
MODiX8mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server