Skip to content

Commit 7ddc363

Browse files
authored
Add a command to shut down the server explicitly (#866)
1 parent 5c31433 commit 7ddc363

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@
234234
"category": "clangd",
235235
"title": "Restart language server"
236236
},
237+
{
238+
"command": "clangd.shutdown",
239+
"category": "clangd",
240+
"title": "Shutdown language server"
241+
},
237242
{
238243
"command": "clangd.typeHierarchy",
239244
"category": "clangd",

src/clangd-context.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ export class ClangdContext implements vscode.Disposable {
242242
return this.client && this.client.state == vscodelc.State.Starting;
243243
}
244244

245+
clientIsRunning() {
246+
return this.client && this.client.state == vscodelc.State.Running;
247+
}
248+
245249
dispose() {
246250
this.subscriptions.forEach((d) => { d.dispose(); });
247251
if (this.client)

src/extension.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ export async function activate(context: vscode.ExtensionContext):
1919

2020
let clangdContext: ClangdContext|null = null;
2121

22-
// An empty place holder for the activate command, otherwise we'll get an
23-
// "command is not registered" error.
2422
context.subscriptions.push(
25-
vscode.commands.registerCommand('clangd.activate', async () => {}));
23+
vscode.commands.registerCommand('clangd.activate', async () => {
24+
if (clangdContext && (clangdContext.clientIsStarting() ||
25+
clangdContext.clientIsRunning())) {
26+
return;
27+
}
28+
vscode.commands.executeCommand('clangd.restart');
29+
}));
2630
context.subscriptions.push(
2731
vscode.commands.registerCommand('clangd.restart', async () => {
2832
if (!get<boolean>('enable')) {
@@ -58,6 +62,14 @@ export async function activate(context: vscode.ExtensionContext):
5862
apiInstance.client = clangdContext?.client;
5963
}
6064
}));
65+
context.subscriptions.push(
66+
vscode.commands.registerCommand('clangd.shutdown', async () => {
67+
if (clangdContext && clangdContext.clientIsStarting()) {
68+
return;
69+
}
70+
if (clangdContext)
71+
clangdContext.dispose();
72+
}));
6173

6274
let shouldCheck = false;
6375

test/inactive-regions.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class MockClangdContext implements ClangdContext {
2020

2121
clientIsStarting() { return false; }
2222

23+
clientIsRunning() { return true; }
24+
2325
dispose() { throw new Error('Method not implemented.'); }
2426
}
2527

0 commit comments

Comments
 (0)