Skip to content

Commit fd0626f

Browse files
authored
Merge pull request swiftlang#2 from allevato/sr-11105
Remove extra space for closures with captures but no arguments.
2 parents f38829e + 8f7e945 commit fd0626f

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,9 +689,16 @@ private final class TokenStreamCreator: SyntaxVisitor {
689689
}
690690

691691
before(node.firstToken, tokens: .open)
692-
after(node.capture?.rightSquare, tokens: .break(.same))
693-
before(node.input?.firstToken, tokens: .open(consistency))
694-
after(node.input?.lastToken, tokens: .close)
692+
693+
if let input = node.input {
694+
// We unconditionally put a break before the `in` keyword below, so we should only put a break
695+
// after the capture list's right bracket if there are arguments following it or we'll end up
696+
// with an extra space if the line doesn't wrap.
697+
after(node.capture?.rightSquare, tokens: .break(.same))
698+
before(input.firstToken, tokens: .open(consistency))
699+
after(input.lastToken, tokens: .close)
700+
}
701+
695702
before(node.throwsTok, tokens: .break)
696703
before(node.output?.arrow, tokens: .break)
697704
after(node.lastToken, tokens: .close)

Tests/SwiftFormatPrettyPrintTests/ClosureExprTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,23 @@ public class ClosureExprTests: PrettyPrintTestCase {
233233
assertPrettyPrintEqual(input: input, expected: expected, linelength: 60)
234234
}
235235

236+
public func testClosureCaptureWithoutArguments() {
237+
let input =
238+
"""
239+
let a = { [weak self] in return foo }
240+
"""
241+
242+
let expected =
243+
"""
244+
let a = { [weak self] in
245+
return foo
246+
}
247+
248+
"""
249+
250+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 30)
251+
}
252+
236253
public func testBodilessClosure() {
237254
let input =
238255
"""

Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ extension ClosureExprTests {
9191
("testBasicFunctionClosures_packArguments", testBasicFunctionClosures_packArguments),
9292
("testBodilessClosure", testBodilessClosure),
9393
("testClosureCapture", testClosureCapture),
94+
("testClosureCaptureWithoutArguments", testClosureCaptureWithoutArguments),
9495
("testClosuresWithIfs", testClosuresWithIfs),
9596
("testTrailingClosure", testTrailingClosure),
9697
]

0 commit comments

Comments
 (0)