diff --git a/clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts b/clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts --- a/clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts +++ b/clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts @@ -110,7 +110,8 @@ const clangdClient = new ClangdLanguageClient('Clang Language Server', serverOptions, clientOptions); const semanticHighlightingFeature = - new semanticHighlighting.SemanticHighlightingFeature(); + new semanticHighlighting.SemanticHighlightingFeature(clangdClient, + context); context.subscriptions.push( vscode.Disposable.from(semanticHighlightingFeature)); clangdClient.registerFeature(semanticHighlightingFeature); @@ -144,14 +145,9 @@ clangdClient.onNotification( 'textDocument/clangd.fileStatus', (fileStatus) => { status.onFileUpdated(fileStatus); }); - clangdClient.onNotification( - semanticHighlighting.NotificationType, - semanticHighlightingFeature.handleNotification.bind( - semanticHighlightingFeature)); } else if (newState == vscodelc.State.Stopped) { // Clear all cached statuses when clangd crashes. status.clear(); - semanticHighlightingFeature.dispose(); } })); // An empty place holder for the activate command, otherwise we'll get an diff --git a/clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts b/clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts --- a/clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts +++ b/clang-tools-extra/clangd/clients/clangd-vscode/src/semantic-highlighting.ts @@ -44,7 +44,7 @@ // Language server push notification providing the semantic highlighting // information for a text document. -export const NotificationType = +const NotificationType = new vscodelc.NotificationType( 'textDocument/semanticHighlighting'); @@ -58,6 +58,19 @@ highlighter: Highlighter; // Any disposables that should be cleaned up when clangd crashes. private subscriptions: vscode.Disposable[] = []; + constructor(client: vscodelc.BaseLanguageClient, + context: vscode.ExtensionContext) { + context.subscriptions.push(client.onDidChangeState(({newState}) => { + if (newState == vscodelc.State.Running) { + // Register handler for semantic highlighting notification. + client.onNotification(NotificationType, + this.handleNotification.bind(this)); + } else if (newState == vscodelc.State.Stopped) { + // Dispose resources when clangd crashes. + this.dispose(); + } + })); + } fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) { // Extend the ClientCapabilities type and add semantic highlighting // capability to the object.