Skip to content

Commit cafc63e

Browse files
committed
Add --workspace-type flag
1 parent b7cc49b commit cafc63e

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

Sources/SourceKitLSP/SourceKitServer+Options.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ 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+
3336
/// Additional paths to search for a compilation database, relative to a workspace root.
3437
public var compilationDatabaseSearchPaths: [RelativePath]
3538

@@ -52,6 +55,7 @@ extension SourceKitServer {
5255
public init(
5356
buildSetup: BuildSetup = .default,
5457
clangdOptions: [String] = [],
58+
workspaceType: WorkspaceType? = nil,
5559
compilationDatabaseSearchPaths: [RelativePath] = [],
5660
indexOptions: IndexOptions = .init(),
5761
completionOptions: SKCompletionOptions = .init(),
@@ -60,6 +64,7 @@ extension SourceKitServer {
6064
) {
6165
self.buildSetup = buildSetup
6266
self.clangdOptions = clangdOptions
67+
self.workspaceType = workspaceType
6368
self.compilationDatabaseSearchPaths = compilationDatabaseSearchPaths
6469
self.indexOptions = indexOptions
6570
self.completionOptions = completionOptions

Sources/SourceKitLSP/SourceKitServer.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,7 @@ extension SourceKitServer {
10151015
capabilityRegistry: capabilityRegistry,
10161016
toolchainRegistry: self.toolchainRegistry,
10171017
buildSetup: self.options.buildSetup.merging(workspaceBuildSetup),
1018+
workspaceType: self.options.workspaceType,
10181019
compilationDatabaseSearchPaths: self.options.compilationDatabaseSearchPaths,
10191020
indexOptions: self.options.indexOptions,
10201021
reloadPackageStatusCallback: { status in

Sources/SourceKitLSP/Workspace.swift

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

41+
public enum WorkspaceType: String {
42+
case buildserver, compdb, swiftpm
43+
}
44+
4145
/// Represents the configuration and state of a project or combination of projects being worked on
4246
/// together.
4347
///
@@ -103,14 +107,53 @@ public final class Workspace {
103107
capabilityRegistry: CapabilityRegistry,
104108
toolchainRegistry: ToolchainRegistry,
105109
buildSetup: BuildSetup,
110+
workspaceType: WorkspaceType?,
106111
compilationDatabaseSearchPaths: [RelativePath],
107112
indexOptions: IndexOptions = IndexOptions(),
108113
reloadPackageStatusCallback: @escaping (ReloadPackageStatus) async -> Void
109114
) async throws {
110115
var buildSystem: BuildSystem? = nil
111116
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
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
114157
} else if let swiftpm = await SwiftPMWorkspace(
115158
url: rootUrl,
116159
toolchainRegistry: toolchainRegistry,

Sources/sourcekit-lsp/SourceKitLSP.swift

Lines changed: 13 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 WorkspaceType: ExpressibleByArgument {}
97+
#else
98+
extension WorkspaceType: @retroactive ExpressibleByArgument {}
99+
#endif
100+
95101
@main
96102
struct SourceKitLSP: ParsableCommand {
97103
static let configuration = CommandConfiguration(
@@ -169,6 +175,12 @@ struct SourceKitLSP: ParsableCommand {
169175
)
170176
var indexPrefixMappings = [PathPrefixMapping]()
171177

178+
@Option(
179+
name: .customLong("workspace-type"),
180+
help: "Override default workspace type selection"
181+
)
182+
var workspaceType: WorkspaceType?
183+
172184
@Option(
173185
name: .customLong("compilation-db-search-path"),
174186
parsing: .singleValue,
@@ -197,6 +209,7 @@ struct SourceKitLSP: ParsableCommand {
197209
serverOptions.buildSetup.flags.linkerFlags = buildFlagsLinker
198210
serverOptions.buildSetup.flags.swiftCompilerFlags = buildFlagsSwift
199211
serverOptions.clangdOptions = clangdOptions
212+
serverOptions.workspaceType = workspaceType
200213
serverOptions.compilationDatabaseSearchPaths = compilationDatabaseSearchPaths
201214
serverOptions.indexOptions.indexStorePath = indexStorePath
202215
serverOptions.indexOptions.indexDatabasePath = indexDatabasePath

0 commit comments

Comments
 (0)