-
Notifications
You must be signed in to change notification settings - Fork 550
/
Copy pathcairols.ts
249 lines (208 loc) · 7.19 KB
/
cairols.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import * as vscode from "vscode";
import { SemanticTokensFeature } from "vscode-languageclient/lib/common/semanticTokens";
import * as lc from "vscode-languageclient/node";
import { Context } from "./context";
import { Scarb } from "./scarb";
import { isScarbProject } from "./scarbProject";
import { StandaloneLS } from "./standalonels";
import { registerMacroExpandProvider, registerVfsProvider } from "./textDocumentProviders";
import { NotificationType } from "vscode-jsonrpc/lib/common/messages";
export interface LanguageServerExecutableProvider {
languageServerExecutable(): lc.Executable;
}
function notifyScarbMissing(ctx: Context) {
const errorMessage =
"This is a Scarb project, but could not find Scarb executable on this machine. " +
"Please add Scarb to the PATH environmental variable or set the 'cairo1.scarbPath' configuration " +
"parameter. Otherwise Cairo code analysis will not work.";
void vscode.window.showWarningMessage(errorMessage);
ctx.log.error(errorMessage);
}
export async function setupLanguageServer(ctx: Context): Promise<lc.LanguageClient> {
const serverOptions = await getServerOptions(ctx);
const clientOptions: lc.LanguageClientOptions = {
documentSelector: [
{ scheme: "file", language: "cairo" },
{ scheme: "vfs", language: "cairo" },
],
};
const client = new lc.LanguageClient(
"cairoLanguageServer",
"Cairo Language Server",
serverOptions,
clientOptions,
);
// Notify the server when client configuration changes.
// CairoLS pulls configuration properties it is interested in by itself, so it
// is not needed to attach any details in the notification payload.
const weakClient = new WeakRef(client);
ctx.extension.subscriptions.push(
vscode.workspace.onDidChangeConfiguration(
async () => {
const client = weakClient.deref();
if (client != undefined) {
await client.sendNotification(lc.DidChangeConfigurationNotification.type, {
settings: "",
});
}
},
null,
ctx.extension.subscriptions,
),
);
client.registerFeature(new SemanticTokensFeature(client));
registerVfsProvider(client, ctx);
registerMacroExpandProvider(client, ctx);
client.onNotification("scarb/could-not-find-scarb-executable", () => notifyScarbMissing(ctx));
client.onNotification("scarb/resolving-start", () => {
vscode.window.withProgress(
{
title: "Scarb is resolving the project...",
location: vscode.ProgressLocation.Notification,
cancellable: false,
},
async () => {
return new Promise((resolve) => {
client.onNotification("scarb/resolving-finish", () => {
resolve(null);
});
});
},
);
});
client.onNotification(
new NotificationType<string>("cairo/corelib-version-mismatch"),
(errMessage) => {
const errorMessage =
"Core crate version mismatch. If you are using Scarb try reopening the project to fix this error. Resort to `scarb cache clean` if it doesn't help. ERROR: " +
errMessage;
vscode.window.showErrorMessage(errorMessage);
ctx.log.error(errorMessage);
},
);
await client.start();
return client;
}
async function getServerOptions(ctx: Context): Promise<lc.ServerOptions> {
// TODO(mkaput): Support multi-root workspaces.
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
const isScarbEnabled = ctx.config.get("enableScarb", false);
let scarb: Scarb | undefined;
if (!isScarbEnabled) {
ctx.log.warn("Scarb integration is disabled");
ctx.log.warn("note: set `cairo1.enableScarb` to `true` to enable it");
} else {
try {
scarb = await Scarb.find(workspaceFolder, ctx);
} catch (e) {
ctx.log.error(`${e}`);
ctx.log.error("note: Scarb integration is disabled due to this error");
}
}
let serverExecutableProvider: LanguageServerExecutableProvider | undefined;
try {
serverExecutableProvider = await determineLanguageServerExecutableProvider(
workspaceFolder,
scarb,
ctx,
);
} catch (e) {
ctx.log.error(`${e}`);
}
const run = serverExecutableProvider?.languageServerExecutable();
if (run == undefined) {
ctx.log.error("failed to start CairoLS");
throw new Error("failed to start CairoLS");
}
setupEnv(run, ctx);
ctx.log.debug(`using CairoLS: ${quoteServerExecutable(run)}`);
return { run, debug: run };
}
async function determineLanguageServerExecutableProvider(
workspaceFolder: vscode.WorkspaceFolder | undefined,
scarb: Scarb | undefined,
ctx: Context,
): Promise<LanguageServerExecutableProvider> {
const log = ctx.log.span("determineLanguageServerExecutableProvider");
const standalone = () => StandaloneLS.find(workspaceFolder, scarb, ctx);
if (!scarb) {
log.trace("Scarb is missing");
return await standalone();
}
if (await isScarbProject()) {
log.trace("this is a Scarb project");
if (!ctx.config.get("preferScarbLanguageServer", true)) {
log.trace("`preferScarbLanguageServer` is false, using standalone LS");
return await standalone();
}
if (await scarb.hasCairoLS(ctx)) {
log.trace("using Scarb LS");
return scarb;
}
log.trace("Scarb has no LS extension, falling back to standalone");
return await standalone();
} else {
log.trace("this is *not* a Scarb project, looking for standalone LS");
try {
return await standalone();
} catch (e) {
log.trace("could not find standalone LS, trying Scarb LS");
if (await scarb.hasCairoLS(ctx)) {
log.trace("using Scarb LS");
return scarb;
}
log.trace("could not find standalone LS and Scarb has no LS extension, will error out");
throw e;
}
}
}
function setupEnv(serverExecutable: lc.Executable, ctx: Context) {
const logEnv = {
CAIRO_LS_LOG: buildEnvFilter(ctx),
RUST_BACKTRACE: "1",
};
const extraEnv = ctx.config.get("languageServerExtraEnv");
serverExecutable.options ??= {};
serverExecutable.options.env = {
// Inherit env from parent process.
...process.env,
...(serverExecutable.options.env ?? {}),
...logEnv,
...extraEnv,
};
}
function buildEnvFilter(ctx: Context): string {
const level = convertVscodeLogLevelToRust(ctx.log.logLevel);
return level ? `cairo_lang_language_server=${level}` : "";
}
function convertVscodeLogLevelToRust(logLevel: vscode.LogLevel): string | null {
switch (logLevel) {
case vscode.LogLevel.Trace:
return "trace";
case vscode.LogLevel.Debug:
return "debug";
case vscode.LogLevel.Info:
return "info";
case vscode.LogLevel.Warning:
return "warn";
case vscode.LogLevel.Error:
return "error";
case vscode.LogLevel.Off:
return null;
}
}
function quoteServerExecutable(serverExecutable: lc.Executable): string {
const parts: string[] = [];
if (serverExecutable.options?.env) {
for (const [key, value] of Object.entries(serverExecutable.options.env)) {
parts.push(`${key}=${value}`);
}
}
parts.push(serverExecutable.command);
if (serverExecutable.args) {
for (const arg of serverExecutable.args) {
parts.push(arg);
}
}
return parts.filter((s) => s.trim()).join(" ");
}