1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 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 IndexStoreDB
import LSPLogging
import LanguageServerProtocol
import SKCore
import SKSupport
import SemanticIndex
import SwiftExtensions
import struct TSCBasic.AbsolutePath
import struct TSCBasic.RelativePath
/// Same as `??` but allows the right-hand side of the operator to 'await'.
fileprivate func firstNonNil<T>(_ optional: T?, _ defaultValue: @autoclosure () async throws -> T) async rethrows -> T {
if let optional {
return optional
}
return try await defaultValue()
}
fileprivate func firstNonNil<T>(
_ optional: T?,
_ defaultValue: @autoclosure () async throws -> T?
) async rethrows -> T? {
if let optional {
return optional
}
return try await defaultValue()
}
/// Represents the configuration and state of a project or combination of projects being worked on
/// together.
///
/// In LSP, this represents the per-workspace state that is typically only available after the
/// "initialize" request has been made.
///
/// Typically a workspace is contained in a root directory.
public final class Workspace: Sendable {
/// The root directory of the workspace.
public let rootUri: DocumentURI?
/// Tracks dynamically registered server capabilities as well as the client's capabilities.
public let capabilityRegistry: CapabilityRegistry
/// The build system manager to use for documents in this workspace.
public let buildSystemManager: BuildSystemManager
let options: SourceKitLSPOptions
/// The source code index, if available.
///
/// Usually a checked index (retrieved using `index(checkedFor:)`) should be used instead of the unchecked index.
private let _uncheckedIndex: ThreadSafeBox<UncheckedIndex?>
public var uncheckedIndex: UncheckedIndex? {
return _uncheckedIndex.value
}
/// The index that syntactically scans the workspace for tests.
let syntacticTestIndex = SyntacticTestIndex()
/// Documents open in the SourceKitLSPServer. This may include open documents from other workspaces.
private let documentManager: DocumentManager
/// Language service for an open document, if available.
let documentService: ThreadSafeBox<[DocumentURI: LanguageService]> = ThreadSafeBox(initialValue: [:])
/// The `SemanticIndexManager` that keeps track of whose file's index is up-to-date in the workspace and schedules
/// indexing and preparation tasks for files with out-of-date index.
///
/// `nil` if background indexing is not enabled.
let semanticIndexManager: SemanticIndexManager?
init(
documentManager: DocumentManager,
rootUri: DocumentURI?,
capabilityRegistry: CapabilityRegistry,
toolchainRegistry: ToolchainRegistry,
options: SourceKitLSPOptions,
testHooks: TestHooks,
underlyingBuildSystem: BuildSystem?,
index uncheckedIndex: UncheckedIndex?,
indexDelegate: SourceKitIndexDelegate?,
indexTaskScheduler: TaskScheduler<AnyIndexTaskDescription>,
logMessageToIndexLog: @escaping @Sendable (_ taskID: IndexTaskID, _ message: String) -> Void,
indexTasksWereScheduled: @escaping @Sendable (Int) -> Void,
indexProgressStatusDidChange: @escaping @Sendable () -> Void
) async {
self.documentManager = documentManager
self.rootUri = rootUri
self.capabilityRegistry = capabilityRegistry
self.options = options
self._uncheckedIndex = ThreadSafeBox(initialValue: uncheckedIndex)
self.buildSystemManager = await BuildSystemManager(
buildSystem: underlyingBuildSystem,
fallbackBuildSystem: FallbackBuildSystem(options: options.fallbackBuildSystem),
mainFilesProvider: uncheckedIndex,
toolchainRegistry: toolchainRegistry
)
if options.backgroundIndexingOrDefault, let uncheckedIndex, await buildSystemManager.supportsPreparation {
self.semanticIndexManager = SemanticIndexManager(
index: uncheckedIndex,
buildSystemManager: buildSystemManager,
updateIndexStoreTimeout: options.index.updateIndexStoreTimeoutOrDefault,
testHooks: testHooks.indexTestHooks,
indexTaskScheduler: indexTaskScheduler,
logMessageToIndexLog: logMessageToIndexLog,
indexTasksWereScheduled: indexTasksWereScheduled,
indexProgressStatusDidChange: indexProgressStatusDidChange
)
} else {
self.semanticIndexManager = nil
}
await indexDelegate?.addMainFileChangedCallback { [weak self] in
await self?.buildSystemManager.mainFilesChanged()
}
await underlyingBuildSystem?.addSourceFilesDidChangeCallback { [weak self] in
guard let self else {
return
}
await self.syntacticTestIndex.listOfTestFilesDidChange(self.buildSystemManager.testFiles())
}
// Trigger an initial population of `syntacticTestIndex`.
await syntacticTestIndex.listOfTestFilesDidChange(buildSystemManager.testFiles())
if let semanticIndexManager {
await semanticIndexManager.scheduleBuildGraphGenerationAndBackgroundIndexAllFiles()
}
}
/// Creates a workspace for a given root `DocumentURI`, inferring the `ExternalWorkspace` if possible.
///
/// - Parameters:
/// - url: The root directory of the workspace, which must be a valid path.
/// - clientCapabilities: The client capabilities provided during server initialization.
/// - toolchainRegistry: The toolchain registry.
convenience init(
documentManager: DocumentManager,
rootUri: DocumentURI,
capabilityRegistry: CapabilityRegistry,
buildSystem: BuildSystem?,
toolchainRegistry: ToolchainRegistry,
options: SourceKitLSPOptions,
testHooks: TestHooks,
indexTaskScheduler: TaskScheduler<AnyIndexTaskDescription>,
logMessageToIndexLog: @escaping @Sendable (_ taskID: IndexTaskID, _ message: String) -> Void,
indexTasksWereScheduled: @Sendable @escaping (Int) -> Void,
indexProgressStatusDidChange: @Sendable @escaping () -> Void
) async throws {
var index: IndexStoreDB? = nil
var indexDelegate: SourceKitIndexDelegate? = nil
let indexOptions = options.index
if let storePath = await firstNonNil(
AbsolutePath(validatingOrNil: indexOptions.indexStorePath),
await buildSystem?.indexStorePath
),
let dbPath = await firstNonNil(
AbsolutePath(validatingOrNil: indexOptions.indexDatabasePath),
await buildSystem?.indexDatabasePath
),
let libPath = await toolchainRegistry.default?.libIndexStore
{
do {
let lib = try IndexStoreLibrary(dylibPath: libPath.pathString)
indexDelegate = SourceKitIndexDelegate()
let prefixMappings =
await firstNonNil(
indexOptions.indexPrefixMap?.map { PathPrefixMapping(original: $0.key, replacement: $0.value) },
await buildSystem?.indexPrefixMappings
) ?? []
index = try IndexStoreDB(
storePath: storePath.pathString,
databasePath: dbPath.pathString,
library: lib,
delegate: indexDelegate,
prefixMappings: prefixMappings.map { PathMapping(original: $0.original, replacement: $0.replacement) }
)
logger.debug("Opened IndexStoreDB at \(dbPath) with store path \(storePath)")
} catch {
logger.error("Failed to open IndexStoreDB: \(error.localizedDescription)")
}
}
await self.init(
documentManager: documentManager,
rootUri: rootUri,
capabilityRegistry: capabilityRegistry,
toolchainRegistry: toolchainRegistry,
options: options,
testHooks: testHooks,
underlyingBuildSystem: buildSystem,
index: UncheckedIndex(index),
indexDelegate: indexDelegate,
indexTaskScheduler: indexTaskScheduler,
logMessageToIndexLog: logMessageToIndexLog,
indexTasksWereScheduled: indexTasksWereScheduled,
indexProgressStatusDidChange: indexProgressStatusDidChange
)
}
@_spi(Testing) public static func forTesting(
toolchainRegistry: ToolchainRegistry,
options: SourceKitLSPOptions,
testHooks: TestHooks,
underlyingBuildSystem: BuildSystem,
indexTaskScheduler: TaskScheduler<AnyIndexTaskDescription>
) async -> Workspace {
return await Workspace(
documentManager: DocumentManager(),
rootUri: nil,
capabilityRegistry: CapabilityRegistry(clientCapabilities: ClientCapabilities()),
toolchainRegistry: toolchainRegistry,
options: options,
testHooks: testHooks,
underlyingBuildSystem: underlyingBuildSystem,
index: nil,
indexDelegate: nil,
indexTaskScheduler: indexTaskScheduler,
logMessageToIndexLog: { _, _ in },
indexTasksWereScheduled: { _ in },
indexProgressStatusDidChange: {}
)
}
/// Returns a `CheckedIndex` that verifies that all the returned entries are up-to-date with the given
/// `IndexCheckLevel`.
func index(checkedFor checkLevel: IndexCheckLevel) -> CheckedIndex? {
return _uncheckedIndex.value?.checked(for: checkLevel)
}
/// Write the index to disk.
///
/// After this method is called, the workspace will no longer have an index associated with it. It should only be
/// called when SourceKit-LSP shuts down.
func closeIndex() {
_uncheckedIndex.value = nil
}
public func filesDidChange(_ events: [FileEvent]) async {
await buildSystemManager.filesDidChange(events)
await syntacticTestIndex.filesDidChange(events)
await semanticIndexManager?.filesDidChange(events)
}
}
/// Wrapper around a workspace that isn't being retained.
struct WeakWorkspace {
weak var value: Workspace?
init(_ value: Workspace? = nil) {
self.value = value
}
}
|