Best way to format source generated code? #75722
-
What's the recommendation on nicely formatting generated code? At the moment I'm just using a string builder and trying to handle it manually but with line breaks and tabs it gets difficult if the code becomes nested. I know you can parse a syntax tree and call normalize whitespace, but is that going to dramatically slow down my source generator? I don't want a laggy experience for users. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Best way is to just emit in a properly formatted fashion to begin with. Just use a string building abstraction that supports indentation. Indent when you enter a block, and dedent when you leave. |
Beta Was this translation helpful? Give feedback.
-
When I played with source generators, I used the csharpier api to format the code. |
Beta Was this translation helpful? Give feedback.
Best way is to just emit in a properly formatted fashion to begin with. Just use a string building abstraction that supports indentation. Indent when you enter a block, and dedent when you leave.