@@ -234,7 +234,7 @@ public class PrettyPrinter {
234
234
// the group.
235
235
case . open( let breaktype) :
236
236
// Determine if the break tokens in this group need to be forced.
237
- if ( shouldBreak ( length) || lastBreak) , case . consistent = breaktype {
237
+ if ( !canFit ( length) || lastBreak) , case . consistent = breaktype {
238
238
forceBreakStack. append ( true )
239
239
} else {
240
240
forceBreakStack. append ( false )
@@ -276,7 +276,7 @@ public class PrettyPrinter {
276
276
// scope), so we need the continuation indentation to persist across all the lines in that
277
277
// scope. Additionally, continuation open breaks must indent when the break fires.
278
278
let continuationBreakWillFire = openKind == . continuation
279
- && ( outputBuffer. isAtStartOfLine || shouldBreak ( length) || mustBreak)
279
+ && ( outputBuffer. isAtStartOfLine || !canFit ( length) || mustBreak)
280
280
let contributesContinuationIndent = currentLineIsContinuation || continuationBreakWillFire
281
281
282
282
activeOpenBreaks. append (
@@ -323,7 +323,7 @@ public class PrettyPrinter {
323
323
// If it's a mandatory breaking close, then we must break (regardless of line length) if
324
324
// the break is on a different line than its corresponding open break.
325
325
mustBreak = openedOnDifferentLine
326
- } else if shouldBreak ( 1 ) {
326
+ } else if !canFit ( ) {
327
327
// If there is no room left on the line, then we must force this break to fire so that the
328
328
// next token that comes along (typically a closing bracket of some kind) ends up on the
329
329
// next line.
@@ -387,7 +387,7 @@ public class PrettyPrinter {
387
387
388
388
// Wait for a contextual break to fire and then update the breaking behavior for the rest of
389
389
// the contextual breaks in this scope to match the behavior of the one that fired.
390
- let willFire = shouldBreak ( length) || mustBreak
390
+ let willFire = !canFit ( length) || mustBreak
391
391
if willFire {
392
392
// Update the active breaking context according to the most recently finished breaking
393
393
// context so all following contextual breaks in this scope to have matching behavior.
@@ -427,7 +427,7 @@ public class PrettyPrinter {
427
427
}
428
428
429
429
let suppressBreaking = isBreakingSuppressed && !overrideBreakingSuppressed
430
- if !suppressBreaking && ( shouldBreak ( length) || mustBreak) {
430
+ if !suppressBreaking && ( !canFit ( length) || mustBreak) {
431
431
currentLineIsContinuation = isContinuationIfBreakFires
432
432
outputBuffer. writeNewlines ( newline)
433
433
lastBreak = true
@@ -458,7 +458,7 @@ public class PrettyPrinter {
458
458
lastBreak = false
459
459
460
460
if wasEndOfLine {
461
- if shouldBreak ( comment. length) && ! isBreakingSuppressed {
461
+ if ! ( canFit ( comment. length) || isBreakingSuppressed) {
462
462
diagnose ( . moveEndOfLineComment, category: . endOfLineComment)
463
463
}
464
464
}
@@ -536,9 +536,11 @@ public class PrettyPrinter {
536
536
}
537
537
}
538
538
539
- private func shouldBreak( _ length: Int ) -> Bool {
539
+ /// Indicates whether the current line can fit a string of the given length. If no length
540
+ /// is given, it indicates whether the current line can accomodate *any* text.
541
+ private func canFit( _ length: Int = 1 ) -> Bool {
540
542
let spaceRemaining = configuration. lineLength - outputBuffer. column
541
- return ! outputBuffer. isAtStartOfLine && length > spaceRemaining
543
+ return outputBuffer. isAtStartOfLine || length <= spaceRemaining
542
544
}
543
545
544
546
/// Scan over the array of Tokens and calculate their lengths.
0 commit comments