This repository has been archived by the owner on Mar 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
76 lines (69 loc) · 2.13 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import * as vscode from "vscode";
import type * as lc from "vscode-languageclient/node";
import * as commands from "./commands";
import { type CommandFactory, Ctx, fetchWorkspace } from "./ctx";
import { setContextValue } from "./util";
const KCL_PROJECT_CONTEXT_NAME = "inKclProject";
export interface KclAnalyzerExtensionApi {
readonly client?: lc.LanguageClient;
}
export async function deactivate() {
await setContextValue(KCL_PROJECT_CONTEXT_NAME, undefined);
}
export async function activate(context: vscode.ExtensionContext): Promise<KclAnalyzerExtensionApi> {
const ctx = new Ctx(context, createCommands(), fetchWorkspace());
// VS Code doesn't show a notification when an extension fails to activate
// so we do it ourselves.
const api = await activateServer(ctx).catch((err) => {
void vscode.window.showErrorMessage(
`Cannot activate kcl-language-server extension: ${err.message}`,
);
throw err;
});
await setContextValue(KCL_PROJECT_CONTEXT_NAME, true);
return api;
}
async function activateServer(ctx: Ctx): Promise<KclAnalyzerExtensionApi> {
await ctx.start();
return ctx;
}
function createCommands(): Record<string, CommandFactory> {
return {
restartServer: {
enabled: (ctx) => async () => {
await ctx.restart();
},
disabled: (ctx) => async () => {
await ctx.start();
},
},
startServer: {
enabled: (ctx) => async () => {
await ctx.start();
ctx.setServerStatus({
health: "ok",
quiescent: true,
});
},
disabled: (ctx) => async () => {
await ctx.start();
ctx.setServerStatus({
health: "ok",
quiescent: true,
});
},
},
stopServer: {
enabled: (ctx) => async () => {
// FIXME: We should reuse the client, that is ctx.deactivate() if none of the configs have changed
await ctx.stopAndDispose();
ctx.setServerStatus({
health: "stopped",
});
},
disabled: (_) => async () => {},
},
serverVersion: { enabled: commands.serverVersion },
openLogs: { enabled: commands.openLogs },
};
}