Use

AttributeSyntax("@MainActor").with(\.leadingTrivia, node.leadingTrivia)

instead. Trailing trivia would be the last part (newlines and spaces) of the leading trivia of the declaration to get the indentation right.

That should work in your posted example. In case other attributes already exist, this becomes a bit trickier, though, depending on whether you want to have the new attribute first or last. Then you need to think thoroughly about which comments to put where. But the basic idea is the same: Take existing trivia and attach it to other nodes.

1 Like