Skip to content

Commit

Permalink
Update the API to reflect that the language client could be null
Browse files Browse the repository at this point in the history
  • Loading branch information
HighCommander4 committed Nov 17, 2024
1 parent e5e25c6 commit 8615020
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
5 changes: 5 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion api/vscode-clangd.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}));

Expand Down Expand Up @@ -87,6 +87,6 @@ export async function activate(context: vscode.ExtensionContext):
}, 5000);
}

apiInstance = new ClangdExtensionImpl(clangdContext!.client);
apiInstance = new ClangdExtensionImpl(clangdContext?.client);
return apiInstance;
}

0 comments on commit 8615020

Please sign in to comment.