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

1

7

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?)

Family answered 1/5, 2021 at 22:9 Comment(3)
Side link: after posting this, I was directed at a recent GitHub issue for similar: github.com/dotnet/roslyn/issues/53020Family
IVsSettingsReader to get to the Tools > Options settings. But was meant to be used in-process, the generator runs out-of-process. Technically it could work, I do see a proxy at HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Classes\InterfaceNavarra
@Hans now consider: dotnet build can also run generators (head explodes)Family
L
2

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.

Lewan answered 3/5, 2021 at 18:4 Comment(1)
awesome, I am officially not worrying about this, then :)Family

© 2022 - 2024 — McMap. All rights reserved.