From 8db9e0e6976e084764cd7a30098b5fa12ddb7f7f Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Thu, 5 Sep 2019 09:14:04 +0000 Subject: [PATCH] [clangd][vscode] Make SemanticHighlightingFeature more self-contained. Summary: so that we don't have too many usage from the client side (just a single occurrance for register), this also aligns with how other builtin feature being implemented in vscode. Reviewers: ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D67165 llvm-svn: 371036 --- .../clangd/clients/clangd-vscode/src/extension.ts | 8 ++------ .../clients/clangd-vscode/src/semantic-highlighting.ts | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts b/clang-tools-extra/clangd/clients/clangd-vscode/src/extension.ts index fa787f9..4cd7b0e5 100644 --- 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 @@ export function activate(context: vscode.ExtensionContext) { 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 @@ export function activate(context: vscode.ExtensionContext) { 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 index 27dfb8b..930079b 100644 --- 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 @@ export interface SemanticHighlightingLine { // 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 @@ export class SemanticHighlightingFeature implements vscodelc.StaticFeature { 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. -- 2.7.4