11
11
//===----------------------------------------------------------------------===//
12
12
13
13
import LanguageServerProtocol
14
+ import SKLogging
14
15
import SourceKitD
15
16
16
17
/// Detailed information about a symbol under the cursor.
@@ -57,6 +58,7 @@ struct CursorInfo {
57
58
58
59
init ? (
59
60
_ dict: SKDResponseDictionary ,
61
+ documentManager: DocumentManager ,
60
62
sourcekitd: some SourceKitD
61
63
) {
62
64
let keys = sourcekitd. keys
@@ -70,12 +72,14 @@ struct CursorInfo {
70
72
let line: Int = dict [ keys. line] ,
71
73
let column: Int = dict [ keys. column]
72
74
{
73
- let position = Position (
74
- line: line - 1 ,
75
- // FIXME: we need to convert the utf8/utf16 column, which may require reading the file!
76
- utf16index: column - 1
77
- )
78
- location = Location ( uri: DocumentURI ( filePath: filepath, isDirectory: false ) , range: Range ( position) )
75
+ let uri = DocumentURI ( filePath: filepath, isDirectory: false )
76
+ if let snapshot = documentManager. latestSnapshotOrDisk ( uri, language: . swift) {
77
+ let position = snapshot. positionOf ( zeroBasedLine: line - 1 , utf8Column: column - 1 )
78
+ location = Location ( uri: uri, range: Range ( position) )
79
+ } else {
80
+ logger. error ( " Failed to get snapshot for \( uri. forLogging) to convert position " )
81
+ location = nil
82
+ }
79
83
} else {
80
84
location = nil
81
85
}
@@ -142,6 +146,7 @@ extension SwiftLanguageService {
142
146
_ range: Range < Position > ,
143
147
additionalParameters appendAdditionalParameters: ( ( SKDRequestDictionary ) -> Void ) ? = nil
144
148
) async throws -> ( cursorInfo: [ CursorInfo ] , refactorActions: [ SemanticRefactorCommand ] ) {
149
+ let documentManager = try self . documentManager
145
150
let snapshot = try documentManager. latestSnapshot ( uri)
146
151
147
152
let offsetRange = snapshot. utf8OffsetRange ( of: range)
@@ -162,10 +167,12 @@ extension SwiftLanguageService {
162
167
let dict = try await sendSourcekitdRequest ( skreq, fileContents: snapshot. text)
163
168
164
169
var cursorInfoResults : [ CursorInfo ] = [ ]
165
- if let cursorInfo = CursorInfo ( dict, sourcekitd: sourcekitd) {
170
+ if let cursorInfo = CursorInfo ( dict, documentManager : documentManager , sourcekitd: sourcekitd) {
166
171
cursorInfoResults. append ( cursorInfo)
167
172
}
168
- cursorInfoResults += dict [ keys. secondarySymbols] ? . compactMap { CursorInfo ( $0, sourcekitd: sourcekitd) } ?? [ ]
173
+ cursorInfoResults +=
174
+ dict [ keys. secondarySymbols] ?
175
+ . compactMap { CursorInfo ( $0, documentManager: documentManager, sourcekitd: sourcekitd) } ?? [ ]
169
176
let refactorActions =
170
177
[ SemanticRefactorCommand] (
171
178
array: dict [ keys. refactorActions] ,
0 commit comments