Skip to content

Commit 115870c

Browse files
authored
Merge pull request swiftlang#244 from dabelknap/overhanging-comma
Fix a bug where a delimiting comma could exceed the line limit.
2 parents d97dd1c + 643fa5d commit 115870c

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,13 @@ private final class TokenStreamCreator: SyntaxVisitor {
962962
}
963963

964964
override func visit(_ node: ConditionElementSyntax) -> SyntaxVisitorContinueKind {
965-
after(node.trailingComma, tokens:. break)
965+
before(node.firstToken, tokens: .open)
966+
if let comma = node.trailingComma {
967+
after(comma, tokens: .close, .break)
968+
}
969+
else {
970+
after(node.lastToken, tokens: .close)
971+
}
966972
return .visitChildren
967973
}
968974

@@ -1375,9 +1381,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
13751381
}
13761382

13771383
override func visit(_ node: OptionalBindingConditionSyntax) -> SyntaxVisitorContinueKind {
1378-
before(node.firstToken, tokens: .open)
13791384
after(node.letOrVarKeyword, tokens: .break)
1380-
after(node.lastToken, tokens: .close)
13811385
return .visitChildren
13821386
}
13831387

Tests/SwiftFormatPrettyPrintTests/IfStmtTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,26 @@ public class IfStmtTests: PrettyPrintTestCase {
182182

183183
assertPrettyPrintEqual(input: input, expected: expected, linelength: 30)
184184
}
185+
186+
public func testIfLetStatements() {
187+
let input =
188+
"""
189+
if let SomeReallyLongVar = Some.More.Stuff(), let a = myfunc() {
190+
// do stuff
191+
}
192+
"""
193+
194+
let expected =
195+
"""
196+
if let SomeReallyLongVar
197+
= Some.More.Stuff(), let a = myfunc()
198+
{
199+
// do stuff
200+
}
201+
202+
"""
203+
204+
// The line length ends on the last paren of .Stuff()
205+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 44)
206+
}
185207
}

0 commit comments

Comments
 (0)