Skip to content

Commit 1af4c17

Browse files
committed
PrettyPrinter reports wrong line LineNumbersTests
The reason for the wrong line number were multiline comments. In to accomodate for this, we now check the string while writing for new lines and increment the line count accordingly. Issue: #882
1 parent 5a0ac33 commit 1af4c17

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

Sources/SwiftFormat/PrettyPrint/PrettyPrintBuffer.swift

+14-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,20 @@ struct PrettyPrintBuffer {
118118
writeRaw(text)
119119
consecutiveNewlineCount = 0
120120
pendingSpaces = 0
121-
column += text.count
121+
122+
// In case of comments, we may get a multi-line string.
123+
// To account for that case, we need to correct the lineNumber count.
124+
// The new column is only the position within the last line.
125+
let lines = text.split(separator: "\n")
126+
lineNumber += lines.count - 1
127+
if lines.count > 1 {
128+
// in case we have inserted new lines, we need to reset the column
129+
column = lines.last?.count ?? 0
130+
} else {
131+
// in case it is an end of line comment or a single line comment,
132+
// we just add to the current column
133+
column += lines.last?.count ?? 0
134+
}
122135
}
123136

124137
/// Request that the given number of spaces be printed out before the next text token.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import SwiftFormat
2+
import _SwiftFormatTestSupport
3+
4+
final class LineNumbersTests: PrettyPrintTestCase {
5+
func testLineNumbers() {
6+
let input =
7+
"""
8+
final class A {
9+
@Test func b() throws {
10+
doSomethingInAFunctionWithAVeryLongName() 1️⃣// Here we have a very long comment that should not be here because it is far too long
11+
}
12+
}
13+
"""
14+
15+
let expected =
16+
"""
17+
final class A {
18+
@Test func b() throws {
19+
doSomethingInAFunctionWithAVeryLongName() // Here we have a very long comment that should not be here because it is far too long
20+
}
21+
}
22+
23+
"""
24+
25+
assertPrettyPrintEqual(
26+
input: input,
27+
expected: expected,
28+
linelength: 120,
29+
whitespaceOnly: true,
30+
findings: [
31+
FindingSpec("1️⃣", message: "move end-of-line comment that exceeds the line length")
32+
]
33+
)
34+
}
35+
36+
func testLineNumbersWithComments() {
37+
let input =
38+
"""
39+
// Copyright (C) 2024 My Coorp. All rights reserved.
40+
//
41+
// This document is the property of My Coorp.
42+
// It is considered confidential and proprietary.
43+
//
44+
// This document may not be reproduced or transmitted in any form,
45+
// in whole or in part, without the express written permission of
46+
// My Coorp.
47+
48+
final class A {
49+
@Test func b() throws {
50+
doSomethingInAFunctionWithAVeryLongName() 1️⃣// Here we have a very long comment that should not be here because it is far too long
51+
}
52+
}
53+
"""
54+
55+
let expected =
56+
"""
57+
// Copyright (C) 2024 My Coorp. All rights reserved.
58+
//
59+
// This document is the property of My Coorp.
60+
// It is considered confidential and proprietary.
61+
//
62+
// This document may not be reproduced or transmitted in any form,
63+
// in whole or in part, without the express written permission of
64+
// My Coorp.
65+
66+
final class A {
67+
@Test func b() throws {
68+
doSomethingInAFunctionWithAVeryLongName() // Here we have a very long comment that should not be here because it is far too long
69+
}
70+
}
71+
72+
"""
73+
74+
assertPrettyPrintEqual(
75+
input: input,
76+
expected: expected,
77+
linelength: 120,
78+
whitespaceOnly: true,
79+
findings: [
80+
FindingSpec("1️⃣", message: "move end-of-line comment that exceeds the line length")
81+
]
82+
)
83+
}
84+
}

0 commit comments

Comments
 (0)