Skip to content

Commit fba8314

Browse files
committed
Add breaks around conditions of while-stmt.
This aligns the behavior of while-stmt and guard-stmt conditions. The while-stmt conditions didn't have open-breaks, which meant there was no additional indentation nor would continuation breaks be able to stack. This means there's always a break before the first token of the condition element list when the while-stmt's condition is multiple lines. That is likely to cause some churn in existing code. An alternative would be to treat this like if-stmt, where there is no break after the while token.
1 parent b573b95 commit fba8314

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
526526
after(node.labelColon, tokens: .space)
527527
after(node.whileKeyword, tokens: .space)
528528

529+
// Add break groups, using open continuation breaks, around all conditions so that continuations
530+
// inside of the conditions can stack in addition to continuations between the conditions.
531+
for condition in node.conditions {
532+
before(condition.firstToken, tokens: .break(.open(kind: .continuation), size: 0))
533+
after(condition.lastToken, tokens: .break(.close(mustBreak: false), size: 0))
534+
}
535+
529536
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
530537

531538
return .visitChildren

Tests/SwiftFormatPrettyPrintTests/WhileStmtTests.swift

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ final class WhileStmtTests: PrettyPrintTestCase {
3535
let a = 123
3636
var b = "abc"
3737
}
38-
while condition1
39-
&& condition2
40-
|| condition3
38+
while
39+
condition1
40+
&& condition2
41+
|| condition3
4142
{
4243
let a = 123
4344
var b = "abc"
@@ -76,9 +77,10 @@ final class WhileStmtTests: PrettyPrintTestCase {
7677
let a = 123
7778
var b = "abc"
7879
}
79-
myLabel: while condition1
80-
&& condition2 || condition3
81-
|| condition4
80+
myLabel: while
81+
condition1 && condition2
82+
|| condition3
83+
|| condition4
8284
{
8385
let a = 123
8486
var b = "abc"
@@ -88,4 +90,49 @@ final class WhileStmtTests: PrettyPrintTestCase {
8890

8991
assertPrettyPrintEqual(input: input, expected: expected, linelength: 29)
9092
}
93+
94+
func testWhileLoopMultipleConditionElements() {
95+
let input =
96+
"""
97+
while x >= 0 && y >= 0 && x < foo && y < bar, let object = foo.value(at: y), let otherObject = foo.value(at: x), isEqual(a, b) {
98+
foo()
99+
}
100+
while x >= 0 && y >= 0
101+
&& x < foo && y < bar,
102+
let object =
103+
foo.value(at: y),
104+
let otherObject = foo.value(at: x), isEqual(a, b) {
105+
foo()
106+
}
107+
"""
108+
109+
let expected =
110+
"""
111+
while
112+
x >= 0 && y >= 0 && x < foo
113+
&& y < bar,
114+
let object = foo.value(
115+
at: y),
116+
let otherObject =
117+
foo.value(at: x),
118+
isEqual(a, b)
119+
{
120+
foo()
121+
}
122+
while
123+
x >= 0 && y >= 0
124+
&& x < foo && y < bar,
125+
let object =
126+
foo.value(at: y),
127+
let otherObject =
128+
foo.value(at: x),
129+
isEqual(a, b)
130+
{
131+
foo()
132+
}
133+
134+
"""
135+
136+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 29)
137+
}
91138
}

Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ extension NewlineTests {
493493
static let __allTests__NewlineTests = [
494494
("testLeadingNewlines", testLeadingNewlines),
495495
("testLeadingNewlinesWithComments", testLeadingNewlinesWithComments),
496+
("testNewlinesBetweenMembers", testNewlinesBetweenMembers),
496497
("testTrailingNewlines", testTrailingNewlines),
497498
("testTrailingNewlinesWithComments", testTrailingNewlinesWithComments),
498499
]
@@ -802,6 +803,7 @@ extension WhileStmtTests {
802803
static let __allTests__WhileStmtTests = [
803804
("testBasicWhileLoops", testBasicWhileLoops),
804805
("testLabeledWhileLoops", testLabeledWhileLoops),
806+
("testWhileLoopMultipleConditionElements", testWhileLoopMultipleConditionElements),
805807
]
806808
}
807809

0 commit comments

Comments
 (0)