Skip to content

Commit 5402fba

Browse files
committed
Do not include trivia in the hover range
We only want to send the token itself, not its surrounding trivia (which would include eg. any beginning newline/whitespace).
1 parent 5df8f3d commit 5402fba

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Sources/SourceKitLSP/Swift/SwiftLanguageService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ extension SwiftLanguageService {
786786
if let snapshot = try? await latestSnapshot(for: uri) {
787787
let tree = await syntaxTreeManager.syntaxTree(for: snapshot)
788788
if let token = tree.token(at: snapshot.absolutePosition(of: position)) {
789-
tokenRange = snapshot.absolutePositionRange(of: token.position..<token.endPosition)
789+
tokenRange = snapshot.absolutePositionRange(of: token.trimmedRange)
790790
}
791791
}
792792

Tests/SourceKitLSPTests/HoverTests.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class HoverTests: XCTestCase {
3232
3333
Details.
3434
""",
35-
expectedRange: Position(line: 3, utf16index: 7)..<Position(line: 3, utf16index: 9)
35+
expectedRange: Position(line: 3, utf16index: 7)..<Position(line: 3, utf16index: 8)
3636
)
3737
}
3838

@@ -173,6 +173,24 @@ final class HoverTests: XCTestCase {
173173
expectedRange: Position(line: 3, utf16index: 5)..<Position(line: 3, utf16index: 13)
174174
)
175175
}
176+
177+
func testTrivia() async throws {
178+
try await assertHover(
179+
"""
180+
func foo() {}
181+
func bar() {
182+
/*some comment*/1️⃣foo/*more comment*/()
183+
}
184+
""",
185+
expectedContent: """
186+
```swift
187+
func foo()
188+
```
189+
190+
""",
191+
expectedRange: Position(line: 2, utf16index: 18)..<Position(line: 2, utf16index: 21)
192+
)
193+
}
176194
}
177195

178196
private func assertHover(

0 commit comments

Comments
 (0)