@@ -59,6 +59,10 @@ public class PrettyPrinter {
59
59
/// Keep track of the indentation level of the current group as the number of blank spaces.
60
60
private var indentStack = [ 0 ]
61
61
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
+
62
66
/// Keep track of whether we are forcing breaks within a group (for consistent breaking).
63
67
private var forceBreakStack = [ false ]
64
68
@@ -126,14 +130,16 @@ public class PrettyPrinter {
126
130
// incremented from the outer group's indent.
127
131
let indentValue = indentStack. last ?? 0
128
132
indentStack. append ( indentValue + offset + lastBreakOffset)
133
+ relativeIndentStack. append ( offset + lastBreakOffset)
129
134
lastBreakOffset = 0
130
135
131
136
case . close:
132
137
if isDebugMode {
133
138
writeCloseGroupDebugMarker ( )
134
139
}
135
140
forceBreakStack. removeLast ( )
136
- let indentValue = indentStack. popLast ( ) ?? 0
141
+ indentStack. removeLast ( )
142
+ let indentValue = relativeIndentStack. popLast ( ) ?? 0
137
143
// The offset of the last break needs to be adjusted according to its parent group. This is so
138
144
// the next open token's indent is initialized with the correct value.
139
145
lastBreakOffset += indentValue
@@ -176,13 +182,16 @@ public class PrettyPrinter {
176
182
177
183
// Print out the number of spaces according to the size, and adjust spaceRemaining.
178
184
case . space( let size) :
179
- spaceRemaining -= size
180
- writeSpaces ( size + lastBreakValue)
185
+ if lastBreakConsecutive {
186
+ writeSpaces ( lastBreakValue)
181
187
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)
186
195
187
196
// Apply N line breaks, calculate the indentation required, and adjust spaceRemaining.
188
197
case . newlines( let N, let offset) :
0 commit comments