@@ -19,9 +19,9 @@ import LanguageServerProtocol
1919///
2020/// Protocol is needed because the `SemanticIndex` module is lower-level than the `SourceKitLSP` module.
2121public 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
2323 /// 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
2525}
2626
2727public enum IndexCheckLevel {
@@ -105,7 +105,7 @@ public final class CheckedIndex {
105105 }
106106
107107 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 {
109109 return [ ]
110110 }
111111 return index. symbols ( inFilePath: path)
@@ -121,11 +121,11 @@ public final class CheckedIndex {
121121 /// If `crossLanguage` is set to `true`, Swift files that import a header through a module will also be reported.
122122 public func mainFilesContainingFile( uri: DocumentURI , crossLanguage: Bool = false ) -> [ DocumentURI ] {
123123 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 {
126126 return nil
127127 }
128- return DocumentURI ( url )
128+ return uri
129129 }
130130 }
131131
@@ -141,16 +141,16 @@ public final class CheckedIndex {
141141 /// If `mainFile` is passed, then `url` is a header file that won't have a unit associated with it. `mainFile` is
142142 /// assumed to be a file that imports `url`. To check that `url` has an up-to-date unit, check that the latest unit
143143 /// 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)
146146 }
147147
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
149149 /// the case if the user made edits to the file but didn't save them yet.
150150 ///
151151 /// - 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 )
154154 }
155155}
156156
@@ -212,14 +212,14 @@ private struct IndexOutOfDateChecker {
212212 }
213213 }
214214
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 ] = [ : ]
217217
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 ] = [ : ]
220220
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 ] = [ : ]
223223
224224 init ( checkLevel: IndexCheckLevel ) {
225225 self . checkLevel = checkLevel
@@ -230,16 +230,16 @@ private struct IndexOutOfDateChecker {
230230 /// Returns `true` if the source file for the given symbol location exists and has not been modified after it has been
231231 /// indexed.
232232 mutating func isUpToDate( _ symbolLocation: SymbolLocation ) -> Bool {
233- let url = URL ( fileURLWithPath : symbolLocation. path, isDirectory: false )
233+ let uri = DocumentURI ( filePath : symbolLocation. path, isDirectory: false )
234234 switch checkLevel {
235235 case . inMemoryModifiedFiles( let documentManager) :
236- if fileHasInMemoryModifications ( url , documentManager: documentManager) {
236+ if fileHasInMemoryModifications ( uri , documentManager: documentManager) {
237237 return false
238238 }
239239 fallthrough
240240 case . modifiedFiles:
241241 do {
242- let sourceFileModificationDate = try modificationDate ( of: url )
242+ let sourceFileModificationDate = try modificationDate ( of: uri )
243243 switch sourceFileModificationDate {
244244 case . fileDoesNotExist:
245245 return false
@@ -251,7 +251,7 @@ private struct IndexOutOfDateChecker {
251251 return true
252252 }
253253 case . deletedFiles:
254- return fileExists ( at: url )
254+ return fileExists ( at: uri )
255255 }
256256 }
257257
@@ -262,7 +262,7 @@ private struct IndexOutOfDateChecker {
262262 /// If `mainFile` is passed, then `filePath` is a header file that won't have a unit associated with it. `mainFile` is
263263 /// assumed to be a file that imports `url`. To check that `url` has an up-to-date unit, check that the latest unit
264264 /// 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 {
266266 switch checkLevel {
267267 case . inMemoryModifiedFiles( let documentManager) :
268268 if fileHasInMemoryModifications ( filePath, documentManager: documentManager) {
@@ -273,7 +273,9 @@ private struct IndexOutOfDateChecker {
273273 // If there are no in-memory modifications check if there are on-disk modifications.
274274 fallthrough
275275 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 {
277279 return false
278280 }
279281 do {
@@ -300,23 +302,25 @@ private struct IndexOutOfDateChecker {
300302 /// `documentManager` must always be the same between calls to `hasFileInMemoryModifications` since it is not part of
301303 /// the cache key. This is fine because we always assume the `documentManager` to come from the associated value of
302304 /// `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] {
305309 return cached
306310 }
307- let hasInMemoryModifications = documentManager. fileHasInMemoryModifications ( url )
308- fileHasInMemoryModificationsCache [ url ] = hasInMemoryModifications
311+ let hasInMemoryModifications = documentManager. fileHasInMemoryModifications ( uri )
312+ fileHasInMemoryModificationsCache [ uri ] = hasInMemoryModifications
309313 return hasInMemoryModifications
310314 }
311315
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
313317 /// the case if the user made edits to the file but didn't save them yet.
314318 ///
315319 /// - 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 {
317321 switch checkLevel {
318322 case . inMemoryModifiedFiles( let documentManager) :
319- return fileHasInMemoryModifications ( url , documentManager: documentManager)
323+ return fileHasInMemoryModifications ( uri , documentManager: documentManager)
320324 case . modifiedFiles, . deletedFiles:
321325 logger. fault (
322326 " fileHasInMemoryModifications(at:) must only be called on an `IndexOutOfDateChecker` with check level .inMemoryModifiedFiles "
@@ -325,9 +329,12 @@ private struct IndexOutOfDateChecker {
325329 }
326330 }
327331
328- private func modificationDateUncached( of url : URL ) throws -> ModificationTime {
332+ private func modificationDateUncached( of uri : DocumentURI ) throws -> ModificationTime {
329333 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)
331338 guard let modificationDate = attributes [ FileAttributeKey . modificationDate] as? Date else {
332339 throw Error . fileAttributesDontHaveModificationDate
333340 }
@@ -337,21 +344,26 @@ private struct IndexOutOfDateChecker {
337344 }
338345 }
339346
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 ] {
342349 return cached
343350 }
344- let modTime = try modificationDateUncached ( of: url )
345- modTimeCache [ url ] = modTime
351+ let modTime = try modificationDateUncached ( of: uri )
352+ modTimeCache [ uri ] = modTime
346353 return modTime
347354 }
348355
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 ] {
351358 return cached
352359 }
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
355367 return fileExists
356368 }
357369}
0 commit comments