Skip to content

Commit 3278ff0

Browse files
committed
Enable per-workspace configuration of default workspace type
1 parent 17d7d97 commit 3278ff0

File tree

8 files changed

+82
-63
lines changed

8 files changed

+82
-63
lines changed

Sources/LanguageServerProtocol/SupportTypes/WorkspaceFolder.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@ public enum BuildConfiguration: Hashable, Codable {
1818
case release
1919
}
2020

21+
/// The type of workspace; default workspace type selection logic can be overridden.
22+
///
23+
/// **(LSP Extension)**
24+
public enum WorkspaceType: Hashable, Codable {
25+
case buildserver, compdb, swiftpm
26+
}
27+
2128
/// Build settings that should be used for a workspace.
2229
///
2330
/// **(LSP Extension)**
2431
public struct WorkspaceBuildSetup: Hashable, Codable {
2532
/// The configuration that the workspace should be built in.
2633
public let buildConfiguration: BuildConfiguration?
2734

35+
/// The default workspace type to use for this workspace.
36+
public let defaultWorkspaceType: WorkspaceType?
37+
2838
/// The build directory for the workspace.
2939
public let scratchPath: DocumentURI?
3040

@@ -42,13 +52,15 @@ public struct WorkspaceBuildSetup: Hashable, Codable {
4252

4353
public init(
4454
buildConfiguration: BuildConfiguration? = nil,
55+
defaultWorkspaceType: WorkspaceType? = nil,
4556
scratchPath: DocumentURI? = nil,
4657
cFlags: [String]? = nil,
4758
cxxFlags: [String]? = nil,
4859
linkerFlags: [String]? = nil,
4960
swiftFlags: [String]? = nil
5061
) {
5162
self.buildConfiguration = buildConfiguration
63+
self.defaultWorkspaceType = defaultWorkspaceType
5264
self.scratchPath = scratchPath
5365
self.cFlags = cFlags
5466
self.cxxFlags = cxxFlags

Sources/SKCore/BuildSetup.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,26 @@ public struct BuildSetup {
2121
/// Default configuration
2222
public static let `default` = BuildSetup(
2323
configuration: nil,
24+
defaultWorkspaceType: nil,
2425
path: nil,
2526
flags: BuildFlags()
2627
)
2728

2829
/// Build configuration (debug|release).
2930
public var configuration: BuildConfiguration?
3031

32+
/// Default workspace type (buildserver|compdb|swiftpm). Overrides workspace type selection logic.
33+
public var defaultWorkspaceType: WorkspaceType?
34+
3135
/// Build artifacts directory path. If nil, the build system may choose a default value.
3236
public var path: AbsolutePath?
3337

3438
/// Additional build flags
3539
public var flags: BuildFlags
3640

37-
public init(configuration: BuildConfiguration?, path: AbsolutePath?, flags: BuildFlags) {
41+
public init(configuration: BuildConfiguration?, defaultWorkspaceType: WorkspaceType?, path: AbsolutePath?, flags: BuildFlags) {
3842
self.configuration = configuration
43+
self.defaultWorkspaceType = defaultWorkspaceType
3944
self.path = path
4045
self.flags = flags
4146
}
@@ -49,6 +54,7 @@ public struct BuildSetup {
4954
flags = flags.merging(other.flags)
5055
return BuildSetup(
5156
configuration: other.configuration ?? self.configuration,
57+
defaultWorkspaceType: other.defaultWorkspaceType ?? self.defaultWorkspaceType,
5258
path: other.path ?? self.path,
5359
flags: flags
5460
)

Sources/SKSupport/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_library(SKSupport STATIC
1212
Random.swift
1313
Result.swift
1414
ThreadSafeBox.swift
15+
WorkspaceType.swift
1516
)
1617
set_target_properties(SKSupport PROPERTIES
1718
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

Sources/SKSupport/WorkspaceType.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
public enum WorkspaceType: String {
14+
case buildserver
15+
case compdb
16+
case swiftpm
17+
}

Sources/SourceKitLSP/SourceKitServer+Options.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ extension SourceKitServer {
3030
/// Additional arguments to pass to `clangd` on the command-line.
3131
public var clangdOptions: [String]
3232

33-
/// Overrides default workspace type selection.
34-
public var workspaceType: WorkspaceType?
35-
3633
/// Additional paths to search for a compilation database, relative to a workspace root.
3734
public var compilationDatabaseSearchPaths: [RelativePath]
3835

@@ -55,7 +52,6 @@ extension SourceKitServer {
5552
public init(
5653
buildSetup: BuildSetup = .default,
5754
clangdOptions: [String] = [],
58-
workspaceType: WorkspaceType? = nil,
5955
compilationDatabaseSearchPaths: [RelativePath] = [],
6056
indexOptions: IndexOptions = .init(),
6157
completionOptions: SKCompletionOptions = .init(),
@@ -64,7 +60,6 @@ extension SourceKitServer {
6460
) {
6561
self.buildSetup = buildSetup
6662
self.clangdOptions = clangdOptions
67-
self.workspaceType = workspaceType
6863
self.compilationDatabaseSearchPaths = compilationDatabaseSearchPaths
6964
self.indexOptions = indexOptions
7065
self.completionOptions = completionOptions

Sources/SourceKitLSP/SourceKitServer.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,17 @@ extension LanguageServerProtocol.BuildConfiguration {
974974
}
975975
}
976976

977+
extension LanguageServerProtocol.WorkspaceType {
978+
/// Convert `LanguageServerProtocol.WorkspaceType` to `SkSupport.WorkspaceType`.
979+
var workspaceType: SKSupport.WorkspaceType {
980+
switch self {
981+
case .buildserver: return .buildserver
982+
case .compdb: return .compdb
983+
case .swiftpm: return .swiftpm
984+
}
985+
}
986+
}
987+
977988
// MARK: - Request and notification handling
978989

979990
extension SourceKitServer {
@@ -991,6 +1002,7 @@ extension SourceKitServer {
9911002
}
9921003
return SKCore.BuildSetup(
9931004
configuration: buildParams?.buildConfiguration?.configuration,
1005+
defaultWorkspaceType: buildParams?.defaultWorkspaceType?.workspaceType,
9941006
path: scratchPath,
9951007
flags: BuildFlags(
9961008
cCompilerFlags: buildParams?.cFlags ?? [],
@@ -1015,7 +1027,6 @@ extension SourceKitServer {
10151027
capabilityRegistry: capabilityRegistry,
10161028
toolchainRegistry: self.toolchainRegistry,
10171029
buildSetup: self.options.buildSetup.merging(workspaceBuildSetup),
1018-
workspaceType: self.options.workspaceType,
10191030
compilationDatabaseSearchPaths: self.options.compilationDatabaseSearchPaths,
10201031
indexOptions: self.options.indexOptions,
10211032
reloadPackageStatusCallback: { status in

Sources/SourceKitLSP/Workspace.swift

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ fileprivate func firstNonNil<T>(
3838
return try await defaultValue()
3939
}
4040

41-
public enum WorkspaceType: String {
42-
case buildserver, compdb, swiftpm
43-
}
44-
4541
/// Represents the configuration and state of a project or combination of projects being worked on
4642
/// together.
4743
///
@@ -107,64 +103,45 @@ public final class Workspace {
107103
capabilityRegistry: CapabilityRegistry,
108104
toolchainRegistry: ToolchainRegistry,
109105
buildSetup: BuildSetup,
110-
workspaceType: WorkspaceType?,
111106
compilationDatabaseSearchPaths: [RelativePath],
112107
indexOptions: IndexOptions = IndexOptions(),
113108
reloadPackageStatusCallback: @escaping (ReloadPackageStatus) async -> Void
114109
) async throws {
115110
var buildSystem: BuildSystem? = nil
116-
if let rootUrl = rootUri.fileURL, let rootPath = try? AbsolutePath(validating: rootUrl.path) {
117-
if let workspaceType = workspaceType {
118-
switch workspaceType {
119-
case WorkspaceType.swiftpm:
120-
if let swiftpm = await SwiftPMWorkspace(
121-
url: rootUrl,
122-
toolchainRegistry: toolchainRegistry,
123-
buildSetup: buildSetup,
124-
reloadPackageStatusCallback: reloadPackageStatusCallback
125-
) {
126-
buildSystem = swiftpm
127-
} else {
128-
logger.error(
129-
"Could not set up a swiftpm build system for workspace at '\(rootUri.forLogging)'"
130-
)
131-
buildSystem = nil
132-
}
133-
case WorkspaceType.buildserver:
134-
if let buildServer = await BuildServerBuildSystem(projectRoot: rootPath, buildSetup: buildSetup) {
135-
buildSystem = buildServer
136-
} else {
137-
logger.error(
138-
"Could not set up a build server build system for workspace at '\(rootUri.forLogging)'"
139-
)
140-
buildSystem = nil
141-
}
142-
case WorkspaceType.compdb:
143-
if let compdb = CompilationDatabaseBuildSystem(
144-
projectRoot: rootPath,
145-
searchPaths: compilationDatabaseSearchPaths
146-
) {
147-
buildSystem = compdb
148-
} else {
149-
logger.error(
150-
"Could not set up a compilation database build system for workspace at '\(rootUri.forLogging)'"
151-
)
152-
buildSystem = nil
153-
}
154-
}
155-
} else if let buildServer = await BuildServerBuildSystem(projectRoot: rootPath, buildSetup: buildSetup) {
156-
buildSystem = buildServer
157-
} else if let swiftpm = await SwiftPMWorkspace(
111+
112+
func buildSwiftPMWorkspace(rootUrl: URL) async -> SwiftPMWorkspace? {
113+
return await SwiftPMWorkspace(
158114
url: rootUrl,
159115
toolchainRegistry: toolchainRegistry,
160116
buildSetup: buildSetup,
161117
reloadPackageStatusCallback: reloadPackageStatusCallback
162-
) {
163-
buildSystem = swiftpm
164-
} else if let compdb = CompilationDatabaseBuildSystem(
118+
)
119+
}
120+
121+
func buildCompDBWorkspace(rootPath: AbsolutePath) -> CompilationDatabaseBuildSystem? {
122+
return CompilationDatabaseBuildSystem(
165123
projectRoot: rootPath,
166124
searchPaths: compilationDatabaseSearchPaths
167-
) {
125+
)
126+
}
127+
128+
func buildBuildServerWorkspace(rootPath: AbsolutePath) async -> BuildServerBuildSystem? {
129+
return await BuildServerBuildSystem(projectRoot: rootPath, buildSetup: buildSetup)
130+
}
131+
132+
if let rootUrl = rootUri.fileURL, let rootPath = try? AbsolutePath(validating: rootUrl.path) {
133+
if let defaultWorkspaceType = buildSetup.defaultWorkspaceType, let defaultBuildSystem: BuildSystem? =
134+
switch defaultWorkspaceType {
135+
case .buildserver: await buildBuildServerWorkspace(rootPath: rootPath)
136+
case .compdb: buildCompDBWorkspace(rootPath: rootPath)
137+
case .swiftpm: await buildSwiftPMWorkspace(rootUrl: rootUrl)
138+
} {
139+
buildSystem = defaultBuildSystem
140+
} else if let buildServer = await buildBuildServerWorkspace(rootPath: rootPath) {
141+
buildSystem = buildServer
142+
} else if let swiftpm = await buildSwiftPMWorkspace(rootUrl: rootUrl) {
143+
buildSystem = swiftpm
144+
} else if let compdb = buildCompDBWorkspace(rootPath: rootPath) {
168145
buildSystem = compdb
169146
} else {
170147
logger.error(

Sources/sourcekit-lsp/SourceKitLSP.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ extension SKSupport.BuildConfiguration: @retroactive ExpressibleByArgument {}
9393
#endif
9494

9595
#if swift(<5.10)
96-
extension WorkspaceType: ExpressibleByArgument {}
96+
extension SKSupport.WorkspaceType: ExpressibleByArgument {}
9797
#else
98-
extension WorkspaceType: @retroactive ExpressibleByArgument {}
98+
extension SKSupport.WorkspaceType: @retroactive ExpressibleByArgument {}
9999
#endif
100100

101101
@main
@@ -176,10 +176,10 @@ struct SourceKitLSP: ParsableCommand {
176176
var indexPrefixMappings = [PathPrefixMapping]()
177177

178178
@Option(
179-
name: .customLong("workspace-type"),
179+
name: .customLong("default-workspace-type"),
180180
help: "Override default workspace type selection; one of 'swiftpm', 'compdb', or 'buildserver'"
181181
)
182-
var workspaceType: WorkspaceType?
182+
var defaultWorkspaceType: SKSupport.WorkspaceType?
183183

184184
@Option(
185185
name: .customLong("compilation-db-search-path"),
@@ -203,13 +203,13 @@ struct SourceKitLSP: ParsableCommand {
203203
var serverOptions = SourceKitServer.Options()
204204

205205
serverOptions.buildSetup.configuration = buildConfiguration
206+
serverOptions.buildSetup.defaultWorkspaceType = defaultWorkspaceType
206207
serverOptions.buildSetup.path = scratchPath
207208
serverOptions.buildSetup.flags.cCompilerFlags = buildFlagsCc
208209
serverOptions.buildSetup.flags.cxxCompilerFlags = buildFlagsCxx
209210
serverOptions.buildSetup.flags.linkerFlags = buildFlagsLinker
210211
serverOptions.buildSetup.flags.swiftCompilerFlags = buildFlagsSwift
211212
serverOptions.clangdOptions = clangdOptions
212-
serverOptions.workspaceType = workspaceType
213213
serverOptions.compilationDatabaseSearchPaths = compilationDatabaseSearchPaths
214214
serverOptions.indexOptions.indexStorePath = indexStorePath
215215
serverOptions.indexOptions.indexDatabasePath = indexDatabasePath

0 commit comments

Comments
 (0)