@@ -15,6 +15,15 @@ import IndexStoreDB
1515import LSPLogging
1616import LanguageServerProtocol
1717
18+ /// Essentially a `DocumentManager` from the `SourceKitLSP` module.
19+ ///
20+ /// Protocol is needed because the `SemanticIndex` module is lower-level than the `SourceKitLSP` module.
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
23+ /// the case if the user made edits to the file but didn't save them yet.
24+ func fileHasInMemoryModifications( _ url: URL ) -> Bool
25+ }
26+
1827public enum IndexCheckLevel {
1928 /// Consider the index out-of-date only if the source file has been deleted on disk.
2029 ///
@@ -31,7 +40,7 @@ public enum IndexCheckLevel {
3140
3241 /// Consider the index out-of-date if the source file has been deleted or modified on disk or if there are
3342 /// in-memory modifications in the given `DocumentManager`.
34- case inMemoryModifiedFiles( DocumentManager )
43+ case inMemoryModifiedFiles( InMemoryDocumentManager )
3544}
3645
3746/// A wrapper around `IndexStoreDB` that checks if returned symbol occurrences are up-to-date with regard to a
@@ -250,7 +259,7 @@ private struct IndexOutOfDateChecker {
250259 /// `documentManager` must always be the same between calls to `hasFileInMemoryModifications` since it is not part of
251260 /// the cache key. This is fine because we always assume the `documentManager` to come from the associated value of
252261 /// `CheckLevel.imMemoryModifiedFiles`, which is constant.
253- private mutating func fileHasInMemoryModifications( _ url: URL , documentManager: DocumentManager ) -> Bool {
262+ private mutating func fileHasInMemoryModifications( _ url: URL , documentManager: InMemoryDocumentManager ) -> Bool {
254263 if let cached = fileHasInMemoryModificationsCache [ url] {
255264 return cached
256265 }
@@ -305,19 +314,3 @@ private struct IndexOutOfDateChecker {
305314 return fileExists
306315 }
307316}
308-
309- extension DocumentManager {
310- /// Returns true if the file at the given URL has a different content in the document manager than on-disk. This is
311- /// the case if the user made edits to the file but didn't save them yet.
312- func fileHasInMemoryModifications( _ url: URL ) -> Bool {
313- guard let document = try ? latestSnapshot ( DocumentURI ( url) ) else {
314- return false
315- }
316-
317- guard let onDiskFileContents = try ? String ( contentsOf: url, encoding: . utf8) else {
318- // If we can't read the file on disk, it can't match any on-disk state, so it's in-memory state
319- return true
320- }
321- return onDiskFileContents != document. lineTable. content
322- }
323- }
0 commit comments