Skip to content

Commit 415b8ea

Browse files
committed
Add missing comments for new struct and its properties.
1 parent a8ff3ca commit 415b8ea

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public class PrettyPrinter {
234234
// the group.
235235
case .open(let breaktype):
236236
// 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 {
238238
forceBreakStack.append(true)
239239
} else {
240240
forceBreakStack.append(false)
@@ -276,7 +276,7 @@ public class PrettyPrinter {
276276
// scope), so we need the continuation indentation to persist across all the lines in that
277277
// scope. Additionally, continuation open breaks must indent when the break fires.
278278
let continuationBreakWillFire = openKind == .continuation
279-
&& (outputBuffer.isAtStartOfLine || shouldBreak(length) || mustBreak)
279+
&& (outputBuffer.isAtStartOfLine || !canFit(length) || mustBreak)
280280
let contributesContinuationIndent = currentLineIsContinuation || continuationBreakWillFire
281281

282282
activeOpenBreaks.append(
@@ -323,7 +323,7 @@ public class PrettyPrinter {
323323
// If it's a mandatory breaking close, then we must break (regardless of line length) if
324324
// the break is on a different line than its corresponding open break.
325325
mustBreak = openedOnDifferentLine
326-
} else if shouldBreak(1) {
326+
} else if !canFit() {
327327
// If there is no room left on the line, then we must force this break to fire so that the
328328
// next token that comes along (typically a closing bracket of some kind) ends up on the
329329
// next line.
@@ -387,7 +387,7 @@ public class PrettyPrinter {
387387

388388
// Wait for a contextual break to fire and then update the breaking behavior for the rest of
389389
// 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
391391
if willFire {
392392
// Update the active breaking context according to the most recently finished breaking
393393
// context so all following contextual breaks in this scope to have matching behavior.
@@ -427,7 +427,7 @@ public class PrettyPrinter {
427427
}
428428

429429
let suppressBreaking = isBreakingSuppressed && !overrideBreakingSuppressed
430-
if !suppressBreaking && (shouldBreak(length) || mustBreak) {
430+
if !suppressBreaking && (!canFit(length) || mustBreak) {
431431
currentLineIsContinuation = isContinuationIfBreakFires
432432
outputBuffer.writeNewlines(newline)
433433
lastBreak = true
@@ -458,7 +458,7 @@ public class PrettyPrinter {
458458
lastBreak = false
459459

460460
if wasEndOfLine {
461-
if shouldBreak(comment.length) && !isBreakingSuppressed {
461+
if !(canFit(comment.length) || isBreakingSuppressed) {
462462
diagnose(.moveEndOfLineComment, category: .endOfLineComment)
463463
}
464464
}
@@ -536,9 +536,11 @@ public class PrettyPrinter {
536536
}
537537
}
538538

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 {
540542
let spaceRemaining = configuration.lineLength - outputBuffer.column
541-
return !outputBuffer.isAtStartOfLine && length > spaceRemaining
543+
return outputBuffer.isAtStartOfLine || length <= spaceRemaining
542544
}
543545

544546
/// Scan over the array of Tokens and calculate their lengths.

Sources/SwiftFormat/PrettyPrint/PrettyPrintBuffer.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@
1212

1313
import Foundation
1414

15+
/// Used by the PrettyPrint class to actually assemble the output string. This struct
16+
/// tracks state specific to the output (line number, column, etc.) rather than the pretty
17+
/// printing algorithm itself.
1518
struct PrettyPrintBuffer {
19+
/// The maximum number of consecutive blank lines that may appear in a file.
1620
let maximumBlankLines: Int
21+
22+
/// The width of the horizontal tab in spaces.
1723
let tabWidth: Int
1824

25+
/// If true, output is generated as normal. If false, the various state variables are
26+
/// updated as normal but nothing is appended to the output (used by selection formatting).
1927
var isEnabled: Bool = true
2028

2129
/// Indicates whether or not the printer is currently at the beginning of a line.
@@ -37,8 +45,10 @@ struct PrettyPrintBuffer {
3745
/// will still point to the position of the previous line.
3846
private(set) var column: Int
3947

48+
/// The current indentation level to be used when text is appended to a new line.
4049
var currentIndentation: [Indent]
4150

51+
/// The accumulated output of the pretty printer.
4252
private(set) var output: String = ""
4353

4454
init(maximumBlankLines: Int, tabWidth: Int, column: Int = 0) {

0 commit comments

Comments
 (0)