Skip to content

Commit d931806

Browse files
authored
Merge pull request #988 from z2oh/configure-workspace-type
Allow configuring workspace type via flag
2 parents ce9fdf9 + c31417a commit d931806

File tree

9 files changed

+102
-9
lines changed

9 files changed

+102
-9
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, compilationDatabase, 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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,31 @@ 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(
42+
configuration: BuildConfiguration?,
43+
defaultWorkspaceType: WorkspaceType?,
44+
path: AbsolutePath?,
45+
flags: BuildFlags
46+
) {
3847
self.configuration = configuration
48+
self.defaultWorkspaceType = defaultWorkspaceType
3949
self.path = path
4050
self.flags = flags
4151
}
@@ -49,6 +59,7 @@ public struct BuildSetup {
4959
flags = flags.merging(other.flags)
5060
return BuildSetup(
5161
configuration: other.configuration ?? self.configuration,
62+
defaultWorkspaceType: other.defaultWorkspaceType ?? self.defaultWorkspaceType,
5263
path: other.path ?? self.path,
5364
flags: flags
5465
)

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 compilationDatabase
16+
case swiftPM
17+
}

Sources/SourceKitLSP/SourceKitServer.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,17 @@ extension LanguageServerProtocol.BuildConfiguration {
986986
}
987987
}
988988

989+
private extension LanguageServerProtocol.WorkspaceType {
990+
/// Convert `LanguageServerProtocol.WorkspaceType` to `SkSupport.WorkspaceType`.
991+
var workspaceType: SKSupport.WorkspaceType {
992+
switch self {
993+
case .buildServer: return .buildServer
994+
case .compilationDatabase: return .compilationDatabase
995+
case .swiftPM: return .swiftPM
996+
}
997+
}
998+
}
999+
9891000
// MARK: - Request and notification handling
9901001

9911002
extension SourceKitServer {
@@ -1003,6 +1014,7 @@ extension SourceKitServer {
10031014
}
10041015
return SKCore.BuildSetup(
10051016
configuration: buildParams?.buildConfiguration?.configuration,
1017+
defaultWorkspaceType: buildParams?.defaultWorkspaceType?.workspaceType,
10061018
path: scratchPath,
10071019
flags: BuildFlags(
10081020
cCompilerFlags: buildParams?.cFlags ?? [],

Sources/SourceKitLSP/Workspace.swift

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,42 @@ public final class Workspace {
108108
reloadPackageStatusCallback: @escaping (ReloadPackageStatus) async -> Void
109109
) async throws {
110110
var buildSystem: BuildSystem? = nil
111-
if let rootUrl = rootUri.fileURL, let rootPath = try? AbsolutePath(validating: rootUrl.path) {
112-
if let buildServer = await BuildServerBuildSystem(projectRoot: rootPath, buildSetup: buildSetup) {
113-
buildSystem = buildServer
114-
} else if let swiftpm = await SwiftPMWorkspace(
111+
112+
func buildSwiftPMWorkspace(rootUrl: URL) async -> SwiftPMWorkspace? {
113+
return await SwiftPMWorkspace(
115114
url: rootUrl,
116115
toolchainRegistry: toolchainRegistry,
117116
buildSetup: buildSetup,
118117
reloadPackageStatusCallback: reloadPackageStatusCallback
119-
) {
120-
buildSystem = swiftpm
121-
} else if let compdb = CompilationDatabaseBuildSystem(
118+
)
119+
}
120+
121+
func buildCompDBWorkspace(rootPath: AbsolutePath) -> CompilationDatabaseBuildSystem? {
122+
return CompilationDatabaseBuildSystem(
122123
projectRoot: rootPath,
123124
searchPaths: compilationDatabaseSearchPaths
124-
) {
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+
let defaultBuildSystem: BuildSystem? =
134+
switch buildSetup.defaultWorkspaceType {
135+
case .buildServer: await buildBuildServerWorkspace(rootPath: rootPath)
136+
case .compilationDatabase: buildCompDBWorkspace(rootPath: rootPath)
137+
case .swiftPM: await buildSwiftPMWorkspace(rootUrl: rootUrl)
138+
case nil: nil
139+
}
140+
if let defaultBuildSystem {
141+
buildSystem = defaultBuildSystem
142+
} else if let buildServer = await buildBuildServerWorkspace(rootPath: rootPath) {
143+
buildSystem = buildServer
144+
} else if let swiftpm = await buildSwiftPMWorkspace(rootUrl: rootUrl) {
145+
buildSystem = swiftpm
146+
} else if let compdb = buildCompDBWorkspace(rootPath: rootPath) {
125147
buildSystem = compdb
126148
} else {
127149
logger.error(

Sources/sourcekit-lsp/SourceKitLSP.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ extension SKSupport.BuildConfiguration: ExpressibleByArgument {}
9292
extension SKSupport.BuildConfiguration: @retroactive ExpressibleByArgument {}
9393
#endif
9494

95+
#if swift(<5.10)
96+
extension SKSupport.WorkspaceType: ExpressibleByArgument {}
97+
#else
98+
extension SKSupport.WorkspaceType: @retroactive ExpressibleByArgument {}
99+
#endif
100+
95101
@main
96102
struct SourceKitLSP: ParsableCommand {
97103
static let configuration = CommandConfiguration(
@@ -169,6 +175,11 @@ struct SourceKitLSP: ParsableCommand {
169175
)
170176
var indexPrefixMappings = [PathPrefixMapping]()
171177

178+
@Option(
179+
help: "Override default workspace type selection; one of 'swiftPM', 'compilationDatabase', or 'buildServer'"
180+
)
181+
var defaultWorkspaceType: SKSupport.WorkspaceType?
182+
172183
@Option(
173184
name: .customLong("compilation-db-search-path"),
174185
parsing: .singleValue,
@@ -191,6 +202,7 @@ struct SourceKitLSP: ParsableCommand {
191202
var serverOptions = SourceKitServer.Options()
192203

193204
serverOptions.buildSetup.configuration = buildConfiguration
205+
serverOptions.buildSetup.defaultWorkspaceType = defaultWorkspaceType
194206
serverOptions.buildSetup.path = scratchPath
195207
serverOptions.buildSetup.flags.cCompilerFlags = buildFlagsCc
196208
serverOptions.buildSetup.flags.cxxCompilerFlags = buildFlagsCxx

Tests/SKCoreTests/FallbackBuildSystemTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ final class FallbackBuildSystemTests: XCTestCase {
5858

5959
let buildSetup = BuildSetup(
6060
configuration: .debug,
61+
defaultWorkspaceType: nil,
6162
path: nil,
6263
flags: BuildFlags(swiftCompilerFlags: [
6364
"-Xfrontend",
@@ -97,6 +98,7 @@ final class FallbackBuildSystemTests: XCTestCase {
9798

9899
let buildSetup = BuildSetup(
99100
configuration: .debug,
101+
defaultWorkspaceType: nil,
100102
path: nil,
101103
flags: BuildFlags(swiftCompilerFlags: [
102104
"-sdk",
@@ -169,6 +171,7 @@ final class FallbackBuildSystemTests: XCTestCase {
169171

170172
let buildSetup = BuildSetup(
171173
configuration: .debug,
174+
defaultWorkspaceType: nil,
172175
path: nil,
173176
flags: BuildFlags(cxxCompilerFlags: [
174177
"-v"
@@ -204,6 +207,7 @@ final class FallbackBuildSystemTests: XCTestCase {
204207

205208
let buildSetup = BuildSetup(
206209
configuration: .debug,
210+
defaultWorkspaceType: nil,
207211
path: nil,
208212
flags: BuildFlags(cxxCompilerFlags: [
209213
"-isysroot",
@@ -254,6 +258,7 @@ final class FallbackBuildSystemTests: XCTestCase {
254258

255259
let buildSetup = BuildSetup(
256260
configuration: .debug,
261+
defaultWorkspaceType: nil,
257262
path: nil,
258263
flags: BuildFlags(cCompilerFlags: [
259264
"-v"

Tests/SKSwiftPMWorkspaceTests/SwiftPMWorkspaceTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
186186

187187
let config = BuildSetup(
188188
configuration: .release,
189+
defaultWorkspaceType: nil,
189190
path: packageRoot.appending(component: "non_default_build_path"),
190191
flags: BuildFlags(cCompilerFlags: ["-m32"], swiftCompilerFlags: ["-typecheck"])
191192
)

0 commit comments

Comments
 (0)