@@ -69,16 +69,14 @@ public class PrettyPrinter {
6969 private var tokens : [ Token ]
7070 private var source : String
7171
72- /// keep track of where formatting was disabled in the original source
72+ /// Keep track of where formatting was disabled in the original source
7373 ///
7474 /// To format a selection, we insert `enableFormatting`/`disableFormatting` tokens into the
7575 /// stream when entering/exiting a selection range. Those tokens include utf8 offsets into the
7676 /// original source. When enabling formatting, we copy the text between `disabledPosition` and the
7777 /// current position to `outputBuffer`. From then on, we continue to format until the next
7878 /// `disableFormatting` token.
7979 private var disabledPosition : AbsolutePosition ? = nil
80- /// true if we're currently formatting
81- private var writingIsEnabled : Bool { disabledPosition == nil }
8280
8381 private var outputBuffer : String = " "
8482
@@ -204,7 +202,9 @@ public class PrettyPrinter {
204202 ///
205203 /// No further processing is performed on the string.
206204 private func writeRaw< S: StringProtocol > ( _ str: S ) {
207- outputBuffer. append ( String ( str) )
205+ if disabledPosition == nil {
206+ outputBuffer. append ( String ( str) )
207+ }
208208 }
209209
210210 /// Writes newlines into the output stream, taking into account any preexisting consecutive
@@ -233,9 +233,7 @@ public class PrettyPrinter {
233233 }
234234
235235 guard numberToPrint > 0 else { return }
236- if writingIsEnabled {
237- writeRaw ( String ( repeating: " \n " , count: numberToPrint) )
238- }
236+ writeRaw ( String ( repeating: " \n " , count: numberToPrint) )
239237 lineNumber += numberToPrint
240238 isAtStartOfLine = true
241239 consecutiveNewlineCount += numberToPrint
@@ -257,17 +255,13 @@ public class PrettyPrinter {
257255 /// leading spaces that are required before the text itself.
258256 private func write( _ text: String ) {
259257 if isAtStartOfLine {
260- if writingIsEnabled {
261- writeRaw ( currentIndentation. indentation ( ) )
262- }
258+ writeRaw ( currentIndentation. indentation ( ) )
263259 spaceRemaining = maxLineLength - currentIndentation. length ( in: configuration)
264260 isAtStartOfLine = false
265- } else if pendingSpaces > 0 && writingIsEnabled {
261+ } else if pendingSpaces > 0 {
266262 writeRaw ( String ( repeating: " " , count: pendingSpaces) )
267263 }
268- if writingIsEnabled {
269- writeRaw ( text)
270- }
264+ writeRaw ( text)
271265 consecutiveNewlineCount = 0
272266 pendingSpaces = 0
273267 }
@@ -546,9 +540,7 @@ public class PrettyPrinter {
546540 }
547541
548542 case . verbatim( let verbatim) :
549- if writingIsEnabled {
550- writeRaw ( verbatim. print ( indent: currentIndentation) )
551- }
543+ writeRaw ( verbatim. print ( indent: currentIndentation) )
552544 consecutiveNewlineCount = 0
553545 pendingSpaces = 0
554546 lastBreak = false
@@ -596,38 +588,37 @@ public class PrettyPrinter {
596588 }
597589
598590 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+ }
614608
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
624617 }
625618
626619 case . disableFormatting( let newPosition) :
627- // a second disable is ignored
628- if writingIsEnabled {
629- disabledPosition = newPosition
630- }
620+ assert ( disabledPosition == nil )
621+ disabledPosition = newPosition
631622 }
632623 }
633624
0 commit comments