Skip to content

Commit 1aa96fa

Browse files
committed
Share common pattern of determining the build system for a workspace and creating it
1 parent 0fbb646 commit 1aa96fa

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,15 @@ extension SourceKitLSPServer {
856856
return workspace
857857
}
858858

859+
/// Determines the build system for the given workspace folder and creates a `Workspace` that uses this inferred build
860+
/// system.
861+
private func createWorkspaceWithInferredBuildSystem(workspaceFolder: DocumentURI) async throws -> Workspace {
862+
return try await self.createWorkspace(
863+
workspaceFolder: workspaceFolder,
864+
buildSystemSpec: determineBuildSystem(forWorkspaceFolder: workspaceFolder, options: self.options)
865+
)
866+
}
867+
859868
func initialize(_ req: InitializeRequest) async throws -> InitializeResult {
860869
logger.logFullObjectInMultipleLogMessages(header: "Initialize request", AnyRequestType(request: req))
861870
// If the client can handle `PeekDocumentsRequest`, they can enable the
@@ -916,34 +925,25 @@ extension SourceKitLSPServer {
916925
if let workspaceFolders = req.workspaceFolders {
917926
self.workspacesAndIsImplicit += await workspaceFolders.asyncCompactMap { workspaceFolder in
918927
await orLog("Creating workspace from workspaceFolders") {
919-
let workspace = try await self.createWorkspace(
920-
workspaceFolder: workspaceFolder.uri,
921-
buildSystemSpec: determineBuildSystem(forWorkspaceFolder: workspaceFolder.uri, options: self.options)
928+
return (
929+
workspace: try await self.createWorkspaceWithInferredBuildSystem(workspaceFolder: workspaceFolder.uri),
930+
isImplicit: false
922931
)
923-
return (workspace: workspace, isImplicit: false)
924932
}
925933
}
926934
} else if let uri = req.rootURI {
927-
let workspace = await orLog("Creating workspace from rootURI") {
928-
try await self.createWorkspace(
929-
workspaceFolder: uri,
930-
buildSystemSpec: determineBuildSystem(forWorkspaceFolder: uri, options: self.options)
935+
await orLog("Creating workspace from rootURI") {
936+
self.workspacesAndIsImplicit.append(
937+
(workspace: try await self.createWorkspaceWithInferredBuildSystem(workspaceFolder: uri), isImplicit: false)
931938
)
932939
}
933-
if let workspace {
934-
self.workspacesAndIsImplicit.append((workspace: workspace, isImplicit: false))
935-
}
936940
} else if let path = req.rootPath {
937941
let uri = DocumentURI(URL(fileURLWithPath: path))
938-
let workspace = await orLog("Creating workspace from rootPath") {
939-
try await self.createWorkspace(
940-
workspaceFolder: uri,
941-
buildSystemSpec: determineBuildSystem(forWorkspaceFolder: uri, options: self.options)
942+
await orLog("Creating workspace from rootPath") {
943+
self.workspacesAndIsImplicit.append(
944+
(workspace: try await self.createWorkspaceWithInferredBuildSystem(workspaceFolder: uri), isImplicit: false)
942945
)
943946
}
944-
if let workspace {
945-
self.workspacesAndIsImplicit.append((workspace: workspace, isImplicit: false))
946-
}
947947
}
948948

949949
if self.workspaces.isEmpty {
@@ -1340,10 +1340,7 @@ extension SourceKitLSPServer {
13401340
if let added = notification.event.added {
13411341
let newWorkspaces = await added.asyncCompactMap { workspaceFolder in
13421342
await orLog("Creating workspace after workspace folder change") {
1343-
try await self.createWorkspace(
1344-
workspaceFolder: workspaceFolder.uri,
1345-
buildSystemSpec: determineBuildSystem(forWorkspaceFolder: workspaceFolder.uri, options: self.options)
1346-
)
1343+
try await self.createWorkspaceWithInferredBuildSystem(workspaceFolder: workspaceFolder.uri)
13471344
}
13481345
}
13491346
self.workspacesAndIsImplicit += newWorkspaces.map { (workspace: $0, isImplicit: false) }

0 commit comments

Comments
 (0)