Skip to content

Commit 9bdc440

Browse files
committed
Move CheckedIndex to a new SemanticIndex module
1 parent a665d82 commit 9bdc440

9 files changed

+54
-19
lines changed

Package.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ let package = Package(
168168
]
169169
),
170170

171+
// MARK: SemanticIndex
172+
173+
.target(
174+
name: "SemanticIndex",
175+
dependencies: [
176+
"LSPLogging",
177+
"SKCore",
178+
.product(name: "IndexStoreDB", package: "indexstore-db"),
179+
],
180+
exclude: ["CMakeLists.txt"]
181+
),
182+
171183
// MARK: SKCore
172184
// Data structures and algorithms useful across the project, but not necessarily
173185
// suitable for use in other packages.
@@ -303,6 +315,7 @@ let package = Package(
303315
"LanguageServerProtocol",
304316
"LanguageServerProtocolJSONRPC",
305317
"LSPLogging",
318+
"SemanticIndex",
306319
"SKCore",
307320
"SKSupport",
308321
"SKSwiftPMWorkspace",

Sources/SemanticIndex/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
add_library(SemanticIndex STATIC
3+
CheckedIndex.swift
4+
)
5+
set_target_properties(SemanticIndex PROPERTIES
6+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
7+
target_link_libraries(SKSupport PRIVATE
8+
LSPLogging
9+
SKCore
10+
IndexStoreDB
11+
$<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>)

Sources/SourceKitLSP/CheckedIndex.swift renamed to Sources/SemanticIndex/CheckedIndex.swift

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ import IndexStoreDB
1515
import LSPLogging
1616
import 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+
1827
public 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-
}

Sources/SourceKitLSP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ target_link_libraries(SourceKitLSP PUBLIC
6262
LanguageServerProtocol
6363
LanguageServerProtocolJSONRPC
6464
LSPLogging
65+
SemanticIndex
6566
SKCore
6667
SKSupport
6768
SKSwiftPMWorkspace

Sources/SourceKitLSP/DocumentManager.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Dispatch
1414
import LSPLogging
1515
import LanguageServerProtocol
1616
import SKSupport
17+
import SemanticIndex
1718
import SwiftSyntax
1819

1920
/// An immutable snapshot of a document at a given time.
@@ -83,7 +84,7 @@ public final class Document {
8384
}
8485
}
8586

86-
public final class DocumentManager {
87+
public final class DocumentManager: InMemoryDocumentManager {
8788

8889
public enum Error: Swift.Error {
8990
case alreadyOpen(DocumentURI)
@@ -187,6 +188,18 @@ public final class DocumentManager {
187188
return document.latestSnapshot
188189
}
189190
}
191+
192+
public func fileHasInMemoryModifications(_ url: URL) -> Bool {
193+
guard let document = try? latestSnapshot(DocumentURI(url)) else {
194+
return false
195+
}
196+
197+
guard let onDiskFileContents = try? String(contentsOf: url, encoding: .utf8) else {
198+
// If we can't read the file on disk, it can't match any on-disk state, so it's in-memory state
199+
return true
200+
}
201+
return onDiskFileContents != document.lineTable.content
202+
}
190203
}
191204

192205
extension DocumentManager {

Sources/SourceKitLSP/IndexStoreDB+MainFilesProvider.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Foundation
1414
import LSPLogging
1515
import LanguageServerProtocol
1616
import SKCore
17+
import SemanticIndex
1718

1819
extension UncheckedIndex {
1920
public func mainFilesContainingFile(_ uri: DocumentURI) -> Set<DocumentURI> {

Sources/SourceKitLSP/Rename.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import IndexStoreDB
1414
import LSPLogging
1515
import LanguageServerProtocol
1616
import SKSupport
17+
import SemanticIndex
1718
import SourceKitD
1819
import SwiftSyntax
1920

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import PackageLoading
2020
import SKCore
2121
import SKSupport
2222
import SKSwiftPMWorkspace
23+
import SemanticIndex
2324
import SourceKitD
2425

2526
import struct PackageModel.BuildFlags

Sources/SourceKitLSP/Workspace.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import LanguageServerProtocol
1616
import SKCore
1717
import SKSupport
1818
import SKSwiftPMWorkspace
19+
import SemanticIndex
1920

2021
import struct TSCBasic.AbsolutePath
2122
import struct TSCBasic.RelativePath

0 commit comments

Comments
 (0)