Skip to content

Commit 9c84a34

Browse files
authored
Merge pull request #1817 from ahoppen/bsp-review
2 parents 714e520 + 1aa96fa commit 9c84a34

File tree

12 files changed

+94
-88
lines changed

12 files changed

+94
-88
lines changed

Sources/BuildServerProtocol/Messages/BuildTargetSourcesRequest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import LanguageServerProtocol
1717
#endif
1818

1919
/// The build target sources request is sent from the client to the server to
20-
/// query for the list of text documents and directories that are belong to a
20+
/// query for the list of text documents and directories that belong to a
2121
/// build target. The sources response must not include sources that are
2222
/// external to the workspace.
2323
public struct BuildTargetSourcesRequest: RequestType, Hashable {

Sources/BuildServerProtocol/SupportTypes/BuildTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public struct BuildTarget: Codable, Hashable, Sendable {
3434
/// A human readable name for this target.
3535
/// May be presented in the user interface.
3636
/// Should be unique if possible.
37-
/// The id.uri is used if None.
37+
/// The id.uri is used if `nil`.
3838
public var displayName: String?
3939

4040
/// The directory where this target belongs to. Multiple build targets are

Sources/BuildSystemIntegration/BuildSystemManager.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private enum BuildSystemAdapter {
136136
}
137137
}
138138

139-
private extension BuildSystemKind {
139+
private extension BuildSystemSpec {
140140
private static func createBuiltInBuildSystemAdapter(
141141
projectRoot: AbsolutePath,
142142
messagesToSourceKitLSPHandler: any MessageHandler,
@@ -176,8 +176,8 @@ private extension BuildSystemKind {
176176
buildSystemTestHooks testHooks: BuildSystemTestHooks,
177177
messagesToSourceKitLSPHandler: any MessageHandler
178178
) async -> BuildSystemAdapter? {
179-
switch self {
180-
case .buildServer(projectRoot: let projectRoot):
179+
switch self.kind {
180+
case .buildServer:
181181
let buildSystem = await orLog("Creating external build system") {
182182
try await ExternalBuildSystemAdapter(
183183
projectRoot: projectRoot,
@@ -190,7 +190,7 @@ private extension BuildSystemKind {
190190
}
191191
logger.log("Created external build server at \(projectRoot.pathString)")
192192
return .external(buildSystem)
193-
case .compilationDatabase(projectRoot: let projectRoot):
193+
case .compilationDatabase:
194194
return await Self.createBuiltInBuildSystemAdapter(
195195
projectRoot: projectRoot,
196196
messagesToSourceKitLSPHandler: messagesToSourceKitLSPHandler,
@@ -204,7 +204,7 @@ private extension BuildSystemKind {
204204
connectionToSourceKitLSP: connectionToSourceKitLSP
205205
)
206206
}
207-
case .swiftPM(projectRoot: let projectRoot):
207+
case .swiftPM:
208208
return await Self.createBuiltInBuildSystemAdapter(
209209
projectRoot: projectRoot,
210210
messagesToSourceKitLSPHandler: messagesToSourceKitLSPHandler,
@@ -218,7 +218,7 @@ private extension BuildSystemKind {
218218
testHooks: testHooks.swiftPMTestHooks
219219
)
220220
}
221-
case .testBuildSystem(projectRoot: let projectRoot):
221+
case .testBuildSystem:
222222
return await Self.createBuiltInBuildSystemAdapter(
223223
projectRoot: projectRoot,
224224
messagesToSourceKitLSPHandler: messagesToSourceKitLSPHandler,
@@ -353,7 +353,7 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
353353
}
354354

355355
package init(
356-
buildSystemKind: BuildSystemKind?,
356+
buildSystemSpec: BuildSystemSpec?,
357357
toolchainRegistry: ToolchainRegistry,
358358
options: SourceKitLSPOptions,
359359
connectionToClient: BuildSystemManagerConnectionToClient,
@@ -362,8 +362,8 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
362362
self.toolchainRegistry = toolchainRegistry
363363
self.options = options
364364
self.connectionToClient = connectionToClient
365-
self.projectRoot = buildSystemKind?.projectRoot
366-
self.buildSystemAdapter = await buildSystemKind?.createBuildSystemAdapter(
365+
self.projectRoot = buildSystemSpec?.projectRoot
366+
self.buildSystemAdapter = await buildSystemSpec?.createBuildSystemAdapter(
367367
toolchainRegistry: toolchainRegistry,
368368
options: options,
369369
buildSystemTestHooks: buildSystemTestHooks,
@@ -392,8 +392,8 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
392392
guard let buildSystemAdapter else {
393393
return nil
394394
}
395-
guard let buildSystemKind else {
396-
logger.fault("If we have a connectionToBuildSystem, we must have had a buildSystemKind")
395+
guard let buildSystemSpec else {
396+
logger.fault("If we have a connectionToBuildSystem, we must have had a buildSystemSpec")
397397
return nil
398398
}
399399
let initializeResponse = await orLog("Initializing build system") {
@@ -402,7 +402,7 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
402402
displayName: "SourceKit-LSP",
403403
version: "",
404404
bspVersion: "2.2.0",
405-
rootUri: URI(buildSystemKind.projectRoot.asURL),
405+
rootUri: URI(buildSystemSpec.projectRoot.asURL),
406406
capabilities: BuildClientCapabilities(languageIds: [.c, .cpp, .objective_c, .objective_cpp, .swift])
407407
)
408408
)
@@ -415,7 +415,7 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
415415
// the build server.
416416
logger.log("Launched a legacy BSP server. Using push-based build settings model.")
417417
let legacyBuildServer = await LegacyBuildServerBuildSystem(
418-
projectRoot: buildSystemKind.projectRoot,
418+
projectRoot: buildSystemSpec.projectRoot,
419419
initializationData: initializeResponse,
420420
externalBuildSystemAdapter
421421
)
@@ -651,7 +651,7 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
651651
let sourcesItems = try await self.sourceFiles(in: [target])
652652
let sourceFiles = sourcesItems.flatMap(\.sources)
653653
var result: Language? = nil
654-
for sourceFile in sourceFiles {
654+
for sourceFile in sourceFiles where sourceFile.uri == document {
655655
guard let language = sourceFile.sourceKitData?.language else {
656656
continue
657657
}

Sources/BuildSystemIntegration/BuiltInBuildSystemAdapter.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,22 @@ import struct TSCBasic.AbsolutePath
2727
import struct TSCBasic.RelativePath
2828
#endif
2929

30-
package enum BuildSystemKind {
31-
case buildServer(projectRoot: AbsolutePath)
32-
case compilationDatabase(projectRoot: AbsolutePath)
33-
case swiftPM(projectRoot: AbsolutePath)
34-
case testBuildSystem(projectRoot: AbsolutePath)
35-
36-
package var projectRoot: AbsolutePath {
37-
switch self {
38-
case .buildServer(let projectRoot): return projectRoot
39-
case .compilationDatabase(let projectRoot): return projectRoot
40-
case .swiftPM(let projectRoot): return projectRoot
41-
case .testBuildSystem(let projectRoot): return projectRoot
42-
}
30+
/// The details necessary to create a `BuildSystemAdapter`.
31+
package struct BuildSystemSpec {
32+
package enum Kind {
33+
case buildServer
34+
case compilationDatabase
35+
case swiftPM
36+
case testBuildSystem
37+
}
38+
39+
package var kind: Kind
40+
41+
package var projectRoot: AbsolutePath
42+
43+
package init(kind: BuildSystemSpec.Kind, projectRoot: AbsolutePath) {
44+
self.kind = kind
45+
self.projectRoot = projectRoot
4346
}
4447
}
4548

Sources/BuildSystemIntegration/DetermineBuildSystem.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import struct TSCBasic.AbsolutePath
3535
package func determineBuildSystem(
3636
forWorkspaceFolder workspaceFolder: DocumentURI,
3737
options: SourceKitLSPOptions
38-
) -> BuildSystemKind? {
38+
) -> BuildSystemSpec? {
3939
var buildSystemPreference: [WorkspaceType] = [
4040
.buildServer, .swiftPM, .compilationDatabase,
4141
]
@@ -52,17 +52,17 @@ package func determineBuildSystem(
5252
switch buildSystemType {
5353
case .buildServer:
5454
if let projectRoot = ExternalBuildSystemAdapter.projectRoot(for: workspaceFolderPath, options: options) {
55-
return .buildServer(projectRoot: projectRoot)
55+
return BuildSystemSpec(kind: .buildServer, projectRoot: projectRoot)
5656
}
5757
case .compilationDatabase:
5858
if let projectRoot = CompilationDatabaseBuildSystem.projectRoot(for: workspaceFolderPath, options: options) {
59-
return .compilationDatabase(projectRoot: projectRoot)
59+
return BuildSystemSpec(kind: .compilationDatabase, projectRoot: projectRoot)
6060
}
6161
case .swiftPM:
6262
if let projectRootURL = SwiftPMBuildSystem.projectRoot(for: workspaceFolderUrl, options: options),
6363
let projectRoot = try? AbsolutePath(validating: projectRootURL.filePath)
6464
{
65-
return .swiftPM(projectRoot: projectRoot)
65+
return BuildSystemSpec(kind: .swiftPM, projectRoot: projectRoot)
6666
}
6767
}
6868
}

Sources/SourceKitLSP/SharedWorkDoneProgressManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ extension WorkDoneProgressManager {
4949
actor SharedWorkDoneProgressManager {
5050
private weak var sourceKitLSPServer: SourceKitLSPServer?
5151

52-
/// The number of in-progress operations. When greater than 0 `workDoneProgress` non-nil and a work done progress is
53-
/// displayed to the user.
52+
/// The number of in-progress operations. When greater than 0 `workDoneProgress` is non-nil and a work done progress
53+
/// is displayed to the user.
5454
private var inProgressOperations = 0
5555
private var workDoneProgress: WorkDoneProgressManager?
5656

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,16 @@ package actor SourceKitLSPServer {
229229
// was added to it and thus currently doesn't know that it can handle that file. In that case, we shouldn't open
230230
// a new workspace for the same root. Instead, the existing workspace's build system needs to be reloaded.
231231
let uri = DocumentURI(url)
232-
guard let buildSystemKind = determineBuildSystem(forWorkspaceFolder: uri, options: self.options) else {
232+
guard let buildSystemSpec = determineBuildSystem(forWorkspaceFolder: uri, options: self.options) else {
233233
continue
234234
}
235-
guard !projectRoots.contains(buildSystemKind.projectRoot) else {
235+
guard !projectRoots.contains(buildSystemSpec.projectRoot) else {
236236
continue
237237
}
238238
guard
239239
let workspace = await orLog(
240240
"Creating workspace",
241-
{ try await createWorkspace(workspaceFolder: uri, buildSystemKind: buildSystemKind) }
241+
{ try await createWorkspace(workspaceFolder: uri, buildSystemSpec: buildSystemSpec) }
242242
)
243243
else {
244244
continue
@@ -811,7 +811,7 @@ extension SourceKitLSPServer {
811811
/// If the build system that was determined for the workspace does not satisfy `condition`, `nil` is returned.
812812
private func createWorkspace(
813813
workspaceFolder: DocumentURI,
814-
buildSystemKind: BuildSystemKind?
814+
buildSystemSpec: BuildSystemSpec?
815815
) async throws -> Workspace {
816816
guard let capabilityRegistry = capabilityRegistry else {
817817
struct NoCapabilityRegistryError: Error {}
@@ -835,7 +835,7 @@ extension SourceKitLSPServer {
835835
documentManager: self.documentManager,
836836
rootUri: workspaceFolder,
837837
capabilityRegistry: capabilityRegistry,
838-
buildSystemKind: buildSystemKind,
838+
buildSystemSpec: buildSystemSpec,
839839
toolchainRegistry: self.toolchainRegistry,
840840
options: options,
841841
testHooks: testHooks,
@@ -858,6 +858,15 @@ extension SourceKitLSPServer {
858858
return workspace
859859
}
860860

861+
/// Determines the build system for the given workspace folder and creates a `Workspace` that uses this inferred build
862+
/// system.
863+
private func createWorkspaceWithInferredBuildSystem(workspaceFolder: DocumentURI) async throws -> Workspace {
864+
return try await self.createWorkspace(
865+
workspaceFolder: workspaceFolder,
866+
buildSystemSpec: determineBuildSystem(forWorkspaceFolder: workspaceFolder, options: self.options)
867+
)
868+
}
869+
861870
func initialize(_ req: InitializeRequest) async throws -> InitializeResult {
862871
logger.logFullObjectInMultipleLogMessages(header: "Initialize request", AnyRequestType(request: req))
863872
// If the client can handle `PeekDocumentsRequest`, they can enable the
@@ -918,34 +927,25 @@ extension SourceKitLSPServer {
918927
if let workspaceFolders = req.workspaceFolders {
919928
self.workspacesAndIsImplicit += await workspaceFolders.asyncCompactMap { workspaceFolder in
920929
await orLog("Creating workspace from workspaceFolders") {
921-
let workspace = try await self.createWorkspace(
922-
workspaceFolder: workspaceFolder.uri,
923-
buildSystemKind: determineBuildSystem(forWorkspaceFolder: workspaceFolder.uri, options: self.options)
930+
return (
931+
workspace: try await self.createWorkspaceWithInferredBuildSystem(workspaceFolder: workspaceFolder.uri),
932+
isImplicit: false
924933
)
925-
return (workspace: workspace, isImplicit: false)
926934
}
927935
}
928936
} else if let uri = req.rootURI {
929-
let workspace = await orLog("Creating workspace from rootURI") {
930-
try await self.createWorkspace(
931-
workspaceFolder: uri,
932-
buildSystemKind: determineBuildSystem(forWorkspaceFolder: uri, options: self.options)
937+
await orLog("Creating workspace from rootURI") {
938+
self.workspacesAndIsImplicit.append(
939+
(workspace: try await self.createWorkspaceWithInferredBuildSystem(workspaceFolder: uri), isImplicit: false)
933940
)
934941
}
935-
if let workspace {
936-
self.workspacesAndIsImplicit.append((workspace: workspace, isImplicit: false))
937-
}
938942
} else if let path = req.rootPath {
939943
let uri = DocumentURI(URL(fileURLWithPath: path))
940-
let workspace = await orLog("Creating workspace from rootPath") {
941-
try await self.createWorkspace(
942-
workspaceFolder: uri,
943-
buildSystemKind: determineBuildSystem(forWorkspaceFolder: uri, options: self.options)
944+
await orLog("Creating workspace from rootPath") {
945+
self.workspacesAndIsImplicit.append(
946+
(workspace: try await self.createWorkspaceWithInferredBuildSystem(workspaceFolder: uri), isImplicit: false)
944947
)
945948
}
946-
if let workspace {
947-
self.workspacesAndIsImplicit.append((workspace: workspace, isImplicit: false))
948-
}
949949
}
950950

951951
if self.workspaces.isEmpty {
@@ -957,7 +957,7 @@ extension SourceKitLSPServer {
957957
documentManager: self.documentManager,
958958
rootUri: req.rootURI,
959959
capabilityRegistry: self.capabilityRegistry!,
960-
buildSystemKind: nil,
960+
buildSystemSpec: nil,
961961
toolchainRegistry: self.toolchainRegistry,
962962
options: options,
963963
testHooks: testHooks,
@@ -1343,10 +1343,7 @@ extension SourceKitLSPServer {
13431343
if let added = notification.event.added {
13441344
let newWorkspaces = await added.asyncCompactMap { workspaceFolder in
13451345
await orLog("Creating workspace after workspace folder change") {
1346-
try await self.createWorkspace(
1347-
workspaceFolder: workspaceFolder.uri,
1348-
buildSystemKind: determineBuildSystem(forWorkspaceFolder: workspaceFolder.uri, options: self.options)
1349-
)
1346+
try await self.createWorkspaceWithInferredBuildSystem(workspaceFolder: workspaceFolder.uri)
13501347
}
13511348
}
13521349
self.workspacesAndIsImplicit += newWorkspaces.map { (workspace: $0, isImplicit: false) }

Sources/SourceKitLSP/Workspace.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ package final class Workspace: Sendable, BuildSystemManagerDelegate {
174174
documentManager: DocumentManager,
175175
rootUri: DocumentURI?,
176176
capabilityRegistry: CapabilityRegistry,
177-
buildSystemKind: BuildSystemKind?,
177+
buildSystemSpec: BuildSystemSpec?,
178178
toolchainRegistry: ToolchainRegistry,
179179
options: SourceKitLSPOptions,
180180
testHooks: TestHooks,
@@ -224,15 +224,15 @@ package final class Workspace: Sendable, BuildSystemManagerDelegate {
224224
}
225225

226226
let buildSystemManager = await BuildSystemManager(
227-
buildSystemKind: buildSystemKind,
227+
buildSystemSpec: buildSystemSpec,
228228
toolchainRegistry: toolchainRegistry,
229229
options: options,
230230
connectionToClient: ConnectionToClient(sourceKitLSPServer: sourceKitLSPServer),
231231
buildSystemTestHooks: testHooks.buildSystemTestHooks
232232
)
233233

234234
logger.log(
235-
"Created workspace at \(rootUri.forLogging) with project root \(buildSystemKind?.projectRoot.pathString ?? "<nil>")"
235+
"Created workspace at \(rootUri.forLogging) with project root \(buildSystemSpec?.projectRoot.pathString ?? "<nil>")"
236236
)
237237

238238
var index: IndexStoreDB? = nil

Sources/SwiftExtensions/Sequence+AsyncMap.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ extension Sequence {
7171

7272
/// Just like `Sequence.first` but allows an `async` predicate function.
7373
package func asyncFirst(
74-
@_inheritActorContext _ predicate: @Sendable (Element) async throws -> Bool
74+
@_inheritActorContext where predicate: @Sendable (Element) async throws -> Bool
7575
) async rethrows -> Element? {
7676
for element in self {
7777
if try await predicate(element) {
@@ -86,6 +86,6 @@ extension Sequence {
8686
package func asyncContains(
8787
@_inheritActorContext where predicate: @Sendable (Element) async throws -> Bool
8888
) async rethrows -> Bool {
89-
return try await asyncFirst(predicate) != nil
89+
return try await asyncFirst(where: predicate) != nil
9090
}
9191
}

0 commit comments

Comments
 (0)