@@ -19,9 +19,9 @@ import LanguageServerProtocol
19
19
///
20
20
/// Protocol is needed because the `SemanticIndex` module is lower-level than the `SourceKitLSP` module.
21
21
public protocol InMemoryDocumentManager {
22
- /// Returns true if the file at the given URL has a different content in the document manager than on-disk. This is
22
+ /// Returns true if the file at the given URI has a different content in the document manager than on-disk. This is
23
23
/// the case if the user made edits to the file but didn't save them yet.
24
- func fileHasInMemoryModifications( _ url : URL ) -> Bool
24
+ func fileHasInMemoryModifications( _ uri : DocumentURI ) -> Bool
25
25
}
26
26
27
27
public enum IndexCheckLevel {
@@ -105,7 +105,7 @@ public final class CheckedIndex {
105
105
}
106
106
107
107
public func symbols( inFilePath path: String ) -> [ Symbol ] {
108
- guard self . hasUpToDateUnit ( for: URL ( fileURLWithPath : path, isDirectory: false ) ) else {
108
+ guard self . hasUpToDateUnit ( for: DocumentURI ( filePath : path, isDirectory: false ) ) else {
109
109
return [ ]
110
110
}
111
111
return index. symbols ( inFilePath: path)
@@ -121,11 +121,11 @@ public final class CheckedIndex {
121
121
/// If `crossLanguage` is set to `true`, Swift files that import a header through a module will also be reported.
122
122
public func mainFilesContainingFile( uri: DocumentURI , crossLanguage: Bool = false ) -> [ DocumentURI ] {
123
123
return index. mainFilesContainingFile ( path: uri. pseudoPath, crossLanguage: crossLanguage) . compactMap {
124
- let url = URL ( fileURLWithPath : $0)
125
- guard checker. indexHasUpToDateUnit ( for: url , mainFile: nil , index: self . index) else {
124
+ let uri = DocumentURI ( filePath : $0, isDirectory : false )
125
+ guard checker. indexHasUpToDateUnit ( for: uri , mainFile: nil , index: self . index) else {
126
126
return nil
127
127
}
128
- return DocumentURI ( url )
128
+ return uri
129
129
}
130
130
}
131
131
@@ -141,16 +141,16 @@ public final class CheckedIndex {
141
141
/// If `mainFile` is passed, then `url` is a header file that won't have a unit associated with it. `mainFile` is
142
142
/// assumed to be a file that imports `url`. To check that `url` has an up-to-date unit, check that the latest unit
143
143
/// for `mainFile` is newer than the mtime of the header file at `url`.
144
- public func hasUpToDateUnit( for url : URL , mainFile: URL ? = nil ) -> Bool {
145
- return checker. indexHasUpToDateUnit ( for: url , mainFile: mainFile, index: index)
144
+ public func hasUpToDateUnit( for uri : DocumentURI , mainFile: DocumentURI ? = nil ) -> Bool {
145
+ return checker. indexHasUpToDateUnit ( for: uri , mainFile: mainFile, index: index)
146
146
}
147
147
148
- /// Returns true if the file at the given URL has a different content in the document manager than on-disk. This is
148
+ /// Returns true if the file at the given URI has a different content in the document manager than on-disk. This is
149
149
/// the case if the user made edits to the file but didn't save them yet.
150
150
///
151
151
/// - Important: This must only be called on a `CheckedIndex` with a `checkLevel` of `inMemoryModifiedFiles`
152
- public func fileHasInMemoryModifications( _ url : URL ) -> Bool {
153
- return checker. fileHasInMemoryModifications ( url )
152
+ public func fileHasInMemoryModifications( _ uri : DocumentURI ) -> Bool {
153
+ return checker. fileHasInMemoryModifications ( uri )
154
154
}
155
155
}
156
156
@@ -212,14 +212,14 @@ private struct IndexOutOfDateChecker {
212
212
}
213
213
}
214
214
215
- /// Caches whether a file URL has modifications in `documentManager` that haven't been saved to disk yet.
216
- private var fileHasInMemoryModificationsCache : [ URL : Bool ] = [ : ]
215
+ /// Caches whether a document has modifications in `documentManager` that haven't been saved to disk yet.
216
+ private var fileHasInMemoryModificationsCache : [ DocumentURI : Bool ] = [ : ]
217
217
218
- /// File URLs to modification times that have already been computed.
219
- private var modTimeCache : [ URL : ModificationTime ] = [ : ]
218
+ /// Document URIs to modification times that have already been computed.
219
+ private var modTimeCache : [ DocumentURI : ModificationTime ] = [ : ]
220
220
221
- /// File URLs to whether they exist on the file system
222
- private var fileExistsCache : [ URL : Bool ] = [ : ]
221
+ /// Document URIs to whether they exist on the file system
222
+ private var fileExistsCache : [ DocumentURI : Bool ] = [ : ]
223
223
224
224
init ( checkLevel: IndexCheckLevel ) {
225
225
self . checkLevel = checkLevel
@@ -230,16 +230,16 @@ private struct IndexOutOfDateChecker {
230
230
/// Returns `true` if the source file for the given symbol location exists and has not been modified after it has been
231
231
/// indexed.
232
232
mutating func isUpToDate( _ symbolLocation: SymbolLocation ) -> Bool {
233
- let url = URL ( fileURLWithPath : symbolLocation. path, isDirectory: false )
233
+ let uri = DocumentURI ( filePath : symbolLocation. path, isDirectory: false )
234
234
switch checkLevel {
235
235
case . inMemoryModifiedFiles( let documentManager) :
236
- if fileHasInMemoryModifications ( url , documentManager: documentManager) {
236
+ if fileHasInMemoryModifications ( uri , documentManager: documentManager) {
237
237
return false
238
238
}
239
239
fallthrough
240
240
case . modifiedFiles:
241
241
do {
242
- let sourceFileModificationDate = try modificationDate ( of: url )
242
+ let sourceFileModificationDate = try modificationDate ( of: uri )
243
243
switch sourceFileModificationDate {
244
244
case . fileDoesNotExist:
245
245
return false
@@ -251,7 +251,7 @@ private struct IndexOutOfDateChecker {
251
251
return true
252
252
}
253
253
case . deletedFiles:
254
- return fileExists ( at: url )
254
+ return fileExists ( at: uri )
255
255
}
256
256
}
257
257
@@ -262,7 +262,7 @@ private struct IndexOutOfDateChecker {
262
262
/// If `mainFile` is passed, then `filePath` is a header file that won't have a unit associated with it. `mainFile` is
263
263
/// assumed to be a file that imports `url`. To check that `url` has an up-to-date unit, check that the latest unit
264
264
/// for `mainFile` is newer than the mtime of the header file at `url`.
265
- mutating func indexHasUpToDateUnit( for filePath: URL , mainFile: URL ? , index: IndexStoreDB ) -> Bool {
265
+ mutating func indexHasUpToDateUnit( for filePath: DocumentURI , mainFile: DocumentURI ? , index: IndexStoreDB ) -> Bool {
266
266
switch checkLevel {
267
267
case . inMemoryModifiedFiles( let documentManager) :
268
268
if fileHasInMemoryModifications ( filePath, documentManager: documentManager) {
@@ -273,7 +273,9 @@ private struct IndexOutOfDateChecker {
273
273
// If there are no in-memory modifications check if there are on-disk modifications.
274
274
fallthrough
275
275
case . modifiedFiles:
276
- guard let lastUnitDate = index. dateOfLatestUnitFor ( filePath: ( mainFile ?? filePath) . path) else {
276
+ guard let fileURL = ( mainFile ?? filePath) . fileURL,
277
+ let lastUnitDate = index. dateOfLatestUnitFor ( filePath: fileURL. path)
278
+ else {
277
279
return false
278
280
}
279
281
do {
@@ -300,23 +302,25 @@ private struct IndexOutOfDateChecker {
300
302
/// `documentManager` must always be the same between calls to `hasFileInMemoryModifications` since it is not part of
301
303
/// the cache key. This is fine because we always assume the `documentManager` to come from the associated value of
302
304
/// `CheckLevel.imMemoryModifiedFiles`, which is constant.
303
- private mutating func fileHasInMemoryModifications( _ url: URL , documentManager: InMemoryDocumentManager ) -> Bool {
304
- if let cached = fileHasInMemoryModificationsCache [ url] {
305
+ private mutating func fileHasInMemoryModifications( _ uri: DocumentURI , documentManager: InMemoryDocumentManager )
306
+ -> Bool
307
+ {
308
+ if let cached = fileHasInMemoryModificationsCache [ uri] {
305
309
return cached
306
310
}
307
- let hasInMemoryModifications = documentManager. fileHasInMemoryModifications ( url )
308
- fileHasInMemoryModificationsCache [ url ] = hasInMemoryModifications
311
+ let hasInMemoryModifications = documentManager. fileHasInMemoryModifications ( uri )
312
+ fileHasInMemoryModificationsCache [ uri ] = hasInMemoryModifications
309
313
return hasInMemoryModifications
310
314
}
311
315
312
- /// Returns true if the file at the given URL has a different content in the document manager than on-disk. This is
316
+ /// Returns true if the file at the given URI has a different content in the document manager than on-disk. This is
313
317
/// the case if the user made edits to the file but didn't save them yet.
314
318
///
315
319
/// - Important: This must only be called on an `IndexOutOfDateChecker` with a `checkLevel` of `inMemoryModifiedFiles`
316
- mutating func fileHasInMemoryModifications( _ url : URL ) -> Bool {
320
+ mutating func fileHasInMemoryModifications( _ uri : DocumentURI ) -> Bool {
317
321
switch checkLevel {
318
322
case . inMemoryModifiedFiles( let documentManager) :
319
- return fileHasInMemoryModifications ( url , documentManager: documentManager)
323
+ return fileHasInMemoryModifications ( uri , documentManager: documentManager)
320
324
case . modifiedFiles, . deletedFiles:
321
325
logger. fault (
322
326
" fileHasInMemoryModifications(at:) must only be called on an `IndexOutOfDateChecker` with check level .inMemoryModifiedFiles "
@@ -325,9 +329,12 @@ private struct IndexOutOfDateChecker {
325
329
}
326
330
}
327
331
328
- private func modificationDateUncached( of url : URL ) throws -> ModificationTime {
332
+ private func modificationDateUncached( of uri : DocumentURI ) throws -> ModificationTime {
329
333
do {
330
- let attributes = try FileManager . default. attributesOfItem ( atPath: url. resolvingSymlinksInPath ( ) . path)
334
+ guard let fileURL = uri. fileURL else {
335
+ return . fileDoesNotExist
336
+ }
337
+ let attributes = try FileManager . default. attributesOfItem ( atPath: fileURL. resolvingSymlinksInPath ( ) . path)
331
338
guard let modificationDate = attributes [ FileAttributeKey . modificationDate] as? Date else {
332
339
throw Error . fileAttributesDontHaveModificationDate
333
340
}
@@ -337,21 +344,26 @@ private struct IndexOutOfDateChecker {
337
344
}
338
345
}
339
346
340
- private mutating func modificationDate( of url : URL ) throws -> ModificationTime {
341
- if let cached = modTimeCache [ url ] {
347
+ private mutating func modificationDate( of uri : DocumentURI ) throws -> ModificationTime {
348
+ if let cached = modTimeCache [ uri ] {
342
349
return cached
343
350
}
344
- let modTime = try modificationDateUncached ( of: url )
345
- modTimeCache [ url ] = modTime
351
+ let modTime = try modificationDateUncached ( of: uri )
352
+ modTimeCache [ uri ] = modTime
346
353
return modTime
347
354
}
348
355
349
- private mutating func fileExists( at url : URL ) -> Bool {
350
- if let cached = fileExistsCache [ url ] {
356
+ private mutating func fileExists( at uri : DocumentURI ) -> Bool {
357
+ if let cached = fileExistsCache [ uri ] {
351
358
return cached
352
359
}
353
- let fileExists = FileManager . default. fileExists ( atPath: url. path)
354
- fileExistsCache [ url] = fileExists
360
+ let fileExists =
361
+ if let fileUrl = uri. fileURL {
362
+ FileManager . default. fileExists ( atPath: fileUrl. path)
363
+ } else {
364
+ false
365
+ }
366
+ fileExistsCache [ uri] = fileExists
355
367
return fileExists
356
368
}
357
369
}
0 commit comments