Skip to content

Commit 8b06475

Browse files
author
Harlan Haskins
committed
Properly compute start location after leading trivia
The logic here was flipped, causing nodes at the beginning of a line to display their start location at the end of the previous line.
1 parent 63034c2 commit 8b06475

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

Sources/SwiftSyntax/Syntax.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ extension Syntax {
239239
in file: URL,
240240
afterLeadingTrivia: Bool = true
241241
) -> SourceLocation {
242-
let pos = afterLeadingTrivia ?
243-
data.position :
244-
data.positionAfterSkippingLeadingTrivia
242+
let pos = afterLeadingTrivia ?
243+
data.positionAfterSkippingLeadingTrivia :
244+
data.position
245245
return SourceLocation(file: file.path, position: pos)
246246
}
247247

Tests/SwiftSyntaxTest/AbsolutePosition.swift

+26
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class AbsolutePositionTestCase: XCTestCase {
1919
("testTrivias", testTrivias),
2020
("testImplicit", testImplicit),
2121
("testWithoutSourceFileRoot", testWithoutSourceFileRoot),
22+
("testSourceLocation", testSourceLocation),
2223
]
2324

2425
public func testVisitor() {
@@ -138,4 +139,29 @@ public class AbsolutePositionTestCase: XCTestCase {
138139
XCTAssertEqual(0, item.position.utf8Offset)
139140
XCTAssertEqual(1, item.positionAfterSkippingLeadingTrivia.utf8Offset)
140141
}
142+
143+
public func testSourceLocation() {
144+
let url = URL(fileURLWithPath: "/tmp/test.swift")
145+
let root = self.createSourceFile(2)
146+
guard let secondReturnStmt = root.child(at: 0)?.child(at: 1) else {
147+
fatalError("out of sync with createSourceFile")
148+
}
149+
let startLoc = secondReturnStmt.startLocation(in: url)
150+
XCTAssertEqual(startLoc.line, 4)
151+
XCTAssertEqual(startLoc.column, 18)
152+
153+
let startLocBeforeTrivia =
154+
secondReturnStmt.startLocation(in: url, afterLeadingTrivia: false)
155+
XCTAssertEqual(startLocBeforeTrivia.line, 3)
156+
XCTAssertEqual(startLocBeforeTrivia.column, 1)
157+
158+
let endLoc = secondReturnStmt.endLocation(in: url)
159+
XCTAssertEqual(endLoc.line, 4)
160+
XCTAssertEqual(endLoc.column, 24)
161+
162+
let endLocAfterTrivia =
163+
secondReturnStmt.endLocation(in: url, afterTrailingTrivia: true)
164+
XCTAssertEqual(endLocAfterTrivia.line, 5)
165+
XCTAssertEqual(endLocAfterTrivia.column, 1)
166+
}
141167
}

0 commit comments

Comments
 (0)