From 86150203fa8a14962a3d21551b7903aff9ff0880 Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Sat, 16 Nov 2024 23:10:43 -0500 Subject: [PATCH] Update the API to reflect that the language client could be null --- api/README.md | 5 +++++ api/vscode-clangd.d.ts | 2 +- src/api.ts | 2 +- src/extension.ts | 4 ++-- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/api/README.md b/api/README.md index f3e95d2..dd2c6fc 100644 --- a/api/README.md +++ b/api/README.md @@ -17,6 +17,11 @@ const provideHover = async (document: vscode.TextDocument, position: vscode.Posi if (clangdExtension) { const api = (await clangdExtension.activate()).getApi(CLANGD_API_VERSION); + + // Extension may be disabled or have failed to initialize + if (!api.languageClient) { + return undefined; + } const textDocument = api.languageClient.code2ProtocolConverter.asTextDocumentIdentifier(document); const range = api.languageClient.code2ProtocolConverter.asRange(new vscode.Range(position, position)); diff --git a/api/vscode-clangd.d.ts b/api/vscode-clangd.d.ts index 1bbbda4..18aa2e5 100644 --- a/api/vscode-clangd.d.ts +++ b/api/vscode-clangd.d.ts @@ -8,7 +8,7 @@ export interface ClangdApiV1 { // https://microsoft.github.io/language-server-protocol/specifications/specification-current // clangd custom requests: // https://clangd.llvm.org/extensions - languageClient: BaseLanguageClient + languageClient: BaseLanguageClient | undefined } export interface ClangdExtension { diff --git a/src/api.ts b/src/api.ts index fa9b443..a54d371 100644 --- a/src/api.ts +++ b/src/api.ts @@ -3,7 +3,7 @@ import {BaseLanguageClient} from 'vscode-languageclient'; import {ClangdApiV1, ClangdExtension} from '../api/vscode-clangd'; export class ClangdExtensionImpl implements ClangdExtension { - constructor(public client: BaseLanguageClient) {} + constructor(public client: BaseLanguageClient | undefined) {} public getApi(version: 1): ClangdApiV1; public getApi(version: number): unknown { diff --git a/src/extension.ts b/src/extension.ts index cf279e9..df16515 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -39,7 +39,7 @@ export async function activate(context: vscode.ExtensionContext): if (clangdContext) context.subscriptions.push(clangdContext); if (apiInstance) { - apiInstance.client = clangdContext!.client; + apiInstance.client = clangdContext?.client; } })); @@ -87,6 +87,6 @@ export async function activate(context: vscode.ExtensionContext): }, 5000); } - apiInstance = new ClangdExtensionImpl(clangdContext!.client); + apiInstance = new ClangdExtensionImpl(clangdContext?.client); return apiInstance; }