-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.ts
51 lines (46 loc) · 1.48 KB
/
repl.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
import type { LSPXServer } from "./types.ts";
import { readline } from "./readline.ts";
import process from "node:process";
import completions from "./completions.json" with { type: "json" };
import { each, spawn } from "effection";
export function* repl(server: LSPXServer) {
yield* spawn(function* () {
for (let notification of yield* each(server.notifications)) {
let { method, params } = notification;
console.log(`Notification: ${method} ${JSON.stringify(params, null, 2)}`);
yield* each.next();
}
});
let lines = readline({
prompt: "LSP> ",
input: process.stdin,
output: process.stdout,
completer: (line: string) => {
let hits = completions.filter((c) => c.startsWith(line));
// Show all completions if none foundp
return [hits.length ? hits : completions, line];
},
});
for (let line of yield* each(lines)) {
let pattern = /([\w\/]+)\((.*)\)/;
let match = pattern.exec(line);
if (match) {
try {
const method = match[1];
let params = JSON.parse(`[${match[2]}]`);
console.log(
`Sending request ${method} with params ${JSON.stringify(params)}...`,
);
let response = yield* server.request(method, ...params);
console.log(JSON.stringify(response, null, 2));
} catch (e) {
console.log(e);
}
} else {
console.log(
'invoke an LSP command. Example: initialize({"capabilities": {}})',
);
}
yield* each.next();
}
}