Skip to content

Commit e9ea204

Browse files
authored
Merge pull request #1930 from ahoppen/inject-indexstoredb
Add a hook to inject an `IndexStoreDB` instance into `SourceKitLSPServer`
2 parents a1716e2 + 8cfc4b6 commit e9ea204

15 files changed

+111
-111
lines changed

Sources/BuildSystemIntegration/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

22
add_library(BuildSystemIntegration STATIC
33
BuildSettingsLogger.swift
4+
BuildSystemHooks.swift
45
BuildSystemManager.swift
56
BuildSystemManagerDelegate.swift
67
BuildSystemMessageDependencyTracker.swift
7-
BuildSystemTestHooks.swift
88
BuildTargetIdentifierExtensions.swift
99
BuiltInBuildSystem.swift
1010
BuiltInBuildSystemAdapter.swift
@@ -17,7 +17,6 @@ add_library(BuildSystemIntegration STATIC
1717
JSONCompilationDatabaseBuildSystem.swift
1818
LegacyBuildServerBuildSystem.swift
1919
MainFilesProvider.swift
20-
PathPrefixMapping.swift
2120
PrefixMessageWithTaskEmoji.swift
2221
SplitShellCommand.swift
2322
SwiftPMBuildSystem.swift

Sources/BuildSystemIntegration/PathPrefixMapping.swift

Lines changed: 0 additions & 26 deletions
This file was deleted.

Sources/SemanticIndex/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ add_library(SemanticIndex STATIC
33
CheckedIndex.swift
44
CompilerCommandLineOption.swift
55
IndexTaskDescription.swift
6-
IndexTestHooks.swift
6+
IndexHooks.swift
77
PreparationTaskDescription.swift
88
SemanticIndexManager.swift
99
TaskScheduler.swift

Sources/SemanticIndex/IndexTestHooks.swift renamed to Sources/SemanticIndex/IndexHooks.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,30 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if compiler(>=6)
14+
package import IndexStoreDB
15+
package import Foundation
16+
#else
17+
import IndexStoreDB
18+
import Foundation
19+
#endif
20+
21+
/// When running SourceKit-LSP in-process, allows the creator of `SourceKitLSPServer` to provide the `IndexStoreDB`
22+
/// instead of SourceKit-LSP creating the instance when needed.
23+
package protocol IndexInjector: Sendable {
24+
func createIndex(
25+
storePath: URL,
26+
databasePath: URL,
27+
indexStoreLibraryPath: URL,
28+
delegate: IndexDelegate,
29+
prefixMappings: [PathMapping]
30+
) async throws -> IndexStoreDB
31+
}
32+
1333
/// Callbacks that allow inspection of internal state modifications during testing.
14-
package struct IndexTestHooks: Sendable {
34+
package struct IndexHooks: Sendable {
35+
package var indexInjector: IndexInjector?
36+
1537
package var buildGraphGenerationDidStart: (@Sendable () async -> Void)?
1638

1739
package var buildGraphGenerationDidFinish: (@Sendable () async -> Void)?
@@ -26,13 +48,15 @@ package struct IndexTestHooks: Sendable {
2648
package var updateIndexStoreTaskDidFinish: (@Sendable (UpdateIndexStoreTaskDescription) async -> Void)?
2749

2850
package init(
51+
indexInjector: IndexInjector? = nil,
2952
buildGraphGenerationDidStart: (@Sendable () async -> Void)? = nil,
3053
buildGraphGenerationDidFinish: (@Sendable () async -> Void)? = nil,
3154
preparationTaskDidStart: (@Sendable (PreparationTaskDescription) async -> Void)? = nil,
3255
preparationTaskDidFinish: (@Sendable (PreparationTaskDescription) async -> Void)? = nil,
3356
updateIndexStoreTaskDidStart: (@Sendable (UpdateIndexStoreTaskDescription) async -> Void)? = nil,
3457
updateIndexStoreTaskDidFinish: (@Sendable (UpdateIndexStoreTaskDescription) async -> Void)? = nil
3558
) {
59+
self.indexInjector = indexInjector
3660
self.buildGraphGenerationDidStart = buildGraphGenerationDidStart
3761
self.buildGraphGenerationDidFinish = buildGraphGenerationDidFinish
3862
self.preparationTaskDidStart = preparationTaskDidStart

Sources/SemanticIndex/PreparationTaskDescription.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ package struct PreparationTaskDescription: IndexTaskDescription {
5555
/// See `SemanticIndexManager.logMessageToIndexLog`.
5656
private let logMessageToIndexLog: @Sendable (_ taskID: String, _ message: String) -> Void
5757

58-
/// Test hooks that should be called when the preparation task finishes.
59-
private let testHooks: IndexTestHooks
58+
/// Hooks that should be called when the preparation task finishes.
59+
private let hooks: IndexHooks
6060

6161
/// The task is idempotent because preparing the same target twice produces the same result as preparing it once.
6262
package var isIdempotent: Bool { true }
@@ -76,13 +76,13 @@ package struct PreparationTaskDescription: IndexTaskDescription {
7676
buildSystemManager: BuildSystemManager,
7777
preparationUpToDateTracker: UpToDateTracker<BuildTargetIdentifier>,
7878
logMessageToIndexLog: @escaping @Sendable (_ taskID: String, _ message: String) -> Void,
79-
testHooks: IndexTestHooks
79+
hooks: IndexHooks
8080
) {
8181
self.targetsToPrepare = targetsToPrepare
8282
self.buildSystemManager = buildSystemManager
8383
self.preparationUpToDateTracker = preparationUpToDateTracker
8484
self.logMessageToIndexLog = logMessageToIndexLog
85-
self.testHooks = testHooks
85+
self.hooks = hooks
8686
}
8787

8888
package func execute() async {
@@ -96,7 +96,7 @@ package struct PreparationTaskDescription: IndexTaskDescription {
9696
if targetsToPrepare.isEmpty {
9797
return
9898
}
99-
await testHooks.preparationTaskDidStart?(self)
99+
await hooks.preparationTaskDidStart?(self)
100100

101101
let targetsToPrepareDescription = targetsToPrepare.map(\.uri.stringValue).joined(separator: ", ")
102102
logger.log(
@@ -119,7 +119,7 @@ package struct PreparationTaskDescription: IndexTaskDescription {
119119
"Preparation failed: \(error.forLogging)"
120120
)
121121
}
122-
await testHooks.preparationTaskDidFinish?(self)
122+
await hooks.preparationTaskDidFinish?(self)
123123
if !Task.isCancelled {
124124
await preparationUpToDateTracker.markUpToDate(targetsToPrepare, updateOperationStartDate: startDate)
125125
}

Sources/SemanticIndex/SemanticIndexManager.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ package final actor SemanticIndexManager {
160160
/// an expression for a long time.
161161
package var updateIndexStoreTimeout: Duration
162162

163-
private let testHooks: IndexTestHooks
163+
private let hooks: IndexHooks
164164

165165
/// The tasks to generate the build graph (resolving package dependencies, generating the build description,
166166
/// ...) and to schedule indexing of modified tasks.
@@ -237,7 +237,7 @@ package final actor SemanticIndexManager {
237237
index: UncheckedIndex,
238238
buildSystemManager: BuildSystemManager,
239239
updateIndexStoreTimeout: Duration,
240-
testHooks: IndexTestHooks,
240+
hooks: IndexHooks,
241241
indexTaskScheduler: TaskScheduler<AnyIndexTaskDescription>,
242242
logMessageToIndexLog: @escaping @Sendable (_ taskID: String, _ message: String) -> Void,
243243
indexTasksWereScheduled: @escaping @Sendable (Int) -> Void,
@@ -246,7 +246,7 @@ package final actor SemanticIndexManager {
246246
self.index = index
247247
self.buildSystemManager = buildSystemManager
248248
self.updateIndexStoreTimeout = updateIndexStoreTimeout
249-
self.testHooks = testHooks
249+
self.hooks = hooks
250250
self.indexTaskScheduler = indexTaskScheduler
251251
self.logMessageToIndexLog = logMessageToIndexLog
252252
self.indexTasksWereScheduled = indexTasksWereScheduled
@@ -278,12 +278,12 @@ package final actor SemanticIndexManager {
278278
let taskId = UUID()
279279
let generateBuildGraphTask = Task(priority: .low) {
280280
await withLoggingSubsystemAndScope(subsystem: indexLoggingSubsystem, scope: "build-graph-generation") {
281-
await testHooks.buildGraphGenerationDidStart?()
281+
await hooks.buildGraphGenerationDidStart?()
282282
await self.buildSystemManager.waitForUpToDateBuildGraph()
283283
// Ensure that we have an up-to-date indexstore-db. Waiting for the indexstore-db to be updated is cheaper than
284284
// potentially not knowing about unit files, which causes the corresponding source files to be re-indexed.
285285
index.pollForUnitChangesAndWait()
286-
await testHooks.buildGraphGenerationDidFinish?()
286+
await hooks.buildGraphGenerationDidFinish?()
287287
// TODO: Ideally this would be a type like any Collection<DocumentURI> & Sendable but that doesn't work due to
288288
// https://github.com/swiftlang/swift/issues/75602
289289
var filesToIndex: [DocumentURI] =
@@ -519,7 +519,7 @@ package final actor SemanticIndexManager {
519519
buildSystemManager: self.buildSystemManager,
520520
preparationUpToDateTracker: preparationUpToDateTracker,
521521
logMessageToIndexLog: logMessageToIndexLog,
522-
testHooks: testHooks
522+
hooks: hooks
523523
)
524524
)
525525
if Task.isCancelled {
@@ -581,7 +581,7 @@ package final actor SemanticIndexManager {
581581
indexFilesWithUpToDateUnit: indexFilesWithUpToDateUnit,
582582
logMessageToIndexLog: logMessageToIndexLog,
583583
timeout: updateIndexStoreTimeout,
584-
testHooks: testHooks
584+
hooks: hooks
585585
)
586586
)
587587

Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
146146
private let timeout: Duration
147147

148148
/// Test hooks that should be called when the index task finishes.
149-
private let testHooks: IndexTestHooks
149+
private let hooks: IndexHooks
150150

151151
/// The task is idempotent because indexing the same file twice produces the same result as indexing it once.
152152
package var isIdempotent: Bool { true }
@@ -173,7 +173,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
173173
indexFilesWithUpToDateUnit: Bool,
174174
logMessageToIndexLog: @escaping @Sendable (_ taskID: String, _ message: String) -> Void,
175175
timeout: Duration,
176-
testHooks: IndexTestHooks
176+
hooks: IndexHooks
177177
) {
178178
self.filesToIndex = filesToIndex
179179
self.buildSystemManager = buildSystemManager
@@ -182,7 +182,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
182182
self.indexFilesWithUpToDateUnit = indexFilesWithUpToDateUnit
183183
self.logMessageToIndexLog = logMessageToIndexLog
184184
self.timeout = timeout
185-
self.testHooks = testHooks
185+
self.hooks = hooks
186186
}
187187

188188
package func execute() async {
@@ -192,7 +192,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
192192
await withLoggingSubsystemAndScope(subsystem: indexLoggingSubsystem, scope: "update-indexstore-\(id % 100)") {
193193
let startDate = Date()
194194

195-
await testHooks.updateIndexStoreTaskDidStart?(self)
195+
await hooks.updateIndexStoreTaskDidStart?(self)
196196

197197
let filesToIndexDescription = filesToIndex.map {
198198
$0.file.sourceFile.fileURL?.lastPathComponent ?? $0.file.sourceFile.stringValue
@@ -207,7 +207,7 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
207207
for file in filesToIndex {
208208
await updateIndexStore(forSingleFile: file.file, in: file.target)
209209
}
210-
await testHooks.updateIndexStoreTaskDidFinish?(self)
210+
await hooks.updateIndexStoreTaskDidFinish?(self)
211211
logger.log(
212212
"Finished updating index store in \(Date().timeIntervalSince(startDate) * 1000, privacy: .public)ms: \(filesToIndexDescription)"
213213
)

Sources/SourceKitLSP/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_library(SourceKitLSP STATIC
33
CapabilityRegistry.swift
44
DocumentManager.swift
55
DocumentSnapshot+FromFileContents.swift
6+
Hooks.swift
67
IndexProgressManager.swift
78
IndexStoreDB+MainFilesProvider.swift
89
LanguageServerType.swift
@@ -16,7 +17,6 @@ add_library(SourceKitLSP STATIC
1617
SourceKitLSPServer.swift
1718
SymbolLocation+DocumentURI.swift
1819
TestDiscovery.swift
19-
TestHooks.swift
2020
TextEdit+IsNoop.swift
2121
Workspace.swift
2222
)

Sources/SourceKitLSP/TestHooks.swift renamed to Sources/SourceKitLSP/Hooks.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import struct TSCBasic.RelativePath
3232

3333
/// Closures can be used to inspect or modify internal behavior in SourceKit-LSP.
3434
public struct Hooks: Sendable {
35-
package var indexTestHooks: IndexTestHooks
35+
package var indexHooks: IndexHooks
3636

3737
package var buildSystemHooks: BuildSystemHooks
3838

@@ -42,15 +42,15 @@ public struct Hooks: Sendable {
4242
package var preHandleRequest: (@Sendable (any RequestType) async -> Void)?
4343

4444
public init() {
45-
self.init(indexTestHooks: IndexTestHooks(), buildSystemHooks: BuildSystemHooks())
45+
self.init(indexHooks: IndexHooks(), buildSystemHooks: BuildSystemHooks())
4646
}
4747

4848
package init(
49-
indexTestHooks: IndexTestHooks = IndexTestHooks(),
49+
indexHooks: IndexHooks = IndexHooks(),
5050
buildSystemHooks: BuildSystemHooks = BuildSystemHooks(),
5151
preHandleRequest: (@Sendable (any RequestType) async -> Void)? = nil
5252
) {
53-
self.indexTestHooks = indexTestHooks
53+
self.indexHooks = indexHooks
5454
self.buildSystemHooks = buildSystemHooks
5555
self.preHandleRequest = preHandleRequest
5656
}

0 commit comments

Comments
 (0)