Skip to content

Commit eb3e2fd

Browse files
committed
Fix initial indent for do and repeat.
1 parent 6ef9ad8 commit eb3e2fd

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,10 @@ private final class TokenStreamCreator: SyntaxVisitor {
433433
}
434434

435435
override func visit(_ node: RepeatWhileStmtSyntax) {
436-
before(node.repeatKeyword, tokens: .reset, .open(.consistent, 0), .open(.inconsistent, 0))
436+
// The zero-width space prevents the consistent group here from immediately following a break,
437+
// which would cause all of the breaks immediately inside the group to fire.
438+
before(
439+
node.repeatKeyword, tokens: .space(size: 0), .open(.consistent, 0), .open(.inconsistent, 0))
437440
after(node.repeatKeyword, tokens: .space)
438441

439442
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
@@ -448,7 +451,9 @@ private final class TokenStreamCreator: SyntaxVisitor {
448451
}
449452

450453
override func visit(_ node: DoStmtSyntax) {
451-
before(node.doKeyword, tokens: .reset, .open(.consistent, 0), .open(.inconsistent, 0))
454+
// The zero-width space prevents the consistent group here from immediately following a break,
455+
// which would cause all of the breaks immediately inside the group to fire.
456+
before(node.doKeyword, tokens: .space(size: 0), .open(.consistent, 0), .open(.inconsistent, 0))
452457
after(node.doKeyword, tokens: .space)
453458

454459
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)

Tests/SwiftFormatPrettyPrintTests/RepeatStmtTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,19 @@ public class RepeatStmtTests: PrettyPrintTestCase {
4545

4646
assertPrettyPrintEqual(input: input, expected: expected, linelength: 25)
4747
}
48+
49+
public func testNestedRepeat() {
50+
// Avoid regressions in the case where a nested `repeat` block was getting shifted all the way
51+
// left.
52+
let input = """
53+
func foo() {
54+
repeat {
55+
bar()
56+
baz()
57+
}
58+
while condition
59+
}
60+
"""
61+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 45)
62+
}
4863
}

Tests/SwiftFormatPrettyPrintTests/TryCatchTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,17 @@ public class TryCatchTests: PrettyPrintTestCase {
9393

9494
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
9595
}
96+
97+
public func testNestedDo() {
98+
// Avoid regressions in the case where a nested `do` block was getting shifted all the way left.
99+
let input = """
100+
func foo() {
101+
do {
102+
bar()
103+
baz()
104+
}
105+
}
106+
"""
107+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 45)
108+
}
96109
}

0 commit comments

Comments
 (0)