-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
132 lines (110 loc) · 2.69 KB
/
validate.js
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
// @ts-check
const { spawn } = require("child_process");
const { nanoid } = require("nanoid");
const lsp = spawn("elyra-pipeline-lsp");
let responseStore = {};
let notifications = [];
let remainder = 0;
let buff = "";
function processMessage(rest) {
while (remainder > 0) {
const c = rest.charAt(0);
rest = rest.substring(1);
if (c === "") {
break;
}
remainder = remainder - 1;
buff += c;
if (remainder === 0) {
const message = JSON.parse(buff);
if (message.id !== undefined) {
responseStore[message.id] = message;
} else {
notifications.push(message);
}
buff = "";
}
}
return rest;
}
lsp.stdout.on("data", (data) => {
let rest = processMessage(data.toString());
const r = /Content-Length: (\d+)\r\n\r\n/;
const rAll = /Content-Length: (\d+)\r\n\r\n/g;
const matches = rest.toString().matchAll(rAll);
for (const match of matches) {
remainder = match[1];
rest = rest.replace(r, "");
rest = processMessage(rest);
}
});
/**
* @param {string} method
* @param {Object} params
*/
function sendNotification(method, params) {
const message = JSON.stringify({
jsonrpc: "2.0",
method,
params,
});
lsp.stdin.write(`Content-Length: ${message.length}\r\n`);
lsp.stdin.write("\r\n");
lsp.stdin.write(message);
}
/**
* @param {string} method
* @param {Object} params
*/
function sendRequest(method, params) {
const id = nanoid();
const promise = new Promise((resolve) => {
function handleData() {
const res = responseStore[id];
if (res !== undefined) {
lsp.stdout.removeListener("data", handleData);
delete responseStore[id];
resolve(res);
}
}
lsp.stdout.on("data", handleData);
});
const message = JSON.stringify({
jsonrpc: "2.0",
id,
method,
params,
});
lsp.stdin.write(`Content-Length: ${message.length}\r\n`);
lsp.stdin.write("\r\n");
lsp.stdin.write(message);
return promise;
}
async function initialize() {
await sendRequest("initialize", {});
sendNotification("initialized", {});
}
/**
* @param {string} content
*/
async function validate(content) {
const promise = new Promise((resolve) => {
function handleData() {
const notification = notifications.pop();
if (notification?.method === "textDocument/publishDiagnostics") {
lsp.stdout.removeListener("data", handleData);
resolve(notification);
}
}
lsp.stdout.on("data", handleData);
});
sendNotification("textDocument/didOpen", {
textDocument: {
version: 0,
text: content,
},
});
return promise;
}
module.exports.initialize = initialize;
module.exports.validate = validate;