Skip to content

Commit 3bfed5e

Browse files
committed
insert space before closures in macro expressions
If a freestanding macro expression contains a trailing closure keep a, single space before its opening brace or insert one if none exists. Part of #542
1 parent 955dce0 commit 3bfed5e

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,11 +1296,23 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
12961296
}
12971297

12981298
override func visit(_ node: MacroExpansionExprSyntax) -> SyntaxVisitorContinueKind {
1299+
let arguments = node.argumentList
1300+
1301+
// If there is a trailing closure, force the right parenthesis down to the next line so it
1302+
// stays with the open curly brace.
1303+
let breakBeforeRightParen =
1304+
(node.trailingClosure != nil && !isCompactSingleFunctionCallArgument(arguments))
1305+
|| mustBreakBeforeClosingDelimiter(of: node, argumentListPath: \.argumentList)
1306+
1307+
before(
1308+
node.trailingClosure?.leftBrace,
1309+
tokens: .break(.same, newlines: .elective(ignoresDiscretionary: true)))
1310+
12991311
arrangeFunctionCallArgumentList(
1300-
node.argumentList,
1312+
arguments,
13011313
leftDelimiter: node.leftParen,
13021314
rightDelimiter: node.rightParen,
1303-
forcesBreakBeforeRightDelimiter: false)
1315+
forcesBreakBeforeRightDelimiter: breakBeforeRightParen)
13041316
return .visitChildren
13051317
}
13061318

0 commit comments

Comments
 (0)