@@ -129,11 +129,16 @@ public class PrettyPrinter {
129129 private var activeBreakSuppressionCount = 0
130130
131131 /// 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 {
134135 return activeBreakSuppressionCount > 0
135136 }
136137
138+ /// Indicates whether discretionary breaks should still be included even if break suppression is
139+ /// enabled (see ``isBreakingSuppressed``).
140+ private var allowSuppressedDiscretionaryBreaks = false
141+
137142 /// The computed indentation level, as a number of spaces, based on the state of any unclosed
138143 /// delimiters and whether or not the current line is a continuation line.
139144 private var currentIndentation : [ Indent ] {
@@ -469,15 +474,15 @@ public class PrettyPrinter {
469474 case . soft( _, let discretionary) :
470475 // A discretionary newline (i.e. from the source) should create a line break even if the
471476 // rules for breaking are disabled.
472- overrideBreakingSuppressed = discretionary
477+ overrideBreakingSuppressed = discretionary && allowSuppressedDiscretionaryBreaks
473478 mustBreak = true
474479 case . hard:
475480 // A hard newline must always create a line break, regardless of the context.
476481 overrideBreakingSuppressed = true
477482 mustBreak = true
478483 }
479484
480- let suppressBreaking = isBreakingSupressed && !overrideBreakingSuppressed
485+ let suppressBreaking = isBreakingSuppressed && !overrideBreakingSuppressed
481486 if !suppressBreaking && ( ( !isAtStartOfLine && length > spaceRemaining) || mustBreak) {
482487 currentLineIsContinuation = isContinuationIfBreakFires
483488 writeNewlines ( newline)
@@ -527,8 +532,14 @@ public class PrettyPrinter {
527532
528533 case . printerControl( let kind) :
529534 switch kind {
530- case . disableBreaking:
535+ case . disableBreaking( let allowDiscretionary ) :
531536 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+ }
532543 case . enableBreaking:
533544 activeBreakSuppressionCount -= 1
534545 }
0 commit comments