Skip to content

Commit 872e303

Browse files
committed
Fix comment length computation
Resolved an issue where "end of line" comments matching the configured line length were incorrectly flagged as exceeding it.
1 parent ed01028 commit 872e303

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

Sources/SwiftFormat/PrettyPrint/Comment.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ struct Comment {
6767

6868
switch kind {
6969
case .line, .docLine:
70+
self.length = text.count
7071
self.text = [text]
7172
self.text[0].removeFirst(kind.prefixLength)
72-
self.length = self.text.reduce(0, { $0 + $1.count + kind.prefixLength + 1 })
7373

7474
case .block, .docBlock:
7575
var fulltext: String = text

Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,9 @@ public class PrettyPrinter {
558558
/// Indicates whether the current line can fit a string of the given length. If no length
559559
/// is given, it indicates whether the current line can accomodate *any* text.
560560
private func canFit(_ length: Int = 1) -> Bool {
561+
// print("\(length)")
561562
let spaceRemaining = configuration.lineLength - outputBuffer.column
563+
// print("let \(spaceRemaining) = \(configuration.lineLength) - \(outputBuffer.column)")
562564
return outputBuffer.isAtStartOfLine || length <= spaceRemaining
563565
}
564566

Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,8 @@ final class CommentTests: PrettyPrintTestCase {
985985
assertPrettyPrintEqual(
986986
input: """
987987
import veryveryverylongmodulenameherebecauseitistypical // special sentinel comment
988-
988+
989+
let x // this comment should line up with 45
989990
func fooBarBazRunningOutOfIdeas() { 1️⃣// comment that needs to move
990991
if foo { // comment is fine
991992
}
@@ -995,6 +996,7 @@ final class CommentTests: PrettyPrintTestCase {
995996
expected: """
996997
import veryveryverylongmodulenameherebecauseitistypical // special sentinel comment
997998
999+
let x // this comment should line up with 45
9981000
func fooBarBazRunningOutOfIdeas() { // comment that needs to move
9991001
if foo { // comment is fine
10001002
}

0 commit comments

Comments
 (0)