From 5907222da12cdb2605e823f3be63c8c491431b95 Mon Sep 17 00:00:00 2001 From: stackotter Date: Sun, 2 Apr 2023 21:58:26 +1000 Subject: [PATCH] Fix pretty printing of incorrectly formatted closures with a signature and multiple statements --- .../TokenStreamCreator.swift | 2 +- .../ClosureExprTests.swift | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift b/Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift index 5fc50c76f..46ab83f44 100644 --- a/Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift +++ b/Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift @@ -1073,7 +1073,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { override func visit(_ node: ClosureExprSyntax) -> SyntaxVisitorContinueKind { let newlineBehavior: NewlineBehavior - if forcedBreakingClosures.remove(node.id) != nil { + if forcedBreakingClosures.remove(node.id) != nil || node.statements.count > 1 { newlineBehavior = .soft } else { newlineBehavior = .elective diff --git a/Tests/SwiftFormatPrettyPrintTests/ClosureExprTests.swift b/Tests/SwiftFormatPrettyPrintTests/ClosureExprTests.swift index 5518c5a96..cab0b8553 100644 --- a/Tests/SwiftFormatPrettyPrintTests/ClosureExprTests.swift +++ b/Tests/SwiftFormatPrettyPrintTests/ClosureExprTests.swift @@ -516,4 +516,24 @@ final class ClosureExprTests: PrettyPrintTestCase { assertPrettyPrintEqual(input: input, expected: expected, linelength: 40) } + + func testClosureWithSignatureAndMultipleStatements() { + let input = + """ + { a in a + 1 + a + 2 + } + """ + + let expected = + """ + { a in + a + 1 + a + 2 + } + + """ + + assertPrettyPrintEqual(input: input, expected: expected, linelength: 40) + } }