@@ -129,11 +129,16 @@ public class PrettyPrinter {
129
129
private var activeBreakSuppressionCount = 0
130
130
131
131
/// Whether breaks are supressed from firing. When true, no breaks should fire and the only way to
132
- /// move to a new line is an explicit new line token.
133
- private var isBreakingSupressed : Bool {
132
+ /// move to a new line is an explicit new line token. Discretionary breaks aren't suppressed
133
+ /// if ``allowSuppressedDiscretionaryBreaks`` is true.
134
+ private var isBreakingSuppressed : Bool {
134
135
return activeBreakSuppressionCount > 0
135
136
}
136
137
138
+ /// Indicates whether discretionary breaks should still be included even if break suppression is
139
+ /// enabled (see ``isBreakingSuppressed``).
140
+ private var allowSuppressedDiscretionaryBreaks = false
141
+
137
142
/// The computed indentation level, as a number of spaces, based on the state of any unclosed
138
143
/// delimiters and whether or not the current line is a continuation line.
139
144
private var currentIndentation : [ Indent ] {
@@ -469,15 +474,15 @@ public class PrettyPrinter {
469
474
case . soft( _, let discretionary) :
470
475
// A discretionary newline (i.e. from the source) should create a line break even if the
471
476
// rules for breaking are disabled.
472
- overrideBreakingSuppressed = discretionary
477
+ overrideBreakingSuppressed = discretionary && allowSuppressedDiscretionaryBreaks
473
478
mustBreak = true
474
479
case . hard:
475
480
// A hard newline must always create a line break, regardless of the context.
476
481
overrideBreakingSuppressed = true
477
482
mustBreak = true
478
483
}
479
484
480
- let suppressBreaking = isBreakingSupressed && !overrideBreakingSuppressed
485
+ let suppressBreaking = isBreakingSuppressed && !overrideBreakingSuppressed
481
486
if !suppressBreaking && ( ( !isAtStartOfLine && length > spaceRemaining) || mustBreak) {
482
487
currentLineIsContinuation = isContinuationIfBreakFires
483
488
writeNewlines ( newline)
@@ -527,8 +532,14 @@ public class PrettyPrinter {
527
532
528
533
case . printerControl( let kind) :
529
534
switch kind {
530
- case . disableBreaking:
535
+ case . disableBreaking( let allowDiscretionary ) :
531
536
activeBreakSuppressionCount += 1
537
+ // Override the supression of discretionary breaks if we're at the top level or
538
+ // discretionary breaks are currently allowed (false should override true, but not the other
539
+ // way around).
540
+ if activeBreakSuppressionCount == 1 || allowSuppressedDiscretionaryBreaks {
541
+ allowSuppressedDiscretionaryBreaks = allowDiscretionary
542
+ }
532
543
case . enableBreaking:
533
544
activeBreakSuppressionCount -= 1
534
545
}
0 commit comments