Skip to content

Commit 2d27d57

Browse files
committed
Extract indexTaskDidFinish in SourceKitLSPServer.Options into a TestHooks struct
1 parent c01c676 commit 2d27d57

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

Sources/SourceKitLSP/SourceKitLSPServer+Options.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ extension SourceKitLSPServer {
2222

2323
/// Configuration options for the SourceKitServer.
2424
public struct Options: Sendable {
25+
/// Callbacks that allow inspection of internal state modifications during testing.
26+
public struct TestHooks: Sendable {
27+
/// A callback that is called when an index task finishes.
28+
public var indexTaskDidFinish: (@Sendable () -> Void)?
29+
30+
public init(indexTaskDidFinish: (@Sendable () -> Void)? = nil) {
31+
self.indexTaskDidFinish = indexTaskDidFinish
32+
}
33+
}
2534

2635
/// Additional compiler flags (e.g. `-Xswiftc` for SwiftPM projects) and other build-related
2736
/// configuration.
@@ -49,10 +58,7 @@ extension SourceKitLSPServer {
4958
/// notification when running unit tests.
5059
public var swiftPublishDiagnosticsDebounceDuration: TimeInterval
5160

52-
/// A callback that is called when an index task finishes.
53-
///
54-
/// Intended for testing purposes.
55-
public var indexTaskDidFinish: (@Sendable () -> Void)?
61+
public var testHooks: TestHooks
5662

5763
public init(
5864
buildSetup: BuildSetup = .default,
@@ -61,7 +67,8 @@ extension SourceKitLSPServer {
6167
indexOptions: IndexOptions = .init(),
6268
completionOptions: SKCompletionOptions = .init(),
6369
generatedInterfacesPath: AbsolutePath = defaultDirectoryForGeneratedInterfaces,
64-
swiftPublishDiagnosticsDebounceDuration: TimeInterval = 2 /* 2s */
70+
swiftPublishDiagnosticsDebounceDuration: TimeInterval = 2, /* 2s */
71+
testHooks: TestHooks = TestHooks()
6572
) {
6673
self.buildSetup = buildSetup
6774
self.clangdOptions = clangdOptions
@@ -70,6 +77,7 @@ extension SourceKitLSPServer {
7077
self.completionOptions = completionOptions
7178
self.generatedInterfacesPath = generatedInterfacesPath
7279
self.swiftPublishDiagnosticsDebounceDuration = swiftPublishDiagnosticsDebounceDuration
80+
self.testHooks = testHooks
7381
}
7482
}
7583
}

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ extension SourceKitLSPServer {
12111211
logger.log("Cannot open workspace before server is initialized")
12121212
return nil
12131213
}
1214-
let indexTaskDidFinishCallback = options.indexTaskDidFinish
1214+
let indexTaskDidFinishCallback = options.testHooks.indexTaskDidFinish
12151215
var options = self.options
12161216
options.buildSetup = self.options.buildSetup.merging(buildSetup(for: workspaceFolder))
12171217
return try? await Workspace(
@@ -1286,7 +1286,7 @@ extension SourceKitLSPServer {
12861286
if self.workspaces.isEmpty {
12871287
logger.error("no workspace found")
12881288

1289-
let indexTaskDidFinishCallback = self.options.indexTaskDidFinish
1289+
let indexTaskDidFinishCallback = self.options.testHooks.indexTaskDidFinish
12901290
let workspace = await Workspace(
12911291
documentManager: self.documentManager,
12921292
rootUri: req.rootURI,

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ final class BackgroundIndexingTests: XCTestCase {
165165

166166
func testBackgroundIndexingHappensWithLowPriority() async throws {
167167
var serverOptions = backgroundIndexingOptions
168-
serverOptions.indexTaskDidFinish = {
168+
serverOptions.testHooks.indexTaskDidFinish = {
169169
XCTAssert(
170170
Task.currentPriority == .low,
171171
"An index task ran with priority \(Task.currentPriority)"

0 commit comments

Comments
 (0)