Skip to content

Commit 0cf973c

Browse files
committed
Force breaking in repeat body when while condition is too long.
Repeat-while-stmt is intended to keep the right brace next to the `while` token, like if-else statements. Previously, there wasn't a group so the pretty printer was allowing the repeat body to not have any breaks when the while condition overflowed the line.
1 parent 5e7ef93 commit 0cf973c

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,19 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
533533
override func visit(_ node: RepeatWhileStmtSyntax) -> SyntaxVisitorContinueKind {
534534
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
535535

536-
let whilePrecedingBreak = config.lineBreakBeforeControlFlowKeywords
537-
? Token.break(.same) : Token.space
538-
before(node.whileKeyword, tokens: whilePrecedingBreak)
536+
if config.lineBreakBeforeControlFlowKeywords {
537+
before(node.whileKeyword, tokens: .break(.same), .open)
538+
after(node.condition.lastToken, tokens: .close)
539+
} else {
540+
// The length of the condition needs to force the breaks around the braces of the repeat
541+
// stmt's body, so that there's always a break before the right brace when the while &
542+
// condition is too long to be on one line.
543+
before(node.whileKeyword, tokens: .space)
544+
// The `open` token occurs after the ending tokens for the braced `body` node.
545+
before(node.body.rightBrace, tokens: .open)
546+
after(node.condition.lastToken, tokens: .close)
547+
}
539548
after(node.whileKeyword, tokens: .space)
540-
541549
return .visitChildren
542550
}
543551

Tests/SwiftFormatPrettyPrintTests/RepeatStmtTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ final class RepeatStmtTests: PrettyPrintTestCase {
1010
while x
1111
repeat { foo() }
1212
while longcondition
13+
repeat { f() }
14+
while long.condition
15+
repeat { f() } while long.condition
16+
repeat { f() } while long.condition.that.ison.many.lines
1317
repeat {
1418
let a = 123
1519
var b = "abc"
@@ -29,6 +33,16 @@ final class RepeatStmtTests: PrettyPrintTestCase {
2933
repeat {
3034
foo()
3135
} while longcondition
36+
repeat {
37+
f()
38+
} while long.condition
39+
repeat {
40+
f()
41+
} while long.condition
42+
repeat {
43+
f()
44+
} while long.condition
45+
.that.ison.many.lines
3246
repeat {
3347
let a = 123
3448
var b = "abc"
@@ -50,6 +64,10 @@ final class RepeatStmtTests: PrettyPrintTestCase {
5064
repeat {} while x
5165
repeat { f() } while x
5266
repeat { foo() } while longcondition
67+
repeat { f() }
68+
while long.condition
69+
repeat { f() } while long.condition
70+
repeat { f() } while long.condition.that.ison.many.lines
5371
repeat {
5472
let a = 123
5573
var b = "abc"
@@ -68,6 +86,13 @@ final class RepeatStmtTests: PrettyPrintTestCase {
6886
repeat { f() } while x
6987
repeat { foo() }
7088
while longcondition
89+
repeat { f() }
90+
while long.condition
91+
repeat { f() }
92+
while long.condition
93+
repeat { f() }
94+
while long.condition.that
95+
.ison.many.lines
7196
repeat {
7297
let a = 123
7398
var b = "abc"

0 commit comments

Comments
 (0)