@@ -46,8 +46,14 @@ struct CursorInfo {
46
46
self . documentation = documentation
47
47
}
48
48
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.
49
54
init ? (
50
55
_ dict: SKDResponseDictionary ,
56
+ snapshot: DocumentSnapshot ,
51
57
documentManager: DocumentManager ,
52
58
sourcekitd: some SourceKitD
53
59
) {
@@ -63,7 +69,13 @@ struct CursorInfo {
63
69
let column: Int = dict [ keys. column]
64
70
{
65
71
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 {
67
79
let position = snapshot. positionOf ( zeroBasedLine: line - 1 , utf8Column: column - 1 )
68
80
location = Location ( uri: uri, range: Range ( position) )
69
81
} else {
@@ -133,14 +145,13 @@ extension SwiftLanguageService {
133
145
/// - fallbackSettingsAfterTimeout: Whether fallback build settings should be used for the cursor info request if no
134
146
/// build settings can be retrieved within a timeout.
135
147
func cursorInfo(
136
- _ uri: DocumentURI ,
148
+ _ snapshot: DocumentSnapshot ,
149
+ compileCommand: SwiftCompileCommand ? ,
137
150
_ range: Range < Position > ,
138
151
includeSymbolGraph: Bool = false ,
139
- fallbackSettingsAfterTimeout: Bool ,
140
152
additionalParameters appendAdditionalParameters: ( ( SKDRequestDictionary ) -> Void ) ? = nil
141
153
) async throws -> ( cursorInfo: [ CursorInfo ] , refactorActions: [ SemanticRefactorCommand ] , symbolGraph: String ? ) {
142
154
let documentManager = try self . documentManager
143
- let snapshot = try await self . latestSnapshot ( for: uri)
144
155
145
156
let offsetRange = snapshot. utf8OffsetRange ( of: range)
146
157
@@ -154,31 +165,46 @@ extension SwiftLanguageService {
154
165
keys. sourceFile: snapshot. uri. sourcekitdSourceFile,
155
166
keys. primaryFile: snapshot. uri. primaryFile? . pseudoPath,
156
167
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 ] ? ,
159
169
] )
160
170
161
171
appendAdditionalParameters ? ( skreq)
162
172
163
173
let dict = try await sendSourcekitdRequest ( skreq, fileContents: snapshot. text)
164
174
165
175
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) {
167
177
cursorInfoResults. append ( cursorInfo)
168
178
}
169
179
cursorInfoResults +=
170
180
dict [ keys. secondarySymbols] ?
171
- . compactMap { CursorInfo ( $0, documentManager: documentManager, sourcekitd: sourcekitd) } ?? [ ]
181
+ . compactMap { CursorInfo ( $0, snapshot : snapshot , documentManager: documentManager, sourcekitd: sourcekitd) } ?? [ ]
172
182
let refactorActions =
173
183
[ SemanticRefactorCommand] (
174
184
array: dict [ keys. refactorActions] ,
175
185
range: range,
176
- textDocument: TextDocumentIdentifier ( uri) ,
186
+ textDocument: TextDocumentIdentifier ( snapshot . uri) ,
177
187
keys,
178
188
self . sourcekitd. api
179
189
) ?? [ ]
180
190
let symbolGraph : String ? = dict [ keys. symbolGraph]
181
191
182
192
return ( cursorInfoResults, refactorActions, symbolGraph)
183
193
}
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
+ }
184
210
}
0 commit comments