Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Roslyn Source Generator discover the IDE's spacing/etc preferences?

I'm writing a Roslyn source generator, and as a matter of principle I'd like to make sure the generated source (which is now visible, debuggable, etc) adheres at least somewhat to the user's configuration of tabs vs spaces, brace locations, etc. I don't plan on supporting everything, but the obvious ones: sure.

So; I'm implementing ISourceGenerator and ISyntaxReceiver, which means I have access to the GeneratorInitializationContext, GeneratorExecutionContext and SyntaxNode APIs. However, I cannot find any way to get these IDE settings from here. Is this possible?

This might not even be possible because of the reality that I'm talking about IDE settings and there isn't always an IDE - for example, the user could just be running dotnet build at the command line.

(or perhaps alternatively; is it possible to trigger a "format document" equivalent on the generated code before handing it back to the caller?)

like image 949
Marc Gravell Avatar asked May 01 '21 22:05

Marc Gravell


People also ask

What is a source generator?

A Source Generator is a . NET Standard 2.0 assembly that is loaded by the compiler along with any analyzers. It's usable in environments where . NET Standard components can be loaded and run.

What are some source code generators?

CodeSmith Generator is a software development tool to help you get your job done faster. Technically speaking it is a template driven source code generator that automates the creation of common application source code for any language (C#, Java, VB, PHP, ASP.NET, SQL, etc.).


Video Answer


1 Answers

As observed by the conversation, there's no good way to do this (or at least not as of this writing). A generator could try to use the values from an .editorconfig to know the formatting settings if you want to piece it together, but you'd have to write some of that yourself at this point. My suggestion would be until we build in support for this, just don't worry about it, and feel free to direct user complaints to this or the bug.

No matter what, the debugging problem makes things tricky: the file open in the IDE needs to match the file that was produced during the build, since the debugger expects that to line up. The .editorconfig at least means both sides can see the same value.

like image 197
Jason Malinowski Avatar answered Nov 15 '22 06:11

Jason Malinowski