@@ -69,16 +69,14 @@ public class PrettyPrinter {
69
69
private var tokens : [ Token ]
70
70
private var source : String
71
71
72
- /// keep track of where formatting was disabled in the original source
72
+ /// Keep track of where formatting was disabled in the original source
73
73
///
74
74
/// To format a selection, we insert `enableFormatting`/`disableFormatting` tokens into the
75
75
/// stream when entering/exiting a selection range. Those tokens include utf8 offsets into the
76
76
/// original source. When enabling formatting, we copy the text between `disabledPosition` and the
77
77
/// current position to `outputBuffer`. From then on, we continue to format until the next
78
78
/// `disableFormatting` token.
79
79
private var disabledPosition : AbsolutePosition ? = nil
80
- /// true if we're currently formatting
81
- private var writingIsEnabled : Bool { disabledPosition == nil }
82
80
83
81
private var outputBuffer : String = " "
84
82
@@ -204,7 +202,9 @@ public class PrettyPrinter {
204
202
///
205
203
/// No further processing is performed on the string.
206
204
private func writeRaw< S: StringProtocol > ( _ str: S ) {
207
- outputBuffer. append ( String ( str) )
205
+ if disabledPosition == nil {
206
+ outputBuffer. append ( String ( str) )
207
+ }
208
208
}
209
209
210
210
/// Writes newlines into the output stream, taking into account any preexisting consecutive
@@ -233,9 +233,7 @@ public class PrettyPrinter {
233
233
}
234
234
235
235
guard numberToPrint > 0 else { return }
236
- if writingIsEnabled {
237
- writeRaw ( String ( repeating: " \n " , count: numberToPrint) )
238
- }
236
+ writeRaw ( String ( repeating: " \n " , count: numberToPrint) )
239
237
lineNumber += numberToPrint
240
238
isAtStartOfLine = true
241
239
consecutiveNewlineCount += numberToPrint
@@ -257,17 +255,13 @@ public class PrettyPrinter {
257
255
/// leading spaces that are required before the text itself.
258
256
private func write( _ text: String ) {
259
257
if isAtStartOfLine {
260
- if writingIsEnabled {
261
- writeRaw ( currentIndentation. indentation ( ) )
262
- }
258
+ writeRaw ( currentIndentation. indentation ( ) )
263
259
spaceRemaining = maxLineLength - currentIndentation. length ( in: configuration)
264
260
isAtStartOfLine = false
265
- } else if pendingSpaces > 0 && writingIsEnabled {
261
+ } else if pendingSpaces > 0 {
266
262
writeRaw ( String ( repeating: " " , count: pendingSpaces) )
267
263
}
268
- if writingIsEnabled {
269
- writeRaw ( text)
270
- }
264
+ writeRaw ( text)
271
265
consecutiveNewlineCount = 0
272
266
pendingSpaces = 0
273
267
}
@@ -546,9 +540,7 @@ public class PrettyPrinter {
546
540
}
547
541
548
542
case . verbatim( let verbatim) :
549
- if writingIsEnabled {
550
- writeRaw ( verbatim. print ( indent: currentIndentation) )
551
- }
543
+ writeRaw ( verbatim. print ( indent: currentIndentation) )
552
544
consecutiveNewlineCount = 0
553
545
pendingSpaces = 0
554
546
lastBreak = false
@@ -596,38 +588,37 @@ public class PrettyPrinter {
596
588
}
597
589
598
590
case . enableFormatting( let enabledPosition) :
599
- // if we're not disabled, we ignore the token
600
- if let disabledPosition {
601
- let start = source. utf8. index ( source. utf8. startIndex, offsetBy: disabledPosition. utf8Offset)
602
- let end : String . Index
603
- if let enabledPosition {
604
- end = source. utf8. index ( source. utf8. startIndex, offsetBy: enabledPosition. utf8Offset)
605
- } else {
606
- end = source. endIndex
607
- }
608
- var text = String ( source [ start..< end] )
609
- // strip trailing whitespace so that the next formatting can add the right amount
610
- if let nonWhitespace = text. rangeOfCharacter (
611
- from: CharacterSet . whitespaces. inverted, options: . backwards) {
612
- text = String ( text [ ..< nonWhitespace. upperBound] )
613
- }
591
+ guard let disabledPosition else {
592
+ // if we're not disabled, we ignore the token
593
+ break
594
+ }
595
+ let start = source. utf8. index ( source. utf8. startIndex, offsetBy: disabledPosition. utf8Offset)
596
+ let end : String . Index
597
+ if let enabledPosition {
598
+ end = source. utf8. index ( source. utf8. startIndex, offsetBy: enabledPosition. utf8Offset)
599
+ } else {
600
+ end = source. endIndex
601
+ }
602
+ var text = String ( source [ start..< end] )
603
+ // strip trailing whitespace so that the next formatting can add the right amount
604
+ if let nonWhitespace = text. rangeOfCharacter (
605
+ from: CharacterSet . whitespaces. inverted, options: . backwards) {
606
+ text = String ( text [ ..< nonWhitespace. upperBound] )
607
+ }
614
608
615
- writeRaw ( text)
616
- if text. hasSuffix ( " \n " ) {
617
- isAtStartOfLine = true
618
- consecutiveNewlineCount = 1
619
- } else {
620
- isAtStartOfLine = false
621
- consecutiveNewlineCount = 0
622
- }
623
- self . disabledPosition = nil
609
+ self . disabledPosition = nil
610
+ writeRaw ( text)
611
+ if text. hasSuffix ( " \n " ) {
612
+ isAtStartOfLine = true
613
+ consecutiveNewlineCount = 1
614
+ } else {
615
+ isAtStartOfLine = false
616
+ consecutiveNewlineCount = 0
624
617
}
625
618
626
619
case . disableFormatting( let newPosition) :
627
- // a second disable is ignored
628
- if writingIsEnabled {
629
- disabledPosition = newPosition
630
- }
620
+ assert ( disabledPosition == nil )
621
+ disabledPosition = newPosition
631
622
}
632
623
}
633
624
0 commit comments