Skip to content

Commit 5907222

Browse files
committed
Fix pretty printing of incorrectly formatted closures with a signature and multiple statements
1 parent 89a0b6f commit 5907222

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
10731073

10741074
override func visit(_ node: ClosureExprSyntax) -> SyntaxVisitorContinueKind {
10751075
let newlineBehavior: NewlineBehavior
1076-
if forcedBreakingClosures.remove(node.id) != nil {
1076+
if forcedBreakingClosures.remove(node.id) != nil || node.statements.count > 1 {
10771077
newlineBehavior = .soft
10781078
} else {
10791079
newlineBehavior = .elective

Tests/SwiftFormatPrettyPrintTests/ClosureExprTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,4 +516,24 @@ final class ClosureExprTests: PrettyPrintTestCase {
516516

517517
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
518518
}
519+
520+
func testClosureWithSignatureAndMultipleStatements() {
521+
let input =
522+
"""
523+
{ a in a + 1
524+
a + 2
525+
}
526+
"""
527+
528+
let expected =
529+
"""
530+
{ a in
531+
a + 1
532+
a + 2
533+
}
534+
535+
"""
536+
537+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
538+
}
519539
}

0 commit comments

Comments
 (0)