Files
sourcekit-lsp/Sources/SourceKitLSP/SourceKitIndexDelegate.swift
Alex Hoppen a6c291b84e Do not block SourceKit-LSP functionality when a build server takes long to initialize
We previously waited for the initialization response from the build server during the creation of a `Workspace` so that we could create a `SemanticIndexManager` with the index store path etc. that was returned by the `build/initialize` response. This caused all functionality (including syntactic) of SourceKit-LSP to be blocked until the build server was initialized.

Change the computation of the `SemanticIndexManager` and related types to happen in the background so that we can provide functionality that doesn’t rely on the build server immediately.

Fixes #2304
2025-09-29 13:02:08 +01:00

61 lines
2.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Dispatch
import IndexStoreDB
import LanguageServerProtocolExtensions
import SKLogging
import SwiftExtensions
/// `IndexDelegate` for the SourceKit workspace.
actor SourceKitIndexDelegate: IndexDelegate {
/// Registered `MainFilesDelegate`s to notify when main files change.
var mainFilesChangedCallback: @Sendable () async -> Void
/// The count of pending unit events. Whenever this transitions to 0, it represents a time where
/// the index finished processing known events. Of course, that may have already changed by the
/// time we are notified.
let pendingUnitCount = AtomicInt32(initialValue: 0)
package init(mainFilesChangedCallback: @escaping @Sendable () async -> Void) {
self.mainFilesChangedCallback = mainFilesChangedCallback
}
nonisolated package func processingAddedPending(_ count: Int) {
pendingUnitCount.value += Int32(count)
}
nonisolated package func processingCompleted(_ count: Int) {
pendingUnitCount.value -= Int32(count)
if pendingUnitCount.value == 0 {
Task {
await indexChanged()
}
}
if pendingUnitCount.value < 0 {
// Technically this is not data race safe because `pendingUnitCount` might change between the check and us setting
// it to 0. But then, this should never happen anyway, so it's fine.
logger.fault("pendingUnitCount dropped below zero: \(self.pendingUnitCount.value)")
pendingUnitCount.value = 0
Task {
await indexChanged()
}
}
}
private func indexChanged() async {
logger.debug("IndexStoreDB changed")
await mainFilesChangedCallback()
}
}