Skip to content

Commit 6470019

Browse files
authored
Merge pull request swiftlang#123 from dabelknap/fix-indents
Fix issue where multiple nested groups could have runaway indentation
2 parents b91218c + 5f097a2 commit 6470019

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

Diff for: Sources/PrettyPrint/PrettyPrint.swift

+16-7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public class PrettyPrinter {
5959
/// Keep track of the indentation level of the current group as the number of blank spaces.
6060
private var indentStack = [0]
6161

62+
/// Keep track of the offset value passed to a group. This is used to adjust the offset value of
63+
/// any trailing break statements in the group.
64+
private var relativeIndentStack = [0]
65+
6266
/// Keep track of whether we are forcing breaks within a group (for consistent breaking).
6367
private var forceBreakStack = [false]
6468

@@ -126,14 +130,16 @@ public class PrettyPrinter {
126130
// incremented from the outer group's indent.
127131
let indentValue = indentStack.last ?? 0
128132
indentStack.append(indentValue + offset + lastBreakOffset)
133+
relativeIndentStack.append(offset + lastBreakOffset)
129134
lastBreakOffset = 0
130135

131136
case .close:
132137
if isDebugMode {
133138
writeCloseGroupDebugMarker()
134139
}
135140
forceBreakStack.removeLast()
136-
let indentValue = indentStack.popLast() ?? 0
141+
indentStack.removeLast()
142+
let indentValue = relativeIndentStack.popLast() ?? 0
137143
// The offset of the last break needs to be adjusted according to its parent group. This is so
138144
// the next open token's indent is initialized with the correct value.
139145
lastBreakOffset += indentValue
@@ -176,13 +182,16 @@ public class PrettyPrinter {
176182

177183
// Print out the number of spaces according to the size, and adjust spaceRemaining.
178184
case .space(let size):
179-
spaceRemaining -= size
180-
writeSpaces(size + lastBreakValue)
185+
if lastBreakConsecutive {
186+
writeSpaces(lastBreakValue)
181187

182-
lastBreak = false
183-
lastBreakConsecutive = false
184-
lastBreakOffset = 0
185-
lastBreakValue = 0
188+
lastBreak = false
189+
lastBreakConsecutive = false
190+
lastBreakOffset = 0
191+
lastBreakValue = 0
192+
}
193+
spaceRemaining -= size
194+
writeSpaces(size)
186195

187196
// Apply N line breaks, calculate the indentation required, and adjust spaceRemaining.
188197
case .newlines(let N, let offset):

Diff for: Sources/PrettyPrint/TokenStreamCreator.swift

+1
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
463463
}
464464

465465
override func visit(_ node: BreakStmtSyntax) {
466+
before(node.label, tokens: .break(offset: 2))
466467
super.visit(node)
467468
}
468469

Diff for: Tests/PrettyPrinterTests/SwitchStmtTests.swift

+35
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,39 @@ public class SwitchStmtTests: PrettyPrintTestCase {
7474

7575
assertPrettyPrintEqual(input: input, expected: expected, linelength: 35)
7676
}
77+
78+
public func testNestedSwitch() {
79+
let input =
80+
"""
81+
myloop: while a != b {
82+
switch a + b {
83+
case firstValue:
84+
break myloop
85+
case secondVale:
86+
let c = 123
87+
var d = 456
88+
default:
89+
a += b
90+
}
91+
}
92+
"""
93+
94+
let expected =
95+
"""
96+
myloop: while a != b {
97+
switch a + b {
98+
case firstValue:
99+
break myloop
100+
case secondVale:
101+
let c = 123
102+
var d = 456
103+
default:
104+
a += b
105+
}
106+
}
107+
108+
"""
109+
110+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 35)
111+
}
77112
}

0 commit comments

Comments
 (0)