Skip to content

Commit dc4c972

Browse files
committed
Merge pull request #544 from kimberninger/macro_expression_white_space
Insert white space before trailing closure of a macro expression
1 parent 93aade8 commit dc4c972

File tree

2 files changed

+131
-2
lines changed

2 files changed

+131
-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

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import SwiftFormatConfiguration
2+
3+
final class MacroCallTests: PrettyPrintTestCase {
4+
func testNoWhiteSpaceAfterMacroWithoutTrailingClosure() {
5+
let input =
6+
"""
7+
func myFunction() {
8+
print("Currently running \\(#function)")
9+
}
10+
11+
"""
12+
13+
let expected =
14+
"""
15+
func myFunction() {
16+
print("Currently running \\(#function)")
17+
}
18+
19+
"""
20+
21+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 50)
22+
}
23+
24+
func testKeepWhiteSpaceBeforeTrailingClosure() {
25+
let input =
26+
"""
27+
#Preview {}
28+
#Preview("MyPreview") {
29+
MyView()
30+
}
31+
let p = #Predicate<Int> { $0 == 0 }
32+
"""
33+
34+
let expected =
35+
"""
36+
#Preview {}
37+
#Preview("MyPreview") {
38+
MyView()
39+
}
40+
let p = #Predicate<Int> { $0 == 0 }
41+
42+
"""
43+
44+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
45+
}
46+
47+
func testInsertWhiteSpaceBeforeTrailingClosure() {
48+
let input =
49+
"""
50+
#Preview{}
51+
#Preview("MyPreview"){
52+
MyView()
53+
}
54+
let p = #Predicate<Int>{ $0 == 0 }
55+
"""
56+
57+
let expected =
58+
"""
59+
#Preview {}
60+
#Preview("MyPreview") {
61+
MyView()
62+
}
63+
let p = #Predicate<Int> { $0 == 0 }
64+
65+
"""
66+
67+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
68+
}
69+
70+
func testDiscretionaryLineBreakBeforeTrailingClosure() {
71+
let input =
72+
"""
73+
#Preview("MyPreview")
74+
{
75+
MyView()
76+
}
77+
#Preview(
78+
"MyPreview", traits: .landscapeLeft
79+
)
80+
{
81+
MyView()
82+
}
83+
#Preview("MyPreview", traits: .landscapeLeft, .sizeThatFitsLayout)
84+
{
85+
MyView()
86+
}
87+
#Preview("MyPreview", traits: .landscapeLeft) {
88+
MyView()
89+
}
90+
"""
91+
92+
let expected =
93+
"""
94+
#Preview("MyPreview") {
95+
MyView()
96+
}
97+
#Preview(
98+
"MyPreview", traits: .landscapeLeft
99+
) {
100+
MyView()
101+
}
102+
#Preview(
103+
"MyPreview", traits: .landscapeLeft,
104+
.sizeThatFitsLayout
105+
) {
106+
MyView()
107+
}
108+
#Preview("MyPreview", traits: .landscapeLeft)
109+
{
110+
MyView()
111+
}
112+
113+
"""
114+
115+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
116+
}
117+
}

0 commit comments

Comments
 (0)