Skip to content

Commit 8ac1bee

Browse files
authored
Merge pull request #1899 from iMostfa/feat/support-range-in-hover
feat: Support range in `textDocument/hover`
2 parents db553c6 + f2978b0 commit 8ac1bee

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

Sources/SKTestSupport/SkipUnless.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,12 @@ package actor SkipUnless {
287287
HoverRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
288288
)
289289
let hover = try XCTUnwrap(response, file: file, line: line)
290-
XCTAssertNil(hover.range, file: file, line: line)
290+
XCTAssertEqual(
291+
hover.range,
292+
Position(line: 1, utf16index: 5)..<Position(line: 1, utf16index: 9),
293+
file: file,
294+
line: line
295+
)
291296
guard case .markupContent(let content) = hover.contents else {
292297
throw ExpectedMarkdownContentsError()
293298
}

Sources/SourceKitLSP/Swift/SwiftLanguageService.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,9 +766,18 @@ extension SwiftLanguageService {
766766
"""
767767
}
768768

769+
var tokenRange: Range<Position>?
770+
771+
if let snapshot = try? await latestSnapshot(for: uri) {
772+
let tree = await syntaxTreeManager.syntaxTree(for: snapshot)
773+
if let token = tree.token(at: snapshot.absolutePosition(of: position)) {
774+
tokenRange = snapshot.absolutePositionRange(of: token.position..<token.endPosition)
775+
}
776+
}
777+
769778
return HoverResponse(
770779
contents: .markupContent(MarkupContent(kind: .markdown, value: joinedDocumentation)),
771-
range: nil
780+
range: tokenRange
772781
)
773782
}
774783

Tests/SourceKitLSPTests/HoverTests.swift

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ final class HoverTests: XCTestCase {
3232
This is a doc comment for S.
3333
3434
Details.
35-
"""
35+
""",
36+
expectedRange: Position(line: 3, utf16index: 7)..<Position(line: 3, utf16index: 9)
3637
)
3738
}
3839

@@ -81,7 +82,9 @@ final class HoverTests: XCTestCase {
8182
init()
8283
```
8384
84-
"""
85+
""",
86+
expectedRange:
87+
.init(line: 3, utf16index: 4) ..< .init(line: 3, utf16index: 7)
8588
)
8689
}
8790

@@ -113,7 +116,8 @@ final class HoverTests: XCTestCase {
113116
```
114117
115118
The initializer
116-
"""
119+
""",
120+
expectedRange: Position(line: 5, utf16index: 4)..<Position(line: 5, utf16index: 7)
117121
)
118122
}
119123

@@ -130,7 +134,8 @@ final class HoverTests: XCTestCase {
130134
```
131135
132136
this is **bold** documentation
133-
"""##
137+
"""##,
138+
expectedRange: Position(line: 1, utf16index: 5)..<Position(line: 1, utf16index: 9)
134139
)
135140
}
136141

@@ -147,7 +152,8 @@ final class HoverTests: XCTestCase {
147152
```
148153
149154
this is *italic* documentation
150-
"""##
155+
"""##,
156+
expectedRange: Position(line: 1, utf16index: 5)..<Position(line: 1, utf16index: 8)
151157
)
152158
}
153159

@@ -168,14 +174,16 @@ final class HoverTests: XCTestCase {
168174
Eat an apple
169175
170176
- Precondition: Must have an apple
171-
"""
177+
""",
178+
expectedRange: Position(line: 3, utf16index: 5)..<Position(line: 3, utf16index: 13)
172179
)
173180
}
174181
}
175182

176183
private func assertHover(
177184
_ markedSource: String,
178185
expectedContent: String,
186+
expectedRange: Range<Position>,
179187
file: StaticString = #filePath,
180188
line: UInt = #line
181189
) async throws {
@@ -189,12 +197,11 @@ private func assertHover(
189197
)
190198

191199
let hover = try XCTUnwrap(response, file: file, line: line)
192-
XCTAssertNil(hover.range, file: file, line: line)
193-
guard case .markupContent(let content) = hover.contents else {
200+
XCTAssertEqual(hover.range, expectedRange, file: file, line: line)
201+
guard case let .markupContent(content) = hover.contents else {
194202
XCTFail("hover.contents is not .markupContents", file: file, line: line)
195203
return
196204
}
197205
XCTAssertEqual(content.kind, .markdown, file: file, line: line)
198206
XCTAssertEqual(content.value, expectedContent, file: file, line: line)
199-
200207
}

0 commit comments

Comments
 (0)