Skip to content

Commit 38d8e10

Browse files
committed
Improve formatting of macro decls with attributes.
Whitespace and newlines were being lost in the token stream when prefixing a macro with an attribute. Call the common attribute handling code, and add a break before the left brace of the decl.
1 parent 6fadf64 commit 38d8e10

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,12 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
13241324
}
13251325

13261326
override func visit(_ node: MacroExpansionDeclSyntax) -> SyntaxVisitorContinueKind {
1327+
arrangeAttributeList(node.attributes)
1328+
1329+
before(
1330+
node.trailingClosure?.leftBrace,
1331+
tokens: .break(.same, newlines: .elective(ignoresDiscretionary: true)))
1332+
13271333
arrangeFunctionCallArgumentList(
13281334
node.arguments,
13291335
leftDelimiter: node.leftParen,

Tests/SwiftFormatTests/PrettyPrint/MacroCallTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,16 @@ final class MacroCallTests: PrettyPrintTestCase {
114114

115115
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
116116
}
117+
118+
func testMacroDeclWithAttributesAndArguments() {
119+
let input = """
120+
@nonsenseAttribute
121+
@available(iOS 17.0, *)
122+
#Preview("Name") {
123+
EmptyView()
124+
}
125+
126+
"""
127+
assertPrettyPrintEqual(input: input, expected: input, linelength: 45)
128+
}
117129
}

0 commit comments

Comments
 (0)