1111//===----------------------------------------------------------------------===//
1212
1313import LanguageServerProtocol
14+ import SKLogging
1415import SourceKitD
1516
1617/// Detailed information about a symbol under the cursor.
@@ -57,6 +58,7 @@ struct CursorInfo {
5758
5859 init ? (
5960 _ dict: SKDResponseDictionary ,
61+ documentManager: DocumentManager ,
6062 sourcekitd: some SourceKitD
6163 ) {
6264 let keys = sourcekitd. keys
@@ -70,12 +72,14 @@ struct CursorInfo {
7072 let line: Int = dict [ keys. line] ,
7173 let column: Int = dict [ keys. column]
7274 {
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+ }
7983 } else {
8084 location = nil
8185 }
@@ -142,6 +146,7 @@ extension SwiftLanguageService {
142146 _ range: Range < Position > ,
143147 additionalParameters appendAdditionalParameters: ( ( SKDRequestDictionary ) -> Void ) ? = nil
144148 ) async throws -> ( cursorInfo: [ CursorInfo ] , refactorActions: [ SemanticRefactorCommand ] ) {
149+ let documentManager = try self . documentManager
145150 let snapshot = try documentManager. latestSnapshot ( uri)
146151
147152 let offsetRange = snapshot. utf8OffsetRange ( of: range)
@@ -162,10 +167,12 @@ extension SwiftLanguageService {
162167 let dict = try await sendSourcekitdRequest ( skreq, fileContents: snapshot. text)
163168
164169 var cursorInfoResults : [ CursorInfo ] = [ ]
165- if let cursorInfo = CursorInfo ( dict, sourcekitd: sourcekitd) {
170+ if let cursorInfo = CursorInfo ( dict, documentManager : documentManager , sourcekitd: sourcekitd) {
166171 cursorInfoResults. append ( cursorInfo)
167172 }
168- cursorInfoResults += dict [ keys. secondarySymbols] ? . compactMap { CursorInfo ( $0, sourcekitd: sourcekitd) } ?? [ ]
173+ cursorInfoResults +=
174+ dict [ keys. secondarySymbols] ?
175+ . compactMap { CursorInfo ( $0, documentManager: documentManager, sourcekitd: sourcekitd) } ?? [ ]
169176 let refactorActions =
170177 [ SemanticRefactorCommand] (
171178 array: dict [ keys. refactorActions] ,
0 commit comments