Skip to content

Commit

Permalink
Guarantee that ClangdContext.client is null
Browse files Browse the repository at this point in the history
The code is reorganized slightly so that in the cases where the client
would be null, we don't create a ClangdContext object in the first place
  • Loading branch information
HighCommander4 committed Nov 17, 2024
1 parent af32b9b commit e5e25c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
24 changes: 16 additions & 8 deletions src/clangd-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,27 @@ class EnableEditsNearCursorFeature implements vscodelc.StaticFeature {
dispose() {}
}

export async function createContext(globalStoragePath: string,
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.subscriptions, 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
22 changes: 13 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,25 @@ 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);
if (clangdContext)
context.subscriptions.push(clangdContext);

shouldCheck = vscode.workspace.getConfiguration('clangd').get<boolean>(
'detectExtensionConflicts') ??
Expand Down Expand Up @@ -83,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 e5e25c6

Please sign in to comment.