Skip to content

Commit

Permalink
Improve formatting of macro decls with attributes.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shawnhyam authored and ahoppen committed Jul 26, 2024
1 parent 53eae80 commit 53a2680
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,12 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

override func visit(_ node: MacroExpansionDeclSyntax) -> SyntaxVisitorContinueKind {
arrangeAttributeList(node.attributes)

before(
node.trailingClosure?.leftBrace,
tokens: .break(.same, newlines: .elective(ignoresDiscretionary: true)))

arrangeFunctionCallArgumentList(
node.arguments,
leftDelimiter: node.leftParen,
Expand Down
12 changes: 12 additions & 0 deletions Tests/SwiftFormatTests/PrettyPrint/MacroCallTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,16 @@ final class MacroCallTests: PrettyPrintTestCase {

assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
}

func testMacroDeclWithAttributesAndArguments() {
let input = """
@nonsenseAttribute
@available(iOS 17.0, *)
#Preview("Name") {
EmptyView()
}
"""
assertPrettyPrintEqual(input: input, expected: input, linelength: 45)
}
}

0 comments on commit 53a2680

Please sign in to comment.