Skip to content

Commit a4100c3

Browse files
dylansturgallevato
authored andcommitted
Disallow discretionary newlines before trailing closures.
The behavior was inconsistent between subscripts and function calls. They both use a same-break that ignores discretionary newlines now. The break ignores discretionary because breaking before a trailing closure's left brace uses excessive vertical whitespace without improving readability. Now the brace is usually glued to the right paren/right square bracket, except when it doesn't fit due to line length.
1 parent 66cac37 commit a4100c3

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
855855
(node.trailingClosure != nil && !isCompactSingleFunctionCallArgument(arguments))
856856
|| mustBreakBeforeClosingDelimiter(of: node, argumentListPath: \.argumentList)
857857

858-
before(node.trailingClosure?.leftBrace, tokens: .break(.same))
858+
before(
859+
node.trailingClosure?.leftBrace,
860+
tokens: .break(.same, newlines: .elective(ignoresDiscretionary: true)))
859861

860862
arrangeFunctionCallArgumentList(
861863
arguments,
@@ -1048,7 +1050,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
10481050
node.trailingClosure != nil
10491051
|| mustBreakBeforeClosingDelimiter(of: node, argumentListPath: \.argumentList)
10501052

1051-
before(node.trailingClosure?.leftBrace, tokens: .space)
1053+
before(
1054+
node.trailingClosure?.leftBrace,
1055+
tokens: .break(.same, newlines: .elective(ignoresDiscretionary: true)))
10521056

10531057
arrangeFunctionCallArgumentList(
10541058
arguments,

Tests/SwiftFormatPrettyPrintTests/FunctionCallTests.swift

+49
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,55 @@ final class FunctionCallTests: PrettyPrintTestCase {
259259
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
260260
}
261261

262+
func testDiscretionaryLineBreakBeforeTrailingClosure() {
263+
let input =
264+
"""
265+
foo(a, b, c)
266+
{
267+
blah()
268+
}
269+
foo(
270+
a, b, c
271+
)
272+
{
273+
blah()
274+
}
275+
foo(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
276+
{
277+
blah()
278+
}
279+
foo(ab, arg1, arg2) {
280+
blah()
281+
}
282+
"""
283+
284+
let expected =
285+
"""
286+
foo(a, b, c) {
287+
blah()
288+
}
289+
foo(
290+
a, b, c
291+
) {
292+
blah()
293+
}
294+
foo(
295+
arg1, arg2, arg3,
296+
arg4, arg5, arg6,
297+
arg7
298+
) {
299+
blah()
300+
}
301+
foo(ab, arg1, arg2)
302+
{
303+
blah()
304+
}
305+
306+
"""
307+
308+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
309+
}
310+
262311
func testGroupsTrailingComma() {
263312
let input =
264313
"""

Tests/SwiftFormatPrettyPrintTests/SubscriptExprTests.swift

+49
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,53 @@ final class SubscriptExprTests: PrettyPrintTestCase {
106106

107107
assertPrettyPrintEqual(input: input, expected: expected, linelength: 70)
108108
}
109+
110+
func testDiscretionaryLineBreakBeforeTrailingClosure() {
111+
let input =
112+
"""
113+
foo[a, b, c]
114+
{
115+
blah()
116+
}
117+
foo[
118+
a, b, c
119+
]
120+
{
121+
blah()
122+
}
123+
foo[arg1, arg2, arg3, arg4, arg5, arg6, arg7]
124+
{
125+
blah()
126+
}
127+
foo[ab, arg1, arg2] {
128+
blah()
129+
}
130+
"""
131+
132+
let expected =
133+
"""
134+
foo[a, b, c] {
135+
blah()
136+
}
137+
foo[
138+
a, b, c
139+
] {
140+
blah()
141+
}
142+
foo[
143+
arg1, arg2, arg3,
144+
arg4, arg5, arg6,
145+
arg7
146+
] {
147+
blah()
148+
}
149+
foo[ab, arg1, arg2]
150+
{
151+
blah()
152+
}
153+
154+
"""
155+
156+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
157+
}
109158
}

Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift

+2
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ extension FunctionCallTests {
298298
("testBasicFunctionCalls_packArguments", testBasicFunctionCalls_packArguments),
299299
("testDiscretionaryLineBreakAfterColon", testDiscretionaryLineBreakAfterColon),
300300
("testDiscretionaryLineBreakBeforeClosingParenthesis", testDiscretionaryLineBreakBeforeClosingParenthesis),
301+
("testDiscretionaryLineBreakBeforeTrailingClosure", testDiscretionaryLineBreakBeforeTrailingClosure),
301302
("testDiscretionaryLineBreaksAreSelfCorrecting", testDiscretionaryLineBreaksAreSelfCorrecting),
302303
("testGroupsTrailingComma", testGroupsTrailingComma),
303304
("testNestedFunctionCallExprSequences", testNestedFunctionCallExprSequences),
@@ -697,6 +698,7 @@ extension SubscriptExprTests {
697698
static let __allTests__SubscriptExprTests = [
698699
("testBasicSubscriptGetters", testBasicSubscriptGetters),
699700
("testBasicSubscriptSetters", testBasicSubscriptSetters),
701+
("testDiscretionaryLineBreakBeforeTrailingClosure", testDiscretionaryLineBreakBeforeTrailingClosure),
700702
("testGroupsTrailingComma", testGroupsTrailingComma),
701703
("testSubscriptGettersWithTrailingClosures", testSubscriptGettersWithTrailingClosures),
702704
("testSubscriptSettersWithTrailingClosures", testSubscriptSettersWithTrailingClosures),

0 commit comments

Comments
 (0)