Skip to content

Commit 7f74d4a

Browse files
committed
[build-system] Fold ExternalWorkspace into BuildSystem
Simplifies clients to only need to care about one thing, and makes it easier for BuildSystems to grow new functionality.
1 parent 63b9b3d commit 7f74d4a

8 files changed

+42
-55
lines changed

Sources/SKCore/BuildSettingsProviderList.swift

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

13+
import Basic
1314
import LanguageServerProtocol
1415

1516
/// Provides build settings from a list of providers in priority order.
16-
public final class BuildSettingsProviderList: BuildSystem {
17+
public final class BuildSettingsProviderList {
1718

1819
/// The build settings providers to try (in order).
1920
public var providers: [BuildSystem] = [
2021
FallbackBuildSettingsProvider()
2122
]
2223

2324
public init() {}
25+
}
26+
27+
extension BuildSettingsProviderList: BuildSystem {
28+
29+
public var indexStorePath: AbsolutePath? { return providers.first?.indexStorePath }
30+
31+
public var indexDatabasePath: AbsolutePath? { return providers.first?.indexDatabasePath }
2432

2533
public func settings(for url: URL, _ language: Language) -> FileBuildSettings? {
2634
for provider in providers {

Sources/SKCore/BuildSystem.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,24 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import LanguageServerProtocol
14+
import Basic
1415

15-
/// Provider of build settings.
16+
/// Provider of FileBuildSettings and other build-related information.
17+
///
18+
/// The primary role of the build system is to answer queries for FileBuildSettings and (TODO) to
19+
/// notify clients when they change. The BuildSystem is also the source of related informatino,
20+
/// such as where the index datastore is located.
21+
///
22+
/// For example, a SwiftPMWorkspace provides compiler arguments for the files contained in a
23+
/// SwiftPM package root directory.
1624
public protocol BuildSystem {
1725

26+
/// The path to the raw index store data, if any.
27+
var indexStorePath: AbsolutePath? { get }
28+
29+
/// The path to put the index database, if any.
30+
var indexDatabasePath: AbsolutePath? { get }
31+
1832
/// Returns the settings for the given url and language mode, if known.
1933
func settings(for: URL, _ language: Language) -> FileBuildSettings?
2034

Sources/SKCore/CompilationDatabaseBuildSystem.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import LanguageServerProtocol
1717
///
1818
/// Provides build settings from a `CompilationDatabase` found by searching a project. For now, only
1919
/// one compilation database, located at the project root.
20-
public final class CompilationDatabaseBuildSystem: BuildSystem {
20+
public final class CompilationDatabaseBuildSystem {
2121

2222
/// The compilation database.
2323
var compdb: CompilationDatabase? = nil
@@ -30,6 +30,13 @@ public final class CompilationDatabaseBuildSystem: BuildSystem {
3030
self.compdb = tryLoadCompilationDatabase(directory: path, fileSystem: fileSystem)
3131
}
3232
}
33+
}
34+
35+
extension CompilationDatabaseBuildSystem: BuildSystem {
36+
37+
// FIXME: derive from the compiler arguments.
38+
public var indexStorePath: AbsolutePath? { return nil }
39+
public var indexDatabasePath: AbsolutePath? { return nil }
3340

3441
public func settings(for url: URL, _ language: Language) -> FileBuildSettings? {
3542
guard let db = database(for: url),

Sources/SKCore/ExternalWorkspace.swift

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

Sources/SKCore/FallbackBuildSettingsProvider.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public final class FallbackBuildSettingsProvider: BuildSystem {
2626
return nil
2727
}()
2828

29+
public var indexStorePath: AbsolutePath? { return nil }
30+
31+
public var indexDatabasePath: AbsolutePath? { return nil }
32+
2933
public func settings(for url: URL, _ language: Language) -> FileBuildSettings? {
3034
guard let path = try? AbsolutePath(validating: url.path) else {
3135
return nil

Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ public final class SwiftPMWorkspace {
162162
}
163163
}
164164

165-
extension SwiftPMWorkspace: ExternalWorkspace, BuildSystem {
166-
167-
public var buildSystem: BuildSystem { return self }
165+
extension SwiftPMWorkspace: BuildSystem {
168166

169167
public var buildPath: AbsolutePath {
170168
return buildParameters.buildPath

Sources/SourceKit/SourceKitServer.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ extension SourceKitServer {
228228
self.workspace = Workspace(
229229
rootPath: nil,
230230
clientCapabilities: req.params.capabilities,
231-
external: nil,
232231
buildSettings: BuildSettingsProviderList(),
233232
index: nil
234233
)

Sources/SourceKit/Workspace.swift

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,15 @@ import SKSwiftPMWorkspace
2323
/// In LSP, this represents the per-workspace state that is typically only available after the
2424
/// "initialize" request has been made.
2525
///
26-
/// Typically a workspace is contained in a root directory, and may be represented by an
27-
/// `ExternalWorkspace` if this workspace is part of a workspace in another tool such as a swiftpm
28-
/// package.
26+
/// Typically a workspace is contained in a root directory.
2927
public final class Workspace {
3028

3129
/// The root directory of the workspace.
3230
public let rootPath: AbsolutePath?
3331

3432
public let clientCapabilities: ClientCapabilities
3533

36-
/// The external workspace connection, if any.
37-
public let external: ExternalWorkspace?
38-
3934
/// The build settings provider to use for documents in this workspace.
40-
///
41-
/// If `external` is not `nil`, this will typically include `external.buildSystem`. It may also
42-
/// provide settings for files outside the workspace using additional providers.
4335
public let buildSettings: BuildSystem
4436

4537
/// The source code index, if available.
@@ -54,13 +46,11 @@ public final class Workspace {
5446
public init(
5547
rootPath: AbsolutePath?,
5648
clientCapabilities: ClientCapabilities,
57-
external: ExternalWorkspace?,
5849
buildSettings: BuildSystem,
5950
index: IndexStoreDB?)
6051
{
6152
self.rootPath = rootPath
6253
self.clientCapabilities = clientCapabilities
63-
self.external = external
6454
self.buildSettings = buildSettings
6555
self.index = index
6656
}
@@ -79,21 +69,17 @@ public final class Workspace {
7969

8070
self.rootPath = try AbsolutePath(validating: url.path)
8171
self.clientCapabilities = clientCapabilities
82-
self.external = SwiftPMWorkspace(url: url, toolchainRegistry: toolchainRegistry)
83-
8472
let settings = BuildSettingsProviderList()
8573
self.buildSettings = settings
8674

8775
settings.providers.insert(CompilationDatabaseBuildSystem(projectRoot: rootPath), at: 0)
8876

89-
guard let external = self.external else {
90-
return
77+
if let swiftpm = SwiftPMWorkspace(url: url, toolchainRegistry: toolchainRegistry) {
78+
settings.providers.insert(swiftpm, at: 0)
9179
}
9280

93-
settings.providers.insert(external.buildSystem, at: 0)
94-
95-
if let storePath = external.indexStorePath,
96-
let dbPath = external.indexDatabasePath,
81+
if let storePath = buildSettings.indexStorePath,
82+
let dbPath = buildSettings.indexDatabasePath,
9783
let libPath = toolchainRegistry.default?.libIndexStore
9884
{
9985
do {

0 commit comments

Comments
 (0)