@@ -46,8 +46,14 @@ struct CursorInfo {
4646 self . documentation = documentation
4747 }
4848
49+ /// `snapshot` is the snapshot from which the cursor info was invoked. It is necessary to pass that snapshot in here
50+ /// because snapshot might not be open in `documentManager` if cursor info was invoked using file contents from disk
51+ /// using `cursorInfoFromDisk` and thus we couldn't convert positions if the snapshot wasn't passed in explicitly.
52+ /// For all document other than `snapshot`, `documentManager` is used to find the latest snapshot of a document to
53+ /// convert positions.
4954 init ? (
5055 _ dict: SKDResponseDictionary ,
56+ snapshot: DocumentSnapshot ,
5157 documentManager: DocumentManager ,
5258 sourcekitd: some SourceKitD
5359 ) {
@@ -63,7 +69,13 @@ struct CursorInfo {
6369 let column: Int = dict [ keys. column]
6470 {
6571 let uri = DocumentURI ( filePath: filepath, isDirectory: false )
66- if let snapshot = documentManager. latestSnapshotOrDisk ( uri, language: . swift) {
72+ let snapshot : DocumentSnapshot ? =
73+ if snapshot. uri. sourcekitdSourceFile == filepath {
74+ snapshot
75+ } else {
76+ documentManager. latestSnapshotOrDisk ( uri, language: . swift)
77+ }
78+ if let snapshot {
6779 let position = snapshot. positionOf ( zeroBasedLine: line - 1 , utf8Column: column - 1 )
6880 location = Location ( uri: uri, range: Range ( position) )
6981 } else {
@@ -133,14 +145,13 @@ extension SwiftLanguageService {
133145 /// - fallbackSettingsAfterTimeout: Whether fallback build settings should be used for the cursor info request if no
134146 /// build settings can be retrieved within a timeout.
135147 func cursorInfo(
136- _ uri: DocumentURI ,
148+ _ snapshot: DocumentSnapshot ,
149+ compileCommand: SwiftCompileCommand ? ,
137150 _ range: Range < Position > ,
138151 includeSymbolGraph: Bool = false ,
139- fallbackSettingsAfterTimeout: Bool ,
140152 additionalParameters appendAdditionalParameters: ( ( SKDRequestDictionary ) -> Void ) ? = nil
141153 ) async throws -> ( cursorInfo: [ CursorInfo ] , refactorActions: [ SemanticRefactorCommand ] , symbolGraph: String ? ) {
142154 let documentManager = try self . documentManager
143- let snapshot = try await self . latestSnapshot ( for: uri)
144155
145156 let offsetRange = snapshot. utf8OffsetRange ( of: range)
146157
@@ -154,31 +165,46 @@ extension SwiftLanguageService {
154165 keys. sourceFile: snapshot. uri. sourcekitdSourceFile,
155166 keys. primaryFile: snapshot. uri. primaryFile? . pseudoPath,
156167 keys. retrieveSymbolGraph: includeSymbolGraph ? 1 : 0 ,
157- keys. compilerArgs: await self . compileCommand ( for: uri, fallbackAfterTimeout: fallbackSettingsAfterTimeout) ?
158- . compilerArgs as [ SKDRequestValue ] ? ,
168+ keys. compilerArgs: compileCommand? . compilerArgs as [ SKDRequestValue ] ? ,
159169 ] )
160170
161171 appendAdditionalParameters ? ( skreq)
162172
163173 let dict = try await sendSourcekitdRequest ( skreq, fileContents: snapshot. text)
164174
165175 var cursorInfoResults : [ CursorInfo ] = [ ]
166- if let cursorInfo = CursorInfo ( dict, documentManager: documentManager, sourcekitd: sourcekitd) {
176+ if let cursorInfo = CursorInfo ( dict, snapshot : snapshot , documentManager: documentManager, sourcekitd: sourcekitd) {
167177 cursorInfoResults. append ( cursorInfo)
168178 }
169179 cursorInfoResults +=
170180 dict [ keys. secondarySymbols] ?
171- . compactMap { CursorInfo ( $0, documentManager: documentManager, sourcekitd: sourcekitd) } ?? [ ]
181+ . compactMap { CursorInfo ( $0, snapshot : snapshot , documentManager: documentManager, sourcekitd: sourcekitd) } ?? [ ]
172182 let refactorActions =
173183 [ SemanticRefactorCommand] (
174184 array: dict [ keys. refactorActions] ,
175185 range: range,
176- textDocument: TextDocumentIdentifier ( uri) ,
186+ textDocument: TextDocumentIdentifier ( snapshot . uri) ,
177187 keys,
178188 self . sourcekitd. api
179189 ) ?? [ ]
180190 let symbolGraph : String ? = dict [ keys. symbolGraph]
181191
182192 return ( cursorInfoResults, refactorActions, symbolGraph)
183193 }
194+
195+ func cursorInfo(
196+ _ uri: DocumentURI ,
197+ _ range: Range < Position > ,
198+ includeSymbolGraph: Bool = false ,
199+ fallbackSettingsAfterTimeout: Bool ,
200+ additionalParameters appendAdditionalParameters: ( ( SKDRequestDictionary ) -> Void ) ? = nil
201+ ) async throws -> ( cursorInfo: [ CursorInfo ] , refactorActions: [ SemanticRefactorCommand ] , symbolGraph: String ? ) {
202+ return try await self . cursorInfo (
203+ self . latestSnapshot ( for: uri) ,
204+ compileCommand: await self . compileCommand ( for: uri, fallbackAfterTimeout: fallbackSettingsAfterTimeout) ,
205+ range,
206+ includeSymbolGraph: includeSymbolGraph,
207+ additionalParameters: appendAdditionalParameters
208+ )
209+ }
184210}
0 commit comments