Skip to content

Commit

Permalink
Gracefully handle 'clangd.restart' being invoked when the extension h…
Browse files Browse the repository at this point in the history
…asn't been activated yet (#587)

Fixes #502
  • Loading branch information
HighCommander4 authored Mar 7, 2024
1 parent f7b1f26 commit 17206eb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/clangd-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ export class ClangdContext implements vscode.Disposable {
(e) => isClangdDocument(e.document));
}

clientIsReady() {
return this.client && this.client.state == vscodelc.State.Running;
}

dispose() {
this.subscriptions.forEach((d) => { d.dispose(); });
if (this.client)
Expand Down
9 changes: 9 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('clangd.activate', async () => {}));
context.subscriptions.push(
vscode.commands.registerCommand('clangd.restart', async () => {
// clangd.restart can be called when the extension is not yet activated.
// In such a case, vscode will activate the extension and then run this
// handler. Detect this situation and bail out (doing an extra
// stop/start cycle in this situation is pointless, and doesn't work
// anyways because the client can't be stop()-ped when it's still in the
// Starting state).
if (!clangdContext.clientIsReady()) {
return;
}
await clangdContext.dispose();
await clangdContext.activate(context.globalStoragePath, outputChannel);
}));
Expand Down
2 changes: 2 additions & 0 deletions test/inactive-regions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class MockClangdContext implements ClangdContext {

async activate() { throw new Error('Method not implemented.'); }

clientIsReady() { return true; }

dispose() { throw new Error('Method not implemented.'); }
}

Expand Down

0 comments on commit 17206eb

Please sign in to comment.