Producing single-line code blocks in a macro

I'm making a macro to generate default implementations for protocols, and I want them to come out on a single line, but SwiftSyntax insists on adding more space.

For example, it turns this:

var x: Int { 0 }

into this (including the extra space after Int):

var x: Int  {
      0
  }

I've tried going through the generated nodes and clearing out the trivia, but it doesn't seem to have any effect. How do I get more control over the generated white space?

You need to add formatCode property with disabled value.
Although, it would be nice a way to fine control which nodes are automatically formatted and which are not.

4 Likes

Thanks! It took a bit more work than that because it also turned off some formatting I didn't realize I was relying on. But in the end I got it :)

2 Likes

Did you used formatted? I just realized that this can be used to format specific nodes when formatCode is disabled.

1 Like