diff --git a/package.json b/package.json index 2249e281..700313df 100644 --- a/package.json +++ b/package.json @@ -140,6 +140,10 @@ { "command": "clangd.activate", "title": "Manually activate clangd extension" + }, + { + "command": "clangd.restart", + "title": "Restart the clangd language server" } ], "keybindings": [ diff --git a/src/extension.ts b/src/extension.ts index b7c6c884..df7825a7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -22,6 +22,17 @@ class ClangdLanguageClient extends vscodelc.LanguageClient { // Call default implementation. super.logFailedRequest(rpcReply, error); } + + activate() { + this.dispose(); + this.startDisposable = this.start(); + } + + dispose() { + if (this.startDisposable) + this.startDisposable.dispose(); + } + private startDisposable: vscodelc.Disposable; } class EnableEditsNearCursorFeature implements vscodelc.StaticFeature { @@ -102,10 +113,11 @@ export async function activate(context: vscode.ExtensionContext) { const client = new ClangdLanguageClient('Clang Language Server', serverOptions, clientOptions); + context.subscriptions.push(vscode.Disposable.from(client)); if (config.get('semanticHighlighting')) semanticHighlighting.activate(client, context); client.registerFeature(new EnableEditsNearCursorFeature); - context.subscriptions.push(client.start()); + client.activate(); console.log('Clang Language Server is now active!'); fileStatus.activate(client, context); switchSourceHeader.activate(client, context); @@ -113,4 +125,9 @@ export async function activate(context: vscode.ExtensionContext) { // "command is not registered" error. context.subscriptions.push( vscode.commands.registerCommand('clangd.activate', async () => {})); + context.subscriptions.push( + vscode.commands.registerCommand('clangd.restart', async () => { + await client.stop(); + client.activate(); + })); }