-
Notifications
You must be signed in to change notification settings - Fork 490
/
cairols.ts
375 lines (337 loc) · 10.4 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import * as child_process from "child_process";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import * as vscode from "vscode";
import { SemanticTokensFeature } from "vscode-languageclient/lib/common/semanticTokens";
import * as lc from "vscode-languageclient/node";
import { Context } from "./context";
// Tries to find the development version of the language server executable,
// assuming the workspace directory is inside the Cairo repository.
function findDevLanguageServerAt(
path: string,
depth: number,
): string | undefined {
if (depth == 0) {
return undefined;
}
let candidate = path + "/target/release/cairo-language-server";
if (fs.existsSync(candidate)) {
return candidate;
}
candidate = path + "/target/debug/cairo-language-server";
if (fs.existsSync(candidate)) {
return candidate;
}
return findDevLanguageServerAt(path + "/..", depth - 1);
}
function rootPath(ctx: Context): string {
let rootPath = ctx.extension.extensionPath;
const workspaceFolders = vscode.workspace.workspaceFolders;
if (workspaceFolders) {
rootPath = workspaceFolders[0]?.uri.path || rootPath;
}
return rootPath;
}
function isExecutable(path: string): boolean {
try {
fs.accessSync(path, fs.constants.X_OK);
return fs.statSync(path).isFile();
} catch (e) {
return false;
}
}
function replacePathPlaceholders(path: string, root: string): string {
return path
.replace(/\${workspaceFolder}/g, root)
.replace(/\${userHome}/g, process.env["HOME"] ?? "");
}
function findLanguageServerExecutable(ctx: Context) {
const root = rootPath(ctx);
const configPath = ctx.config.get<string>("languageServerPath");
if (configPath) {
const serverPath = replacePathPlaceholders(configPath, root);
if (!isExecutable(serverPath)) {
return undefined;
}
return serverPath;
}
// TODO(spapini): Use a bundled language server.
return findDevLanguageServerAt(root, 10);
}
async function findExecutableFromPathVar(name: string) {
const envPath = process.env["PATH"] || "";
const envExt = process.env["PATHEXT"] || "";
const pathDirs = envPath
.replace(/["]+/g, "")
.split(path.delimiter)
.filter(Boolean);
const extensions = envExt.split(";");
const candidates: string[] = [];
pathDirs.map((d) =>
extensions.map((ext) => {
candidates.push(path.join(d, name + ext));
}),
);
const isExecutable = (path: string) =>
fs.promises
.access(path, fs.constants.X_OK)
.then(() => path)
.catch(() => undefined);
try {
return await Promise.all(candidates.map(isExecutable)).then((values) =>
values.find((value) => !!value),
);
} catch (e) {
return undefined;
}
}
async function findScarbExecutablePathInAsdfDir() {
if (os.platform() === "win32") {
return undefined;
}
let asdfDataDir = process.env["ASDF_DATA_DIR"];
if (!asdfDataDir) {
const home = process.env["HOME"];
if (!home) {
return undefined;
}
asdfDataDir = path.join(home, ".asdf");
}
const scarbExecutablePath = path.join(asdfDataDir, "shims", "scarb");
try {
await fs.promises.access(scarbExecutablePath, fs.constants.X_OK);
return scarbExecutablePath;
} catch (e) {
return undefined;
}
}
async function findScarbExecutablePath(ctx: Context) {
// Check config for scarb path.
const root = rootPath(ctx);
const configPath = ctx.config.get<string>("scarbPath");
if (configPath) {
const scarbPath = replacePathPlaceholders(configPath, root);
if (!isExecutable(scarbPath)) {
return undefined;
}
return scarbPath;
}
// Check PATH env var for scarb path.
const envPath = await findExecutableFromPathVar("scarb");
if (envPath) return envPath;
return findScarbExecutablePathInAsdfDir();
}
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);
}
async function listScarbCommandsOutput(
scarbPath: undefined | string,
ctx: Context,
) {
if (!scarbPath) {
return undefined;
}
const child = child_process.spawn(scarbPath, ["--json", "commands"], {
stdio: "pipe",
cwd: rootPath(ctx),
});
let stdout = "";
for await (const chunk of child.stdout) {
stdout += chunk;
}
return stdout;
}
async function isScarbLsPresent(
scarbPath: undefined | string,
ctx: Context,
): Promise<boolean> {
if (!scarbPath) {
return false;
}
const scarbOutput = await listScarbCommandsOutput(scarbPath, ctx);
if (!scarbOutput) return false;
return scarbOutput
.split("\n")
.map((v) => v.trim())
.filter((v) => !!v)
.map((v) => JSON.parse(v))
.some(
(commands: Record<string, unknown>) =>
!!commands["cairo-language-server"],
);
}
async function runStandaloneLs(
scarbPath: undefined | string,
ctx: Context,
): Promise<undefined | child_process.ChildProcessWithoutNullStreams> {
const executable = findLanguageServerExecutable(ctx);
if (!executable) {
ctx.log.error("could not find Cairo language server executable");
ctx.log.error(
"note: make sure CairoLS is installed and `cairo1.languageServerPath` points to it",
);
return;
}
ctx.log.debug(`using CairoLS: ${executable}`);
return child_process.spawn(executable, {
env: { SCARB: scarbPath },
});
}
async function runScarbLs(
scarbPath: undefined | string,
ctx: Context,
): Promise<undefined | child_process.ChildProcessWithoutNullStreams> {
if (!scarbPath) {
return;
}
ctx.log.debug(`using CairoLS: ${scarbPath} cairo-language-server`);
return child_process.spawn(scarbPath, ["cairo-language-server"], {
cwd: rootPath(ctx),
});
}
enum ServerType {
Standalone,
Scarb,
}
async function getServerType(
isScarbEnabled: boolean,
scarbPath: string | undefined,
configLanguageServerPath: string | undefined,
ctx: Context,
) {
if (!isScarbEnabled) return ServerType.Standalone;
if (!(await isScarbProject()) && !!configLanguageServerPath) {
// If Scarb manifest is missing, and Cairo-LS path is explicit.
return ServerType.Standalone;
}
if (await isScarbLsPresent(scarbPath, ctx)) return ServerType.Scarb;
return ServerType.Standalone;
}
async function isScarbProjectAt(path: string, depth: number): Promise<boolean> {
if (depth == 0) return false;
const isFile = await fs.promises
.access(path + "/Scarb.toml", fs.constants.F_OK)
.then(() => true)
.catch(() => false);
if (isFile) return true;
return isScarbProjectAt(path + "/..", depth - 1);
}
async function isScarbProject(): Promise<boolean> {
const depth = 20;
const workspaceFolders = vscode.workspace.workspaceFolders;
if (
!!workspaceFolders?.[0] &&
(await isScarbProjectAt(path.dirname(workspaceFolders[0].uri.path), depth))
)
return true;
const editor = vscode.window.activeTextEditor;
return (
!!editor &&
(await isScarbProjectAt(path.dirname(editor.document.uri.path), depth))
);
}
export async function setupLanguageServer(
ctx: Context,
): Promise<lc.LanguageClient> {
const isScarbEnabled = ctx.config.get<boolean>("enableScarb", false);
const scarbPath = await findScarbExecutablePath(ctx);
const configLanguageServerPath = ctx.config.get<string>("languageServerPath");
if (!isScarbEnabled) {
ctx.log.warn("Scarb integration is disabled");
ctx.log.warn("note: set `cairo1.enableScarb` to `true` to enable it");
} else if (!scarbPath) {
ctx.log.error("could not find Scarb executable on this machine");
} else {
ctx.log.debug(`using Scarb: ${scarbPath}`);
}
const serverOptions: lc.ServerOptions =
async (): Promise<child_process.ChildProcessWithoutNullStreams> => {
const serverType = await getServerType(
isScarbEnabled,
scarbPath,
configLanguageServerPath,
ctx,
);
let child;
if (serverType === ServerType.Scarb) {
child = await runScarbLs(scarbPath, ctx);
} else {
child = await runStandaloneLs(scarbPath, ctx);
}
if (!child) {
ctx.log.error("failed to start Cairo language server");
throw new Error("Failed to start Cairo language server.");
}
// Forward stderr to vscode logs.
child.stderr.on("data", (data: Buffer) => {
ctx.log.trace("Server stderr> " + data.toString());
});
child.on("exit", (code, signal) => {
ctx.log.debug(
`Cairo language server exited with code ${code} and signal ${signal}`,
);
});
// Create a resolved promise with the child process.
return child;
};
const clientOptions: lc.LanguageClientOptions = {
documentSelector: [
{ scheme: "file", language: "cairo" },
{ scheme: "vfs", language: "cairo" },
],
};
const client = new lc.LanguageClient(
"cairoLanguageServer",
"Cairo Language Server",
serverOptions,
clientOptions,
);
client.registerFeature(new SemanticTokensFeature(client));
const myProvider = new (class implements vscode.TextDocumentContentProvider {
async provideTextDocumentContent(uri: vscode.Uri): Promise<string> {
interface ProvideVirtualFileResponse {
content?: string;
}
const res = await client.sendRequest<ProvideVirtualFileResponse>(
"vfs/provide",
{
uri: uri.toString(),
},
);
return res.content ?? "";
}
onDidChangeEmitter = new vscode.EventEmitter<vscode.Uri>();
onDidChange = this.onDidChangeEmitter.event;
})();
client.onNotification("vfs/update", (param) => {
myProvider.onDidChangeEmitter.fire(param.uri);
});
vscode.workspace.registerTextDocumentContentProvider("vfs", myProvider);
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);
});
});
},
);
});
await client.start();
return client;
}