Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support API usage when clangd is disabled or failed to initialize properly #728

Merged
merged 7 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 17 additions & 8 deletions src/clangd-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,28 @@ class EnableEditsNearCursorFeature implements vscodelc.StaticFeature {
dispose() {}
}

export async function createContext(globalStoragePath: string,
HighCommander4 marked this conversation as resolved.
Show resolved Hide resolved
outputChannel: vscode.OutputChannel):
Promise<ClangdContext|null> {
const subscriptions: vscode.Disposable[] = [];
const clangdPath = await install.activate(subscriptions, globalStoragePath);
if (!clangdPath)
return null;

const clangdArguments = await config.get<string[]>('arguments');

return new ClangdContext(clangdPath, clangdArguments, outputChannel);
}

export class ClangdContext implements vscode.Disposable {
subscriptions: vscode.Disposable[] = [];
client!: ClangdLanguageClient;

async activate(globalStoragePath: string,
outputChannel: vscode.OutputChannel) {
const clangdPath = await install.activate(this, globalStoragePath);
if (!clangdPath)
return;
client: ClangdLanguageClient;

constructor(clangdPath: string, clangdArguments: string[],
outputChannel: vscode.OutputChannel) {
const clangd: vscodelc.Executable = {
command: clangdPath,
args: await config.get<string[]>('arguments'),
args: clangdArguments,
options: {cwd: vscode.workspace.rootPath || process.cwd()}
};
const traceFile = config.get<string>('trace');
Expand Down
24 changes: 15 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as vscode from 'vscode';
import {ClangdExtension} from '../api/vscode-clangd';

import {ClangdExtensionImpl} from './api';
import {ClangdContext} from './clangd-context';
import {ClangdContext, createContext} from './clangd-context';

let apiInstance: ClangdExtensionImpl|undefined;

Expand All @@ -16,8 +16,7 @@ export async function activate(context: vscode.ExtensionContext):
const outputChannel = vscode.window.createOutputChannel('clangd');
context.subscriptions.push(outputChannel);

const clangdContext = new ClangdContext;
context.subscriptions.push(clangdContext);
let clangdContext: ClangdContext|null = null;

// An empty place holder for the activate command, otherwise we'll get an
// "command is not registered" error.
Expand All @@ -31,20 +30,27 @@ export async function activate(context: vscode.ExtensionContext):
// stop/start cycle in this situation is pointless, and doesn't work
// anyways because the client can't be stop()-ped when it's still in the
// Starting state).
if (clangdContext.clientIsStarting()) {
if (clangdContext && clangdContext.clientIsStarting()) {
return;
}
await clangdContext.dispose();
await clangdContext.activate(context.globalStoragePath, outputChannel);
if (clangdContext)
await clangdContext.dispose();
clangdContext =
await createContext(context.globalStoragePath, outputChannel);
if (clangdContext)
context.subscriptions.push(clangdContext);
if (apiInstance) {
apiInstance.client = clangdContext.client;
apiInstance.client = clangdContext?.client;
}
}));

let shouldCheck = false;

if (vscode.workspace.getConfiguration('clangd').get<boolean>('enable')) {
await clangdContext.activate(context.globalStoragePath, outputChannel);
clangdContext =
await createContext(context.globalStoragePath, outputChannel);
HighCommander4 marked this conversation as resolved.
Show resolved Hide resolved
if (clangdContext)
context.subscriptions.push(clangdContext);

shouldCheck = vscode.workspace.getConfiguration('clangd').get<boolean>(
'detectExtensionConflicts') ??
Expand Down Expand Up @@ -83,6 +89,6 @@ export async function activate(context: vscode.ExtensionContext):
}, 5000);
}

apiInstance = new ClangdExtensionImpl(clangdContext.client);
apiInstance = new ClangdExtensionImpl(clangdContext?.client);
return apiInstance;
}
17 changes: 8 additions & 9 deletions src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import AbortController from 'abort-controller';
import * as path from 'path';
import * as vscode from 'vscode';

import {ClangdContext} from './clangd-context';
import * as config from './config';

// Returns the clangd path to be used, or null if clangd is not installed.
export async function activate(
context: ClangdContext, globalStoragePath: string): Promise<string|null> {
const ui = new UI(context, globalStoragePath);
context.subscriptions.push(vscode.commands.registerCommand(
export async function activate(disposables: vscode.Disposable[],
globalStoragePath: string):
Promise<string|null> {
const ui = new UI(disposables, globalStoragePath);
disposables.push(vscode.commands.registerCommand(
'clangd.install', async () => common.installLatest(ui)));
context.subscriptions.push(vscode.commands.registerCommand(
disposables.push(vscode.commands.registerCommand(
'clangd.update', async () => common.checkUpdates(true, ui)));
const status = await common.prepare(ui, config.get<boolean>('checkUpdates'));
return status.clangdPath;
}

class UI {
constructor(private context: ClangdContext,
constructor(private disposables: vscode.Disposable[],
private globalStoragePath: string) {}

get storagePath(): string { return this.globalStoragePath; }
Expand Down Expand Up @@ -60,8 +60,7 @@ class UI {
error(s: string) { vscode.window.showErrorMessage(s); }
info(s: string) { vscode.window.showInformationMessage(s); }
command(name: string, body: () => any) {
this.context.subscriptions.push(
vscode.commands.registerCommand(name, body));
this.disposables.push(vscode.commands.registerCommand(name, body));
}

async shouldReuse(release: string): Promise<boolean|undefined> {
Expand Down
Loading