Skip to content

Commit 361d5ef

Browse files
committed
Pick which lsp-server to restart if more than one is available
1 parent 12381cb commit 361d5ef

File tree

5 files changed

+73
-57
lines changed

5 files changed

+73
-57
lines changed

src/commands.ts

+3-14
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { runTask } from "./commands/runTask";
4646
import { TestKind } from "./TestExplorer/TestKind";
4747
import { pickProcess } from "./commands/pickProcess";
4848
import { openDocumentation } from "./commands/openDocumentation";
49-
import showFolderSelectionQuickPick from "./utilities/folderQuickPick";
49+
import restartLSPServer from "./commands/restartLSPServer";
5050

5151
/**
5252
* References:
@@ -97,6 +97,7 @@ export enum Commands {
9797
DEBUG_ALL_TESTS = "swift.debugAllTests",
9898
COVER_ALL_TESTS = "swift.coverAllTests",
9999
OPEN_MANIFEST = "swift.openManifest",
100+
RESTART_LSP = "swift.restartLSPServer",
100101
}
101102

102103
/**
@@ -147,19 +148,7 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
147148
),
148149
vscode.commands.registerCommand(Commands.RUN_PLUGIN_TASK, () => runPluginTask()),
149150
vscode.commands.registerCommand(Commands.RUN_TASK, name => runTask(ctx, name)),
150-
vscode.commands.registerCommand("swift.restartLSPServer", async () => {
151-
const folder =
152-
ctx.currentFolder ??
153-
(await showFolderSelectionQuickPick(
154-
ctx,
155-
"Select a folder to restart the LSP server for"
156-
));
157-
if (!folder) {
158-
return;
159-
}
160-
const languageClientManager = ctx.languageClientManager.get(folder);
161-
await languageClientManager.restart();
162-
}),
151+
vscode.commands.registerCommand(Commands.RESTART_LSP, () => restartLSPServer(ctx)),
163152
vscode.commands.registerCommand("swift.reindexProject", () => reindexProject(ctx)),
164153
vscode.commands.registerCommand("swift.insertFunctionComment", () =>
165154
insertFunctionComment(ctx)

src/commands/dependencies/resolve.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@ import { FolderContext } from "../../FolderContext";
1717
import { createSwiftTask, SwiftTaskProvider } from "../../tasks/SwiftTaskProvider";
1818
import { WorkspaceContext } from "../../WorkspaceContext";
1919
import { executeTaskWithUI, updateAfterError } from "../utilities";
20-
import showFolderSelectionQuickPick from "../../utilities/folderQuickPick";
2120

2221
/**
2322
* Executes a {@link vscode.Task task} to resolve this package's dependencies.
2423
*/
2524
export async function resolveDependencies(ctx: WorkspaceContext) {
26-
const current =
27-
ctx.currentFolder ??
28-
(await showFolderSelectionQuickPick(ctx, "Select a folder to resolve dependencies for"));
25+
const current = ctx.currentFolder;
2926
if (!current) {
3027
return false;
3128
}

src/commands/restartLSPServer.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 2025 the VS Code Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import * as vscode from "vscode";
16+
import { WorkspaceContext } from "../WorkspaceContext";
17+
import { FolderContext } from "../FolderContext";
18+
19+
export default async function restartLSPServer(ctx: WorkspaceContext) {
20+
const toolchainLookup = ctx.folders.reduce(
21+
(acc, folder) => {
22+
acc[folder.swiftVersion.toString()] = acc[folder.swiftVersion.toString()] ?? [];
23+
acc[folder.swiftVersion.toString()].push({
24+
folder,
25+
fullToolchainName: folder.toolchain.swiftVersionString,
26+
});
27+
return acc;
28+
},
29+
{} as Record<string, { folder: FolderContext; fullToolchainName: string }[]>
30+
);
31+
32+
const toolchains: vscode.QuickPickItem[] = Object.keys(toolchainLookup).map(key => ({
33+
label: key,
34+
description: toolchainLookup[key][0].fullToolchainName,
35+
detail: toolchainLookup[key].map(({ folder }) => folder.name).join(", "),
36+
}));
37+
38+
// Skip picking a toolchain if there is only one option to pick
39+
if (toolchains.length === 1) {
40+
return restartLSP(ctx, toolchains[0].label);
41+
}
42+
43+
const selected = await vscode.window.showQuickPick(toolchains, {
44+
title: "Restart LSP server",
45+
placeHolder: "Select a sourcekit-lsp instance to restart",
46+
canPickMany: false,
47+
});
48+
49+
if (!selected) {
50+
return undefined;
51+
}
52+
53+
return restartLSP(ctx, selected.label);
54+
}
55+
56+
async function restartLSP(ctx: WorkspaceContext, version: string) {
57+
const languageClientManager = ctx.languageClientManager.getByVersion(version);
58+
await languageClientManager.restart();
59+
}

src/sourcekit-lsp/LanguageClientToolchainCoordinator.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,16 @@ export class LanguageClientToolchainCoordinator implements vscode.Disposable {
8787
* @returns
8888
*/
8989
public get(folder: FolderContext): LanguageClientManager {
90-
const client = this.clients.get(folder.swiftVersion.toString());
90+
return this.getByVersion(folder.swiftVersion.toString());
91+
}
92+
93+
/**
94+
* Returns the LanguageClientManager for the supplied toolchain version.
95+
* @param folder
96+
* @returns
97+
*/
98+
public getByVersion(version: string): LanguageClientManager {
99+
const client = this.clients.get(version);
91100
if (!client) {
92101
throw new Error(
93102
"LanguageClientManager has not yet been created. This is a bug, please file an issue at https://github.com/swiftlang/vscode-swift/issues"

src/utilities/folderQuickPick.ts

-38
This file was deleted.

0 commit comments

Comments
 (0)