Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PrettyPrinter reports wrong line LineNumbersTests #883

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Sources/SwiftFormat/PrettyPrint/PrettyPrintBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,20 @@ struct PrettyPrintBuffer {
writeRaw(text)
consecutiveNewlineCount = 0
pendingSpaces = 0
column += text.count

// In case of comments, we may get a multi-line string.
// To account for that case, we need to correct the lineNumber count.
// The new column is only the position within the last line.
let lines = text.split(separator: "\n")
lineNumber += lines.count - 1
if lines.count > 1 {
// in case we have inserted new lines, we need to reset the column
column = lines.last?.count ?? 0
} else {
// in case it is an end of line comment or a single line comment,
// we just add to the current column
column += lines.last?.count ?? 0
}
}

/// Request that the given number of spaces be printed out before the next text token.
Expand Down
84 changes: 84 additions & 0 deletions Tests/SwiftFormatTests/PrettyPrint/LineNumbersTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import SwiftFormat
import _SwiftFormatTestSupport

final class LineNumbersTests: PrettyPrintTestCase {
func testLineNumbers() {
let input =
"""
final class A {
@Test func b() throws {
doSomethingInAFunctionWithAVeryLongName() 1️⃣// Here we have a very long comment that should not be here because it is far too long
}
}
"""

let expected =
"""
final class A {
@Test func b() throws {
doSomethingInAFunctionWithAVeryLongName() // Here we have a very long comment that should not be here because it is far too long
}
}

"""

assertPrettyPrintEqual(
input: input,
expected: expected,
linelength: 120,
whitespaceOnly: true,
findings: [
FindingSpec("1️⃣", message: "move end-of-line comment that exceeds the line length")
]
)
}

func testLineNumbersWithComments() {
let input =
"""
// Copyright (C) 2024 My Coorp. All rights reserved.
//
// This document is the property of My Coorp.
// It is considered confidential and proprietary.
//
// This document may not be reproduced or transmitted in any form,
// in whole or in part, without the express written permission of
// My Coorp.

final class A {
@Test func b() throws {
doSomethingInAFunctionWithAVeryLongName() 1️⃣// Here we have a very long comment that should not be here because it is far too long
}
}
"""

let expected =
"""
// Copyright (C) 2024 My Coorp. All rights reserved.
//
// This document is the property of My Coorp.
// It is considered confidential and proprietary.
//
// This document may not be reproduced or transmitted in any form,
// in whole or in part, without the express written permission of
// My Coorp.

final class A {
@Test func b() throws {
doSomethingInAFunctionWithAVeryLongName() // Here we have a very long comment that should not be here because it is far too long
}
}

"""

assertPrettyPrintEqual(
input: input,
expected: expected,
linelength: 120,
whitespaceOnly: true,
findings: [
FindingSpec("1️⃣", message: "move end-of-line comment that exceeds the line length")
]
)
}
}
Loading