Skip to content

Commit

Permalink
Merge pull request #895 from rauhul/rauhul/fix-end-of-line-off-by-1
Browse files Browse the repository at this point in the history
Fix comment length computation
  • Loading branch information
allevato authored Dec 11, 2024
2 parents ed01028 + a652b9d commit e658606
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/SwiftFormat/PrettyPrint/Comment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ struct Comment {

switch kind {
case .line, .docLine:
self.length = text.count
self.text = [text]
self.text[0].removeFirst(kind.prefixLength)
self.length = self.text.reduce(0, { $0 + $1.count + kind.prefixLength + 1 })

case .block, .docBlock:
var fulltext: String = text
Expand Down
23 changes: 23 additions & 0 deletions Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,29 @@ final class CommentTests: PrettyPrintTestCase {
)
}

// Tests that "end of line" comments are flagged only when they exceed the configured line length.
func testDiagnoseMoveEndOfLineCommentAroundBoundary() {
assertPrettyPrintEqual(
input: """
x // 789
x // 7890
x 1️⃣// 78901
""",
expected: """
x // 789
x // 7890
x // 78901
""",
linelength: 10,
whitespaceOnly: true,
findings: [
FindingSpec("1️⃣", message: "move end-of-line comment that exceeds the line length")
]
)
}

func testLineWithDocLineComment() {
// none of these should be merged if/when there is comment formatting
let input =
Expand Down

0 comments on commit e658606

Please sign in to comment.