Skip to content

Commit f2ce829

Browse files
committed
refactor(editor): LanguageClient can be undefined
1 parent 1b30150 commit f2ce829

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

editors/vscode/client/extension.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const enum LspCommands {
4141
FixAll = 'oxc.fixAll',
4242
}
4343

44-
let client: LanguageClient;
44+
let client: LanguageClient | undefined;
4545

4646
let myStatusBarItem: StatusBarItem;
4747

@@ -50,7 +50,7 @@ export async function activate(context: ExtensionContext) {
5050
const restartCommand = commands.registerCommand(
5151
OxcCommands.RestartServer,
5252
async () => {
53-
if (!client) {
53+
if (client === undefined) {
5454
window.showErrorMessage('oxc client not found');
5555
return;
5656
}
@@ -65,7 +65,7 @@ export async function activate(context: ExtensionContext) {
6565

6666
window.showInformationMessage('oxc server restarted.');
6767
} else {
68-
await startClient();
68+
await startClient(client);
6969
}
7070
} catch (err) {
7171
client.error('Restarting client failed', err, 'force');
@@ -85,13 +85,17 @@ export async function activate(context: ExtensionContext) {
8585
async () => {
8686
await configService.config.updateEnable(!configService.config.enable);
8787

88+
if (client === undefined) {
89+
return;
90+
}
91+
8892
if (client.isRunning()) {
8993
if (!configService.config.enable) {
9094
await client.stop();
9195
}
9296
} else {
9397
if (configService.config.enable) {
94-
await startClient();
98+
await startClient(client);
9599
}
96100
}
97101
},
@@ -239,15 +243,19 @@ export async function activate(context: ExtensionContext) {
239243
});
240244

241245
workspace.onDidDeleteFiles((event) => {
242-
event.files.forEach((fileUri) => {
243-
client.diagnostics?.delete(fileUri);
244-
});
246+
for (const fileUri of event.files) {
247+
client?.diagnostics?.delete(fileUri);
248+
}
245249
});
246250

247251
configService.onConfigChange = async function onConfigChange(event) {
248252
let settings = this.config.toLanguageServerConfig();
249253
updateStatsBar(this.config.enable);
250254

255+
if (client === undefined) {
256+
return;
257+
}
258+
251259
if (client.isRunning()) {
252260
client.sendNotification('workspace/didChangeConfiguration', { settings });
253261
}
@@ -286,12 +294,15 @@ export async function activate(context: ExtensionContext) {
286294
updateStatsBar(configService.config.enable);
287295

288296
if (configService.config.enable) {
289-
await startClient();
297+
await startClient(client);
290298
}
291299
}
292300

293301
// Starts the client, it does not check if it is already started
294-
async function startClient() {
302+
async function startClient(client: LanguageClient | undefined) {
303+
if (client === undefined) {
304+
return;
305+
}
295306
await client.start();
296307
// ToDo: refactor it on the server side.
297308
// Do not touch watchers on client side, just simplify the start of the server.

0 commit comments

Comments
 (0)