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

Fixes #502
  • Loading branch information
HighCommander4 committed Feb 25, 2024
1 parent eca1e05 commit d2f3c98
Show file tree
Hide file tree
Showing 2 changed files with 12 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 @@ -175,6 +175,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
8 changes: 8 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ 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

0 comments on commit d2f3c98

Please sign in to comment.