Skip to content

Commit 666e9ad

Browse files
committed
Absorb trailing comma of function call args in grouped exprs.
Previously, these trailing commas could be placed on a new line after the expression because the close break after the expression would fire if the comma landed exactly 1 over the column limit. Now the comma is moved inside of the group and breaks around the expr so that a break inside of the expr fires instead of the close break.
1 parent 9cbfbcb commit 666e9ad

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
837837

838838
let shouldGroupAroundArgument = !isCompactSingleFunctionCallArgument(arguments)
839839
for argument in arguments {
840+
if let trailingComma = argument.trailingComma {
841+
closingDelimiterTokens.insert(trailingComma)
842+
}
840843
arrangeAsFunctionCallArgument(argument, shouldGroup: shouldGroupAroundArgument)
841844
}
842845
}

Tests/SwiftFormatPrettyPrintTests/FunctionCallTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,25 @@ final class FunctionCallTests: PrettyPrintTestCase {
249249

250250
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
251251
}
252+
253+
func testGroupsTrailingComma() {
254+
let input =
255+
"""
256+
foo(
257+
image: useLongName ? image(named: .longNameImage) : image(named: .veryLongNameImageZ),
258+
bar: bar)
259+
"""
260+
261+
let expected =
262+
"""
263+
foo(
264+
image: useLongName
265+
? image(named: .longNameImage)
266+
: image(named: .veryLongNameImageZ),
267+
bar: bar)
268+
269+
"""
270+
271+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 70)
272+
}
252273
}

Tests/SwiftFormatPrettyPrintTests/ObjectLiteralExprTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,25 @@ final class ObjectLiteralExprTests: PrettyPrintTestCase {
110110
config.lineBreakBeforeEachArgument = false
111111
assertPrettyPrintEqual(input: input, expected: expected, linelength: 38, configuration: config)
112112
}
113+
114+
func testGroupsTrailingComma() {
115+
let input =
116+
"""
117+
#imageLiteral(
118+
image: useLongName ? image(named: .longNameImage) : image(named: .veryLongNameImageZ),
119+
bar: bar)
120+
"""
121+
122+
let expected =
123+
"""
124+
#imageLiteral(
125+
image: useLongName
126+
? image(named: .longNameImage)
127+
: image(named: .veryLongNameImageZ),
128+
bar: bar)
129+
130+
"""
131+
132+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 70)
133+
}
113134
}

Tests/SwiftFormatPrettyPrintTests/SubscriptExprTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,25 @@ final class SubscriptExprTests: PrettyPrintTestCase {
8585

8686
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
8787
}
88+
89+
func testGroupsTrailingComma() {
90+
let input =
91+
"""
92+
myCollection[
93+
image: useLongName ? image(named: .longNameImage) : image(named: .veryLongNameImageZ),
94+
bar: bar]
95+
"""
96+
97+
let expected =
98+
"""
99+
myCollection[
100+
image: useLongName
101+
? image(named: .longNameImage)
102+
: image(named: .veryLongNameImageZ),
103+
bar: bar]
104+
105+
"""
106+
107+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 70)
108+
}
88109
}

Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ extension FunctionCallTests {
270270
("testBasicFunctionCalls_packArguments", testBasicFunctionCalls_packArguments),
271271
("testDiscretionaryLineBreakBeforeClosingParenthesis", testDiscretionaryLineBreakBeforeClosingParenthesis),
272272
("testDiscretionaryLineBreaksAreSelfCorrecting", testDiscretionaryLineBreaksAreSelfCorrecting),
273+
("testGroupsTrailingComma", testGroupsTrailingComma),
273274
("testIgnoresDiscretionaryLineBreakAfterColon", testIgnoresDiscretionaryLineBreakAfterColon),
274275
("testNestedFunctionCallExprSequences", testNestedFunctionCallExprSequences),
275276
("testSingleUnlabeledArgumentWithDelimiters", testSingleUnlabeledArgumentWithDelimiters),
@@ -473,6 +474,7 @@ extension ObjectLiteralExprTests {
473474
static let __allTests__ObjectLiteralExprTests = [
474475
("testColorLiteral_noPackArguments", testColorLiteral_noPackArguments),
475476
("testColorLiteral_packArguments", testColorLiteral_packArguments),
477+
("testGroupsTrailingComma", testGroupsTrailingComma),
476478
("testImageLiteral_noPackArguments", testImageLiteral_noPackArguments),
477479
("testImageLiteral_packArguments", testImageLiteral_packArguments),
478480
]
@@ -660,6 +662,7 @@ extension SubscriptExprTests {
660662
static let __allTests__SubscriptExprTests = [
661663
("testBasicSubscriptGetters", testBasicSubscriptGetters),
662664
("testBasicSubscriptSetters", testBasicSubscriptSetters),
665+
("testGroupsTrailingComma", testGroupsTrailingComma),
663666
("testSubscriptGettersWithTrailingClosures", testSubscriptGettersWithTrailingClosures),
664667
("testSubscriptSettersWithTrailingClosures", testSubscriptSettersWithTrailingClosures),
665668
]

0 commit comments

Comments
 (0)