@lvce-editor/completion-worker 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/completionWorkerMain.js +1406 -0
- package/package.json +13 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Lvce Editor
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,1406 @@
|
|
|
1
|
+
const normalizeLine = line => {
|
|
2
|
+
if (line.startsWith('Error: ')) {
|
|
3
|
+
return line.slice('Error: '.length);
|
|
4
|
+
}
|
|
5
|
+
if (line.startsWith('VError: ')) {
|
|
6
|
+
return line.slice('VError: '.length);
|
|
7
|
+
}
|
|
8
|
+
return line;
|
|
9
|
+
};
|
|
10
|
+
const getCombinedMessage = (error, message) => {
|
|
11
|
+
const stringifiedError = normalizeLine(`${error}`);
|
|
12
|
+
if (message) {
|
|
13
|
+
return `${message}: ${stringifiedError}`;
|
|
14
|
+
}
|
|
15
|
+
return stringifiedError;
|
|
16
|
+
};
|
|
17
|
+
const NewLine$2 = '\n';
|
|
18
|
+
const getNewLineIndex$1 = (string, startIndex = undefined) => {
|
|
19
|
+
return string.indexOf(NewLine$2, startIndex);
|
|
20
|
+
};
|
|
21
|
+
const mergeStacks = (parent, child) => {
|
|
22
|
+
if (!child) {
|
|
23
|
+
return parent;
|
|
24
|
+
}
|
|
25
|
+
const parentNewLineIndex = getNewLineIndex$1(parent);
|
|
26
|
+
const childNewLineIndex = getNewLineIndex$1(child);
|
|
27
|
+
if (childNewLineIndex === -1) {
|
|
28
|
+
return parent;
|
|
29
|
+
}
|
|
30
|
+
const parentFirstLine = parent.slice(0, parentNewLineIndex);
|
|
31
|
+
const childRest = child.slice(childNewLineIndex);
|
|
32
|
+
const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
|
|
33
|
+
if (parentFirstLine.includes(childFirstLine)) {
|
|
34
|
+
return parentFirstLine + childRest;
|
|
35
|
+
}
|
|
36
|
+
return child;
|
|
37
|
+
};
|
|
38
|
+
class VError extends Error {
|
|
39
|
+
constructor(error, message) {
|
|
40
|
+
const combinedMessage = getCombinedMessage(error, message);
|
|
41
|
+
super(combinedMessage);
|
|
42
|
+
this.name = 'VError';
|
|
43
|
+
if (error instanceof Error) {
|
|
44
|
+
this.stack = mergeStacks(this.stack, error.stack);
|
|
45
|
+
}
|
|
46
|
+
if (error.codeFrame) {
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
this.codeFrame = error.codeFrame;
|
|
49
|
+
}
|
|
50
|
+
if (error.code) {
|
|
51
|
+
// @ts-ignore
|
|
52
|
+
this.code = error.code;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
class AssertionError extends Error {
|
|
58
|
+
constructor(message) {
|
|
59
|
+
super(message);
|
|
60
|
+
this.name = 'AssertionError';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const getType = value => {
|
|
64
|
+
switch (typeof value) {
|
|
65
|
+
case 'number':
|
|
66
|
+
return 'number';
|
|
67
|
+
case 'function':
|
|
68
|
+
return 'function';
|
|
69
|
+
case 'string':
|
|
70
|
+
return 'string';
|
|
71
|
+
case 'object':
|
|
72
|
+
if (value === null) {
|
|
73
|
+
return 'null';
|
|
74
|
+
}
|
|
75
|
+
if (Array.isArray(value)) {
|
|
76
|
+
return 'array';
|
|
77
|
+
}
|
|
78
|
+
return 'object';
|
|
79
|
+
case 'boolean':
|
|
80
|
+
return 'boolean';
|
|
81
|
+
default:
|
|
82
|
+
return 'unknown';
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const number = value => {
|
|
86
|
+
const type = getType(value);
|
|
87
|
+
if (type !== 'number') {
|
|
88
|
+
throw new AssertionError('expected value to be of type number');
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const isMessagePort = value => {
|
|
93
|
+
return value && value instanceof MessagePort;
|
|
94
|
+
};
|
|
95
|
+
const isMessagePortMain = value => {
|
|
96
|
+
return value && value.constructor && value.constructor.name === 'MessagePortMain';
|
|
97
|
+
};
|
|
98
|
+
const isOffscreenCanvas = value => {
|
|
99
|
+
return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
|
|
100
|
+
};
|
|
101
|
+
const isInstanceOf = (value, constructorName) => {
|
|
102
|
+
return value?.constructor?.name === constructorName;
|
|
103
|
+
};
|
|
104
|
+
const isSocket = value => {
|
|
105
|
+
return isInstanceOf(value, 'Socket');
|
|
106
|
+
};
|
|
107
|
+
const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
|
|
108
|
+
const isTransferrable = value => {
|
|
109
|
+
for (const fn of transferrables) {
|
|
110
|
+
if (fn(value)) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
};
|
|
116
|
+
const walkValue = (value, transferrables, isTransferrable) => {
|
|
117
|
+
if (!value) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (isTransferrable(value)) {
|
|
121
|
+
transferrables.push(value);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (Array.isArray(value)) {
|
|
125
|
+
for (const item of value) {
|
|
126
|
+
walkValue(item, transferrables, isTransferrable);
|
|
127
|
+
}
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (typeof value === 'object') {
|
|
131
|
+
for (const property of Object.values(value)) {
|
|
132
|
+
walkValue(property, transferrables, isTransferrable);
|
|
133
|
+
}
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
const getTransferrables = value => {
|
|
138
|
+
const transferrables = [];
|
|
139
|
+
walkValue(value, transferrables, isTransferrable);
|
|
140
|
+
return transferrables;
|
|
141
|
+
};
|
|
142
|
+
const attachEvents = that => {
|
|
143
|
+
const handleMessage = (...args) => {
|
|
144
|
+
const data = that.getData(...args);
|
|
145
|
+
that.dispatchEvent(new MessageEvent('message', {
|
|
146
|
+
data
|
|
147
|
+
}));
|
|
148
|
+
};
|
|
149
|
+
that.onMessage(handleMessage);
|
|
150
|
+
const handleClose = event => {
|
|
151
|
+
that.dispatchEvent(new Event('close'));
|
|
152
|
+
};
|
|
153
|
+
that.onClose(handleClose);
|
|
154
|
+
};
|
|
155
|
+
class Ipc extends EventTarget {
|
|
156
|
+
constructor(rawIpc) {
|
|
157
|
+
super();
|
|
158
|
+
this._rawIpc = rawIpc;
|
|
159
|
+
attachEvents(this);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
|
|
163
|
+
const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
|
|
164
|
+
const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
|
|
165
|
+
const NewLine$1 = '\n';
|
|
166
|
+
const joinLines$1 = lines => {
|
|
167
|
+
return lines.join(NewLine$1);
|
|
168
|
+
};
|
|
169
|
+
const RE_AT = /^\s+at/;
|
|
170
|
+
const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
|
|
171
|
+
const isNormalStackLine = line => {
|
|
172
|
+
return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
|
|
173
|
+
};
|
|
174
|
+
const getDetails = lines => {
|
|
175
|
+
const index = lines.findIndex(isNormalStackLine);
|
|
176
|
+
if (index === -1) {
|
|
177
|
+
return {
|
|
178
|
+
actualMessage: joinLines$1(lines),
|
|
179
|
+
rest: []
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
let lastIndex = index - 1;
|
|
183
|
+
while (++lastIndex < lines.length) {
|
|
184
|
+
if (!isNormalStackLine(lines[lastIndex])) {
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
actualMessage: lines[index - 1],
|
|
190
|
+
rest: lines.slice(index, lastIndex)
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
const splitLines$1 = lines => {
|
|
194
|
+
return lines.split(NewLine$1);
|
|
195
|
+
};
|
|
196
|
+
const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
|
|
197
|
+
const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
|
|
198
|
+
const isMessageCodeBlockStartIndex = line => {
|
|
199
|
+
return RE_MESSAGE_CODE_BLOCK_START.test(line);
|
|
200
|
+
};
|
|
201
|
+
const isMessageCodeBlockEndIndex = line => {
|
|
202
|
+
return RE_MESSAGE_CODE_BLOCK_END.test(line);
|
|
203
|
+
};
|
|
204
|
+
const getMessageCodeBlock = stderr => {
|
|
205
|
+
const lines = splitLines$1(stderr);
|
|
206
|
+
const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
|
|
207
|
+
const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
|
|
208
|
+
const relevantLines = lines.slice(startIndex, endIndex);
|
|
209
|
+
const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
|
|
210
|
+
return relevantMessage;
|
|
211
|
+
};
|
|
212
|
+
const isModuleNotFoundMessage = line => {
|
|
213
|
+
return line.includes('[ERR_MODULE_NOT_FOUND]');
|
|
214
|
+
};
|
|
215
|
+
const getModuleNotFoundError = stderr => {
|
|
216
|
+
const lines = splitLines$1(stderr);
|
|
217
|
+
const messageIndex = lines.findIndex(isModuleNotFoundMessage);
|
|
218
|
+
const message = lines[messageIndex];
|
|
219
|
+
return {
|
|
220
|
+
message,
|
|
221
|
+
code: ERR_MODULE_NOT_FOUND
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
const isModuleNotFoundError = stderr => {
|
|
225
|
+
if (!stderr) {
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
return stderr.includes('ERR_MODULE_NOT_FOUND');
|
|
229
|
+
};
|
|
230
|
+
const isModulesSyntaxError = stderr => {
|
|
231
|
+
if (!stderr) {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
return stderr.includes('SyntaxError: Cannot use import statement outside a module');
|
|
235
|
+
};
|
|
236
|
+
const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
|
|
237
|
+
const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
|
|
238
|
+
const isUnhelpfulNativeModuleError = stderr => {
|
|
239
|
+
return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
|
|
240
|
+
};
|
|
241
|
+
const getNativeModuleErrorMessage = stderr => {
|
|
242
|
+
const message = getMessageCodeBlock(stderr);
|
|
243
|
+
return {
|
|
244
|
+
message: `Incompatible native node module: ${message}`,
|
|
245
|
+
code: E_INCOMPATIBLE_NATIVE_MODULE
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
const getModuleSyntaxError = () => {
|
|
249
|
+
return {
|
|
250
|
+
message: `ES Modules are not supported in electron`,
|
|
251
|
+
code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
const getHelpfulChildProcessError = (stdout, stderr) => {
|
|
255
|
+
if (isUnhelpfulNativeModuleError(stderr)) {
|
|
256
|
+
return getNativeModuleErrorMessage(stderr);
|
|
257
|
+
}
|
|
258
|
+
if (isModulesSyntaxError(stderr)) {
|
|
259
|
+
return getModuleSyntaxError();
|
|
260
|
+
}
|
|
261
|
+
if (isModuleNotFoundError(stderr)) {
|
|
262
|
+
return getModuleNotFoundError(stderr);
|
|
263
|
+
}
|
|
264
|
+
const lines = splitLines$1(stderr);
|
|
265
|
+
const {
|
|
266
|
+
actualMessage,
|
|
267
|
+
rest
|
|
268
|
+
} = getDetails(lines);
|
|
269
|
+
return {
|
|
270
|
+
message: actualMessage,
|
|
271
|
+
code: '',
|
|
272
|
+
stack: rest
|
|
273
|
+
};
|
|
274
|
+
};
|
|
275
|
+
class IpcError extends VError {
|
|
276
|
+
// @ts-ignore
|
|
277
|
+
constructor(betterMessage, stdout = '', stderr = '') {
|
|
278
|
+
if (stdout || stderr) {
|
|
279
|
+
// @ts-ignore
|
|
280
|
+
const {
|
|
281
|
+
message,
|
|
282
|
+
code,
|
|
283
|
+
stack
|
|
284
|
+
} = getHelpfulChildProcessError(stdout, stderr);
|
|
285
|
+
const cause = new Error(message);
|
|
286
|
+
// @ts-ignore
|
|
287
|
+
cause.code = code;
|
|
288
|
+
cause.stack = stack;
|
|
289
|
+
super(cause, betterMessage);
|
|
290
|
+
} else {
|
|
291
|
+
super(betterMessage);
|
|
292
|
+
}
|
|
293
|
+
// @ts-ignore
|
|
294
|
+
this.name = 'IpcError';
|
|
295
|
+
// @ts-ignore
|
|
296
|
+
this.stdout = stdout;
|
|
297
|
+
// @ts-ignore
|
|
298
|
+
this.stderr = stderr;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
const readyMessage = 'ready';
|
|
302
|
+
const getData$2 = event => {
|
|
303
|
+
return event.data;
|
|
304
|
+
};
|
|
305
|
+
const listen$7 = () => {
|
|
306
|
+
// @ts-ignore
|
|
307
|
+
if (typeof WorkerGlobalScope === 'undefined') {
|
|
308
|
+
throw new TypeError('module is not in web worker scope');
|
|
309
|
+
}
|
|
310
|
+
return globalThis;
|
|
311
|
+
};
|
|
312
|
+
const signal$8 = global => {
|
|
313
|
+
global.postMessage(readyMessage);
|
|
314
|
+
};
|
|
315
|
+
class IpcChildWithModuleWorker extends Ipc {
|
|
316
|
+
getData(event) {
|
|
317
|
+
return getData$2(event);
|
|
318
|
+
}
|
|
319
|
+
send(message) {
|
|
320
|
+
// @ts-ignore
|
|
321
|
+
this._rawIpc.postMessage(message);
|
|
322
|
+
}
|
|
323
|
+
sendAndTransfer(message) {
|
|
324
|
+
const transfer = getTransferrables(message);
|
|
325
|
+
// @ts-ignore
|
|
326
|
+
this._rawIpc.postMessage(message, transfer);
|
|
327
|
+
}
|
|
328
|
+
dispose() {
|
|
329
|
+
// ignore
|
|
330
|
+
}
|
|
331
|
+
onClose(callback) {
|
|
332
|
+
// ignore
|
|
333
|
+
}
|
|
334
|
+
onMessage(callback) {
|
|
335
|
+
this._rawIpc.addEventListener('message', callback);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const wrap$f = global => {
|
|
339
|
+
return new IpcChildWithModuleWorker(global);
|
|
340
|
+
};
|
|
341
|
+
const waitForFirstMessage = async port => {
|
|
342
|
+
const {
|
|
343
|
+
resolve,
|
|
344
|
+
promise
|
|
345
|
+
} = Promise.withResolvers();
|
|
346
|
+
port.addEventListener('message', resolve, {
|
|
347
|
+
once: true
|
|
348
|
+
});
|
|
349
|
+
const event = await promise;
|
|
350
|
+
// @ts-ignore
|
|
351
|
+
return event.data;
|
|
352
|
+
};
|
|
353
|
+
const listen$6 = async () => {
|
|
354
|
+
const parentIpcRaw = listen$7();
|
|
355
|
+
signal$8(parentIpcRaw);
|
|
356
|
+
const parentIpc = wrap$f(parentIpcRaw);
|
|
357
|
+
const firstMessage = await waitForFirstMessage(parentIpc);
|
|
358
|
+
if (firstMessage.method !== 'initialize') {
|
|
359
|
+
throw new IpcError('unexpected first message');
|
|
360
|
+
}
|
|
361
|
+
const type = firstMessage.params[0];
|
|
362
|
+
if (type === 'message-port') {
|
|
363
|
+
parentIpc.send({
|
|
364
|
+
jsonrpc: '2.0',
|
|
365
|
+
id: firstMessage.id,
|
|
366
|
+
result: null
|
|
367
|
+
});
|
|
368
|
+
parentIpc.dispose();
|
|
369
|
+
const port = firstMessage.params[1];
|
|
370
|
+
return port;
|
|
371
|
+
}
|
|
372
|
+
return globalThis;
|
|
373
|
+
};
|
|
374
|
+
class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
|
|
375
|
+
getData(event) {
|
|
376
|
+
return getData$2(event);
|
|
377
|
+
}
|
|
378
|
+
send(message) {
|
|
379
|
+
this._rawIpc.postMessage(message);
|
|
380
|
+
}
|
|
381
|
+
sendAndTransfer(message) {
|
|
382
|
+
const transfer = getTransferrables(message);
|
|
383
|
+
this._rawIpc.postMessage(message, transfer);
|
|
384
|
+
}
|
|
385
|
+
dispose() {
|
|
386
|
+
if (this._rawIpc.close) {
|
|
387
|
+
this._rawIpc.close();
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
onClose(callback) {
|
|
391
|
+
// ignore
|
|
392
|
+
}
|
|
393
|
+
onMessage(callback) {
|
|
394
|
+
this._rawIpc.addEventListener('message', callback);
|
|
395
|
+
this._rawIpc.start();
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
const wrap$e = port => {
|
|
399
|
+
return new IpcChildWithModuleWorkerAndMessagePort(port);
|
|
400
|
+
};
|
|
401
|
+
const IpcChildWithModuleWorkerAndMessagePort$1 = {
|
|
402
|
+
__proto__: null,
|
|
403
|
+
listen: listen$6,
|
|
404
|
+
wrap: wrap$e
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
const Two = '2.0';
|
|
408
|
+
const create$4 = (method, params) => {
|
|
409
|
+
return {
|
|
410
|
+
jsonrpc: Two,
|
|
411
|
+
method,
|
|
412
|
+
params
|
|
413
|
+
};
|
|
414
|
+
};
|
|
415
|
+
const callbacks = Object.create(null);
|
|
416
|
+
const set$2 = (id, fn) => {
|
|
417
|
+
callbacks[id] = fn;
|
|
418
|
+
};
|
|
419
|
+
const get$2 = id => {
|
|
420
|
+
return callbacks[id];
|
|
421
|
+
};
|
|
422
|
+
const remove = id => {
|
|
423
|
+
delete callbacks[id];
|
|
424
|
+
};
|
|
425
|
+
let id = 0;
|
|
426
|
+
const create$3$1 = () => {
|
|
427
|
+
return ++id;
|
|
428
|
+
};
|
|
429
|
+
const registerPromise = () => {
|
|
430
|
+
const id = create$3$1();
|
|
431
|
+
const {
|
|
432
|
+
resolve,
|
|
433
|
+
promise
|
|
434
|
+
} = Promise.withResolvers();
|
|
435
|
+
set$2(id, resolve);
|
|
436
|
+
return {
|
|
437
|
+
id,
|
|
438
|
+
promise
|
|
439
|
+
};
|
|
440
|
+
};
|
|
441
|
+
const create$2$1 = (method, params) => {
|
|
442
|
+
const {
|
|
443
|
+
id,
|
|
444
|
+
promise
|
|
445
|
+
} = registerPromise();
|
|
446
|
+
const message = {
|
|
447
|
+
jsonrpc: Two,
|
|
448
|
+
method,
|
|
449
|
+
params,
|
|
450
|
+
id
|
|
451
|
+
};
|
|
452
|
+
return {
|
|
453
|
+
message,
|
|
454
|
+
promise
|
|
455
|
+
};
|
|
456
|
+
};
|
|
457
|
+
class JsonRpcError extends Error {
|
|
458
|
+
constructor(message) {
|
|
459
|
+
super(message);
|
|
460
|
+
this.name = 'JsonRpcError';
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const NewLine = '\n';
|
|
464
|
+
const DomException = 'DOMException';
|
|
465
|
+
const ReferenceError$1 = 'ReferenceError';
|
|
466
|
+
const SyntaxError$1 = 'SyntaxError';
|
|
467
|
+
const TypeError$1 = 'TypeError';
|
|
468
|
+
const getErrorConstructor = (message, type) => {
|
|
469
|
+
if (type) {
|
|
470
|
+
switch (type) {
|
|
471
|
+
case DomException:
|
|
472
|
+
return DOMException;
|
|
473
|
+
case TypeError$1:
|
|
474
|
+
return TypeError;
|
|
475
|
+
case SyntaxError$1:
|
|
476
|
+
return SyntaxError;
|
|
477
|
+
case ReferenceError$1:
|
|
478
|
+
return ReferenceError;
|
|
479
|
+
default:
|
|
480
|
+
return Error;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
if (message.startsWith('TypeError: ')) {
|
|
484
|
+
return TypeError;
|
|
485
|
+
}
|
|
486
|
+
if (message.startsWith('SyntaxError: ')) {
|
|
487
|
+
return SyntaxError;
|
|
488
|
+
}
|
|
489
|
+
if (message.startsWith('ReferenceError: ')) {
|
|
490
|
+
return ReferenceError;
|
|
491
|
+
}
|
|
492
|
+
return Error;
|
|
493
|
+
};
|
|
494
|
+
const constructError = (message, type, name) => {
|
|
495
|
+
const ErrorConstructor = getErrorConstructor(message, type);
|
|
496
|
+
if (ErrorConstructor === DOMException && name) {
|
|
497
|
+
return new ErrorConstructor(message, name);
|
|
498
|
+
}
|
|
499
|
+
if (ErrorConstructor === Error) {
|
|
500
|
+
const error = new Error(message);
|
|
501
|
+
if (name && name !== 'VError') {
|
|
502
|
+
error.name = name;
|
|
503
|
+
}
|
|
504
|
+
return error;
|
|
505
|
+
}
|
|
506
|
+
return new ErrorConstructor(message);
|
|
507
|
+
};
|
|
508
|
+
const getNewLineIndex = (string, startIndex = undefined) => {
|
|
509
|
+
return string.indexOf(NewLine, startIndex);
|
|
510
|
+
};
|
|
511
|
+
const getParentStack = error => {
|
|
512
|
+
let parentStack = error.stack || error.data || error.message || '';
|
|
513
|
+
if (parentStack.startsWith(' at')) {
|
|
514
|
+
parentStack = error.message + NewLine + parentStack;
|
|
515
|
+
}
|
|
516
|
+
return parentStack;
|
|
517
|
+
};
|
|
518
|
+
const joinLines = lines => {
|
|
519
|
+
return lines.join(NewLine);
|
|
520
|
+
};
|
|
521
|
+
const MethodNotFound = -32601;
|
|
522
|
+
const Custom = -32001;
|
|
523
|
+
const splitLines = lines => {
|
|
524
|
+
return lines.split(NewLine);
|
|
525
|
+
};
|
|
526
|
+
const restoreJsonRpcError = error => {
|
|
527
|
+
if (error && error instanceof Error) {
|
|
528
|
+
return error;
|
|
529
|
+
}
|
|
530
|
+
const currentStack = joinLines(splitLines(new Error().stack || '').slice(1));
|
|
531
|
+
if (error && error.code && error.code === MethodNotFound) {
|
|
532
|
+
const restoredError = new JsonRpcError(error.message);
|
|
533
|
+
const parentStack = getParentStack(error);
|
|
534
|
+
restoredError.stack = parentStack + NewLine + currentStack;
|
|
535
|
+
return restoredError;
|
|
536
|
+
}
|
|
537
|
+
if (error && error.message) {
|
|
538
|
+
const restoredError = constructError(error.message, error.type, error.name);
|
|
539
|
+
if (error.data) {
|
|
540
|
+
if (error.data.stack && error.data.type && error.message) {
|
|
541
|
+
restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
|
|
542
|
+
} else if (error.data.stack) {
|
|
543
|
+
restoredError.stack = error.data.stack;
|
|
544
|
+
}
|
|
545
|
+
if (error.data.codeFrame) {
|
|
546
|
+
// @ts-ignore
|
|
547
|
+
restoredError.codeFrame = error.data.codeFrame;
|
|
548
|
+
}
|
|
549
|
+
if (error.data.code) {
|
|
550
|
+
// @ts-ignore
|
|
551
|
+
restoredError.code = error.data.code;
|
|
552
|
+
}
|
|
553
|
+
if (error.data.type) {
|
|
554
|
+
// @ts-ignore
|
|
555
|
+
restoredError.name = error.data.type;
|
|
556
|
+
}
|
|
557
|
+
} else {
|
|
558
|
+
if (error.stack) {
|
|
559
|
+
const lowerStack = restoredError.stack || '';
|
|
560
|
+
// @ts-ignore
|
|
561
|
+
const indexNewLine = getNewLineIndex(lowerStack);
|
|
562
|
+
const parentStack = getParentStack(error);
|
|
563
|
+
// @ts-ignore
|
|
564
|
+
restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
|
|
565
|
+
}
|
|
566
|
+
if (error.codeFrame) {
|
|
567
|
+
// @ts-ignore
|
|
568
|
+
restoredError.codeFrame = error.codeFrame;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
return restoredError;
|
|
572
|
+
}
|
|
573
|
+
if (typeof error === 'string') {
|
|
574
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
575
|
+
}
|
|
576
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
577
|
+
};
|
|
578
|
+
const unwrapJsonRpcResult = responseMessage => {
|
|
579
|
+
if ('error' in responseMessage) {
|
|
580
|
+
const restoredError = restoreJsonRpcError(responseMessage.error);
|
|
581
|
+
throw restoredError;
|
|
582
|
+
}
|
|
583
|
+
if ('result' in responseMessage) {
|
|
584
|
+
return responseMessage.result;
|
|
585
|
+
}
|
|
586
|
+
throw new JsonRpcError('unexpected response message');
|
|
587
|
+
};
|
|
588
|
+
const warn = (...args) => {
|
|
589
|
+
console.warn(...args);
|
|
590
|
+
};
|
|
591
|
+
const resolve = (id, response) => {
|
|
592
|
+
const fn = get$2(id);
|
|
593
|
+
if (!fn) {
|
|
594
|
+
console.log(response);
|
|
595
|
+
warn(`callback ${id} may already be disposed`);
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
fn(response);
|
|
599
|
+
remove(id);
|
|
600
|
+
};
|
|
601
|
+
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
|
602
|
+
const getErrorType = prettyError => {
|
|
603
|
+
if (prettyError && prettyError.type) {
|
|
604
|
+
return prettyError.type;
|
|
605
|
+
}
|
|
606
|
+
if (prettyError && prettyError.constructor && prettyError.constructor.name) {
|
|
607
|
+
return prettyError.constructor.name;
|
|
608
|
+
}
|
|
609
|
+
return undefined;
|
|
610
|
+
};
|
|
611
|
+
const getErrorProperty = (error, prettyError) => {
|
|
612
|
+
if (error && error.code === E_COMMAND_NOT_FOUND) {
|
|
613
|
+
return {
|
|
614
|
+
code: MethodNotFound,
|
|
615
|
+
message: error.message,
|
|
616
|
+
data: error.stack
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
return {
|
|
620
|
+
code: Custom,
|
|
621
|
+
message: prettyError.message,
|
|
622
|
+
data: {
|
|
623
|
+
stack: prettyError.stack,
|
|
624
|
+
codeFrame: prettyError.codeFrame,
|
|
625
|
+
type: getErrorType(prettyError),
|
|
626
|
+
code: prettyError.code,
|
|
627
|
+
name: prettyError.name
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
};
|
|
631
|
+
const create$1$1 = (message, error) => {
|
|
632
|
+
return {
|
|
633
|
+
jsonrpc: Two,
|
|
634
|
+
id: message.id,
|
|
635
|
+
error
|
|
636
|
+
};
|
|
637
|
+
};
|
|
638
|
+
const getErrorResponse = (message, error, preparePrettyError, logError) => {
|
|
639
|
+
const prettyError = preparePrettyError(error);
|
|
640
|
+
logError(error, prettyError);
|
|
641
|
+
const errorProperty = getErrorProperty(error, prettyError);
|
|
642
|
+
return create$1$1(message, errorProperty);
|
|
643
|
+
};
|
|
644
|
+
const create$5 = (message, result) => {
|
|
645
|
+
return {
|
|
646
|
+
jsonrpc: Two,
|
|
647
|
+
id: message.id,
|
|
648
|
+
result: result ?? null
|
|
649
|
+
};
|
|
650
|
+
};
|
|
651
|
+
const getSuccessResponse = (message, result) => {
|
|
652
|
+
const resultProperty = result ?? null;
|
|
653
|
+
return create$5(message, resultProperty);
|
|
654
|
+
};
|
|
655
|
+
const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
|
|
656
|
+
try {
|
|
657
|
+
const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
|
|
658
|
+
return getSuccessResponse(message, result);
|
|
659
|
+
} catch (error) {
|
|
660
|
+
return getErrorResponse(message, error, preparePrettyError, logError);
|
|
661
|
+
}
|
|
662
|
+
};
|
|
663
|
+
const defaultPreparePrettyError = error => {
|
|
664
|
+
return error;
|
|
665
|
+
};
|
|
666
|
+
const defaultLogError = () => {
|
|
667
|
+
// ignore
|
|
668
|
+
};
|
|
669
|
+
const defaultRequiresSocket = () => {
|
|
670
|
+
return false;
|
|
671
|
+
};
|
|
672
|
+
const defaultResolve = resolve;
|
|
673
|
+
|
|
674
|
+
// TODO maybe remove this in v6 or v7, only accept options object to simplify the code
|
|
675
|
+
const normalizeParams = args => {
|
|
676
|
+
if (args.length === 1) {
|
|
677
|
+
const options = args[0];
|
|
678
|
+
return {
|
|
679
|
+
ipc: options.ipc,
|
|
680
|
+
message: options.message,
|
|
681
|
+
execute: options.execute,
|
|
682
|
+
resolve: options.resolve || defaultResolve,
|
|
683
|
+
preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
|
|
684
|
+
logError: options.logError || defaultLogError,
|
|
685
|
+
requiresSocket: options.requiresSocket || defaultRequiresSocket
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
return {
|
|
689
|
+
ipc: args[0],
|
|
690
|
+
message: args[1],
|
|
691
|
+
execute: args[2],
|
|
692
|
+
resolve: args[3],
|
|
693
|
+
preparePrettyError: args[4],
|
|
694
|
+
logError: args[5],
|
|
695
|
+
requiresSocket: args[6]
|
|
696
|
+
};
|
|
697
|
+
};
|
|
698
|
+
const handleJsonRpcMessage = async (...args) => {
|
|
699
|
+
const options = normalizeParams(args);
|
|
700
|
+
const {
|
|
701
|
+
message,
|
|
702
|
+
ipc,
|
|
703
|
+
execute,
|
|
704
|
+
resolve,
|
|
705
|
+
preparePrettyError,
|
|
706
|
+
logError,
|
|
707
|
+
requiresSocket
|
|
708
|
+
} = options;
|
|
709
|
+
if ('id' in message) {
|
|
710
|
+
if ('method' in message) {
|
|
711
|
+
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
712
|
+
try {
|
|
713
|
+
ipc.send(response);
|
|
714
|
+
} catch (error) {
|
|
715
|
+
const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
|
|
716
|
+
ipc.send(errorResponse);
|
|
717
|
+
}
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
resolve(message.id, message);
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
if ('method' in message) {
|
|
724
|
+
await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
throw new JsonRpcError('unexpected message');
|
|
728
|
+
};
|
|
729
|
+
const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
|
|
730
|
+
const {
|
|
731
|
+
message,
|
|
732
|
+
promise
|
|
733
|
+
} = create$2$1(method, params);
|
|
734
|
+
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
|
735
|
+
ipc.sendAndTransfer(message);
|
|
736
|
+
} else {
|
|
737
|
+
ipc.send(message);
|
|
738
|
+
}
|
|
739
|
+
const responseMessage = await promise;
|
|
740
|
+
return unwrapJsonRpcResult(responseMessage);
|
|
741
|
+
};
|
|
742
|
+
const send = (transport, method, ...params) => {
|
|
743
|
+
const message = create$4(method, params);
|
|
744
|
+
transport.send(message);
|
|
745
|
+
};
|
|
746
|
+
const invoke$3 = (ipc, method, ...params) => {
|
|
747
|
+
return invokeHelper(ipc, method, params, false);
|
|
748
|
+
};
|
|
749
|
+
const invokeAndTransfer = (ipc, method, ...params) => {
|
|
750
|
+
return invokeHelper(ipc, method, params, true);
|
|
751
|
+
};
|
|
752
|
+
|
|
753
|
+
const commands = Object.create(null);
|
|
754
|
+
const register = commandMap => {
|
|
755
|
+
Object.assign(commands, commandMap);
|
|
756
|
+
};
|
|
757
|
+
const getCommand = key => {
|
|
758
|
+
return commands[key];
|
|
759
|
+
};
|
|
760
|
+
const execute$1 = (command, ...args) => {
|
|
761
|
+
const fn = getCommand(command);
|
|
762
|
+
if (!fn) {
|
|
763
|
+
throw new Error(`command not found ${command}`);
|
|
764
|
+
}
|
|
765
|
+
return fn(...args);
|
|
766
|
+
};
|
|
767
|
+
|
|
768
|
+
const createRpc = ipc => {
|
|
769
|
+
const rpc = {
|
|
770
|
+
// @ts-ignore
|
|
771
|
+
ipc,
|
|
772
|
+
/**
|
|
773
|
+
* @deprecated
|
|
774
|
+
*/
|
|
775
|
+
send(method, ...params) {
|
|
776
|
+
send(ipc, method, ...params);
|
|
777
|
+
},
|
|
778
|
+
invoke(method, ...params) {
|
|
779
|
+
return invoke$3(ipc, method, ...params);
|
|
780
|
+
},
|
|
781
|
+
invokeAndTransfer(method, ...params) {
|
|
782
|
+
return invokeAndTransfer(ipc, method, ...params);
|
|
783
|
+
},
|
|
784
|
+
async dispose() {
|
|
785
|
+
await ipc?.dispose();
|
|
786
|
+
}
|
|
787
|
+
};
|
|
788
|
+
return rpc;
|
|
789
|
+
};
|
|
790
|
+
const requiresSocket = () => {
|
|
791
|
+
return false;
|
|
792
|
+
};
|
|
793
|
+
const preparePrettyError = error => {
|
|
794
|
+
return error;
|
|
795
|
+
};
|
|
796
|
+
const logError = () => {
|
|
797
|
+
// handled by renderer worker
|
|
798
|
+
};
|
|
799
|
+
const handleMessage = event => {
|
|
800
|
+
const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
|
|
801
|
+
const actualExecute = event?.target?.execute || execute$1;
|
|
802
|
+
return handleJsonRpcMessage(event.target, event.data, actualExecute, resolve, preparePrettyError, logError, actualRequiresSocket);
|
|
803
|
+
};
|
|
804
|
+
const handleIpc = ipc => {
|
|
805
|
+
if ('addEventListener' in ipc) {
|
|
806
|
+
ipc.addEventListener('message', handleMessage);
|
|
807
|
+
} else if ('on' in ipc) {
|
|
808
|
+
// deprecated
|
|
809
|
+
ipc.on('message', handleMessage);
|
|
810
|
+
}
|
|
811
|
+
};
|
|
812
|
+
const listen$1 = async (module, options) => {
|
|
813
|
+
const rawIpc = await module.listen(options);
|
|
814
|
+
if (module.signal) {
|
|
815
|
+
module.signal(rawIpc);
|
|
816
|
+
}
|
|
817
|
+
const ipc = module.wrap(rawIpc);
|
|
818
|
+
return ipc;
|
|
819
|
+
};
|
|
820
|
+
const create$3 = async ({
|
|
821
|
+
commandMap
|
|
822
|
+
}) => {
|
|
823
|
+
// TODO create a commandMap per rpc instance
|
|
824
|
+
register(commandMap);
|
|
825
|
+
const ipc = await listen$1(IpcChildWithModuleWorkerAndMessagePort$1);
|
|
826
|
+
handleIpc(ipc);
|
|
827
|
+
const rpc = createRpc(ipc);
|
|
828
|
+
return rpc;
|
|
829
|
+
};
|
|
830
|
+
const WebWorkerRpcClient = {
|
|
831
|
+
__proto__: null,
|
|
832
|
+
create: create$3
|
|
833
|
+
};
|
|
834
|
+
|
|
835
|
+
const create$2 = () => {
|
|
836
|
+
const states = Object.create(null);
|
|
837
|
+
return {
|
|
838
|
+
get(uid) {
|
|
839
|
+
return states[uid];
|
|
840
|
+
},
|
|
841
|
+
set(uid, oldState, newState) {
|
|
842
|
+
states[uid] = {
|
|
843
|
+
oldState,
|
|
844
|
+
newState
|
|
845
|
+
};
|
|
846
|
+
},
|
|
847
|
+
dispose(uid) {
|
|
848
|
+
delete states[uid];
|
|
849
|
+
},
|
|
850
|
+
getKeys() {
|
|
851
|
+
return Object.keys(states).map(key => {
|
|
852
|
+
return Number.parseInt(key);
|
|
853
|
+
});
|
|
854
|
+
},
|
|
855
|
+
clear() {
|
|
856
|
+
for (const key of Object.keys(states)) {
|
|
857
|
+
delete states[key];
|
|
858
|
+
}
|
|
859
|
+
},
|
|
860
|
+
wrapCommand(fn) {
|
|
861
|
+
const wrapped = async (uid, ...args) => {
|
|
862
|
+
const {
|
|
863
|
+
newState
|
|
864
|
+
} = states[uid];
|
|
865
|
+
const newerState = await fn(newState, ...args);
|
|
866
|
+
if (newState === newerState) {
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
const latest = states[uid];
|
|
870
|
+
states[uid] = {
|
|
871
|
+
oldState: latest.oldState,
|
|
872
|
+
newState: newerState
|
|
873
|
+
};
|
|
874
|
+
};
|
|
875
|
+
return wrapped;
|
|
876
|
+
},
|
|
877
|
+
diff(uid, modules, numbers) {
|
|
878
|
+
const {
|
|
879
|
+
oldState,
|
|
880
|
+
newState
|
|
881
|
+
} = states[uid];
|
|
882
|
+
const diffResult = [];
|
|
883
|
+
for (let i = 0; i < modules.length; i++) {
|
|
884
|
+
const fn = modules[i];
|
|
885
|
+
if (!fn(oldState, newState)) {
|
|
886
|
+
diffResult.push(numbers[i]);
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
return diffResult;
|
|
890
|
+
}
|
|
891
|
+
};
|
|
892
|
+
};
|
|
893
|
+
|
|
894
|
+
const {
|
|
895
|
+
get: get$1,
|
|
896
|
+
set: set$1,
|
|
897
|
+
wrapCommand
|
|
898
|
+
} = create$2();
|
|
899
|
+
|
|
900
|
+
const create$1 = (uid, x, y, width, height, parentUid) => {
|
|
901
|
+
const state = {
|
|
902
|
+
uid,
|
|
903
|
+
items: [],
|
|
904
|
+
itemHeight: 20,
|
|
905
|
+
maxHeight: 150,
|
|
906
|
+
minLineY: 0,
|
|
907
|
+
maxLineY: 0,
|
|
908
|
+
focusedIndex: -1,
|
|
909
|
+
x: 0,
|
|
910
|
+
y: 0,
|
|
911
|
+
width: 0,
|
|
912
|
+
height: 0,
|
|
913
|
+
deltaY: 0,
|
|
914
|
+
finalDeltaY: 0,
|
|
915
|
+
focused: false,
|
|
916
|
+
headerHeight: 0,
|
|
917
|
+
leadingWord: '',
|
|
918
|
+
unfilteredItems: [],
|
|
919
|
+
version: 0
|
|
920
|
+
};
|
|
921
|
+
set$1(uid, state, state);
|
|
922
|
+
};
|
|
923
|
+
|
|
924
|
+
const modules = [];
|
|
925
|
+
const numbers = [];
|
|
926
|
+
|
|
927
|
+
const diff = (oldState, newState) => {
|
|
928
|
+
const diffResult = [];
|
|
929
|
+
for (let i = 0; i < modules.length; i++) {
|
|
930
|
+
const fn = modules[i];
|
|
931
|
+
if (!fn(oldState, newState)) {
|
|
932
|
+
diffResult.push(numbers[i]);
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
return diffResult;
|
|
936
|
+
};
|
|
937
|
+
|
|
938
|
+
const diff2 = uid => {
|
|
939
|
+
const {
|
|
940
|
+
oldState,
|
|
941
|
+
newState
|
|
942
|
+
} = get$1(uid);
|
|
943
|
+
const diffResult = diff(oldState, newState);
|
|
944
|
+
return diffResult;
|
|
945
|
+
};
|
|
946
|
+
|
|
947
|
+
const commandIds = ['handleSliderPointerDown', 'handleSliderPointerMove', 'initialize'];
|
|
948
|
+
|
|
949
|
+
const getCommandIds = () => {
|
|
950
|
+
return commandIds;
|
|
951
|
+
};
|
|
952
|
+
|
|
953
|
+
const initialize = async () => {
|
|
954
|
+
// TODO create connection to extension host worker
|
|
955
|
+
};
|
|
956
|
+
|
|
957
|
+
const OnCompletion = 'onCompletion';
|
|
958
|
+
|
|
959
|
+
const CompletionExecute = 'ExtensionHostCompletion.execute';
|
|
960
|
+
|
|
961
|
+
const rpcs = Object.create(null);
|
|
962
|
+
const set$9 = (id, rpc) => {
|
|
963
|
+
rpcs[id] = rpc;
|
|
964
|
+
};
|
|
965
|
+
const get = id => {
|
|
966
|
+
return rpcs[id];
|
|
967
|
+
};
|
|
968
|
+
|
|
969
|
+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
970
|
+
|
|
971
|
+
const create = rpcId => {
|
|
972
|
+
return {
|
|
973
|
+
// @ts-ignore
|
|
974
|
+
invoke(method, ...params) {
|
|
975
|
+
const rpc = get(rpcId);
|
|
976
|
+
// @ts-ignore
|
|
977
|
+
return rpc.invoke(method, ...params);
|
|
978
|
+
},
|
|
979
|
+
// @ts-ignore
|
|
980
|
+
invokeAndTransfer(method, ...params) {
|
|
981
|
+
const rpc = get(rpcId);
|
|
982
|
+
// @ts-ignore
|
|
983
|
+
return rpc.invokeAndTransfer(method, ...params);
|
|
984
|
+
},
|
|
985
|
+
set(rpc) {
|
|
986
|
+
set$9(rpcId, rpc);
|
|
987
|
+
}
|
|
988
|
+
};
|
|
989
|
+
};
|
|
990
|
+
const EditorWorker$1 = 99;
|
|
991
|
+
const ExtensionHostWorker = 44;
|
|
992
|
+
const RendererWorker$1 = 1;
|
|
993
|
+
const {
|
|
994
|
+
invoke: invoke$8} = create(EditorWorker$1);
|
|
995
|
+
const EditorWorker = {
|
|
996
|
+
__proto__: null,
|
|
997
|
+
invoke: invoke$8};
|
|
998
|
+
const {
|
|
999
|
+
invoke: invoke$6} = create(ExtensionHostWorker);
|
|
1000
|
+
const ExtensionHost = {
|
|
1001
|
+
__proto__: null,
|
|
1002
|
+
invoke: invoke$6};
|
|
1003
|
+
const {
|
|
1004
|
+
invoke: invoke$4,
|
|
1005
|
+
set: set$4
|
|
1006
|
+
} = create(RendererWorker$1);
|
|
1007
|
+
const RendererWorker = {
|
|
1008
|
+
__proto__: null,
|
|
1009
|
+
invoke: invoke$4,
|
|
1010
|
+
set: set$4
|
|
1011
|
+
};
|
|
1012
|
+
|
|
1013
|
+
const {
|
|
1014
|
+
set,
|
|
1015
|
+
invoke: invoke$2} = RendererWorker;
|
|
1016
|
+
|
|
1017
|
+
// TODO add tests for this
|
|
1018
|
+
const activateByEvent = async event => {
|
|
1019
|
+
await invoke$2('ExtensionHostManagement.activateByEvent', event);
|
|
1020
|
+
};
|
|
1021
|
+
|
|
1022
|
+
const {
|
|
1023
|
+
invoke: invoke$1} = ExtensionHost;
|
|
1024
|
+
|
|
1025
|
+
const execute = async ({
|
|
1026
|
+
editor,
|
|
1027
|
+
args,
|
|
1028
|
+
event,
|
|
1029
|
+
method,
|
|
1030
|
+
noProviderFoundMessage,
|
|
1031
|
+
noProviderFoundResult = undefined
|
|
1032
|
+
}) => {
|
|
1033
|
+
const fullEvent = `${event}:${editor.languageId}`;
|
|
1034
|
+
await activateByEvent(fullEvent);
|
|
1035
|
+
const result = await invoke$1(method, editor.uid, ...args);
|
|
1036
|
+
return result;
|
|
1037
|
+
};
|
|
1038
|
+
|
|
1039
|
+
const executeCompletionProvider = async (editor, offset) => {
|
|
1040
|
+
return execute({
|
|
1041
|
+
editor,
|
|
1042
|
+
event: OnCompletion,
|
|
1043
|
+
method: CompletionExecute,
|
|
1044
|
+
args: [offset],
|
|
1045
|
+
noProviderFoundMessage: 'no completion provider found',
|
|
1046
|
+
noProviderFoundResult: []});
|
|
1047
|
+
};
|
|
1048
|
+
|
|
1049
|
+
const getOffsetAtCursor = editor => {
|
|
1050
|
+
// TODO
|
|
1051
|
+
return 0;
|
|
1052
|
+
};
|
|
1053
|
+
|
|
1054
|
+
// TODO possible to do this with events/state machine instead of promises -> enables canceling operations / concurrent calls
|
|
1055
|
+
const getCompletions = async editor => {
|
|
1056
|
+
const offset = getOffsetAtCursor();
|
|
1057
|
+
const completions = await executeCompletionProvider(editor, offset);
|
|
1058
|
+
return completions;
|
|
1059
|
+
};
|
|
1060
|
+
|
|
1061
|
+
const Diagonal = 1;
|
|
1062
|
+
const Left = 2;
|
|
1063
|
+
|
|
1064
|
+
// based on https://github.com/microsoft/vscode/blob/3059063b805ed0ac10a6d9539e213386bfcfb852/src/vs/base/common/filters.ts by Microsoft (License MIT)
|
|
1065
|
+
|
|
1066
|
+
const createTable = size => {
|
|
1067
|
+
const table = [];
|
|
1068
|
+
for (let i = 0; i < size; i++) {
|
|
1069
|
+
const row = new Uint8Array(size);
|
|
1070
|
+
table.push(row);
|
|
1071
|
+
}
|
|
1072
|
+
return table;
|
|
1073
|
+
};
|
|
1074
|
+
const EmptyMatches$1 = [];
|
|
1075
|
+
const Dash = '-';
|
|
1076
|
+
const Dot = '.';
|
|
1077
|
+
const EmptyString$1 = '';
|
|
1078
|
+
const Space = ' ';
|
|
1079
|
+
const Underline = '_';
|
|
1080
|
+
const T = 't';
|
|
1081
|
+
const isLowerCase = char => {
|
|
1082
|
+
return char === char.toLowerCase();
|
|
1083
|
+
};
|
|
1084
|
+
const isUpperCase = char => {
|
|
1085
|
+
return char === char.toUpperCase();
|
|
1086
|
+
};
|
|
1087
|
+
|
|
1088
|
+
// based on https://github.com/microsoft/vscode/blob/3059063b805ed0ac10a6d9539e213386bfcfb852/src/vs/base/common/filters.ts by Microsoft (License MIT)
|
|
1089
|
+
const isGap = (columnCharBefore, columnChar) => {
|
|
1090
|
+
switch (columnCharBefore) {
|
|
1091
|
+
case Dash:
|
|
1092
|
+
case Underline:
|
|
1093
|
+
case EmptyString$1:
|
|
1094
|
+
case T:
|
|
1095
|
+
case Space:
|
|
1096
|
+
case Dot:
|
|
1097
|
+
return true;
|
|
1098
|
+
}
|
|
1099
|
+
if (isLowerCase(columnCharBefore) && isUpperCase(columnChar)) {
|
|
1100
|
+
return true;
|
|
1101
|
+
}
|
|
1102
|
+
return false;
|
|
1103
|
+
};
|
|
1104
|
+
|
|
1105
|
+
// based on https://github.com/microsoft/vscode/blob/3059063b805ed0ac10a6d9539e213386bfcfb852/src/vs/base/common/filters.ts by Microsoft (License MIT)
|
|
1106
|
+
const getScore = (rowCharLow, rowChar, columnCharBefore, columnCharLow, columnChar, isDiagonalMatch) => {
|
|
1107
|
+
if (rowCharLow !== columnCharLow) {
|
|
1108
|
+
return -1;
|
|
1109
|
+
}
|
|
1110
|
+
const isMatch = rowChar === columnChar;
|
|
1111
|
+
if (isMatch) {
|
|
1112
|
+
if (isDiagonalMatch) {
|
|
1113
|
+
return 8;
|
|
1114
|
+
}
|
|
1115
|
+
if (isGap(columnCharBefore, columnChar)) {
|
|
1116
|
+
return 8;
|
|
1117
|
+
}
|
|
1118
|
+
return 5;
|
|
1119
|
+
}
|
|
1120
|
+
if (isGap(columnCharBefore, columnChar)) {
|
|
1121
|
+
return 8;
|
|
1122
|
+
}
|
|
1123
|
+
return 5;
|
|
1124
|
+
};
|
|
1125
|
+
|
|
1126
|
+
// based on https://github.com/microsoft/vscode/blob/3059063b805ed0ac10a6d9539e213386bfcfb852/src/vs/base/common/filters.ts by Microsoft (License MIT)
|
|
1127
|
+
|
|
1128
|
+
const isPatternInWord = (patternLow, patternPos, patternLen, wordLow, wordPos, wordLen) => {
|
|
1129
|
+
while (patternPos < patternLen && wordPos < wordLen) {
|
|
1130
|
+
if (patternLow[patternPos] === wordLow[wordPos]) {
|
|
1131
|
+
patternPos += 1;
|
|
1132
|
+
}
|
|
1133
|
+
wordPos += 1;
|
|
1134
|
+
}
|
|
1135
|
+
return patternPos === patternLen; // pattern must be exhausted
|
|
1136
|
+
};
|
|
1137
|
+
|
|
1138
|
+
// based on https://github.com/microsoft/vscode/blob/3059063b805ed0ac10a6d9539e213386bfcfb852/src/vs/base/common/filters.ts by Microsoft (License MIT)
|
|
1139
|
+
const traceHighlights = (table, arrows, patternLength, wordLength) => {
|
|
1140
|
+
let row = patternLength;
|
|
1141
|
+
let column = wordLength;
|
|
1142
|
+
const matches = [];
|
|
1143
|
+
while (row >= 1 && column >= 1) {
|
|
1144
|
+
const arrow = arrows[row][column];
|
|
1145
|
+
if (arrow === Left) {
|
|
1146
|
+
column--;
|
|
1147
|
+
} else if (arrow === Diagonal) {
|
|
1148
|
+
row--;
|
|
1149
|
+
column--;
|
|
1150
|
+
const start = column + 1;
|
|
1151
|
+
while (row >= 1 && column >= 1) {
|
|
1152
|
+
const arrow = arrows[row][column];
|
|
1153
|
+
if (arrow === Left) {
|
|
1154
|
+
break;
|
|
1155
|
+
}
|
|
1156
|
+
if (arrow === Diagonal) {
|
|
1157
|
+
row--;
|
|
1158
|
+
column--;
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
const end = column;
|
|
1162
|
+
matches.unshift(end, start);
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
matches.unshift(table[patternLength][wordLength - 1]);
|
|
1166
|
+
return matches;
|
|
1167
|
+
};
|
|
1168
|
+
|
|
1169
|
+
// based on https://github.com/microsoft/vscode/blob/3059063b805ed0ac10a6d9539e213386bfcfb852/src/vs/base/common/filters.ts by Microsoft (License MIT)
|
|
1170
|
+
const gridSize = 128;
|
|
1171
|
+
const table = createTable(gridSize);
|
|
1172
|
+
const arrows = createTable(gridSize);
|
|
1173
|
+
const fuzzySearch = (pattern, word) => {
|
|
1174
|
+
const patternLength = Math.min(pattern.length, gridSize - 1);
|
|
1175
|
+
const wordLength = Math.min(word.length, gridSize - 1);
|
|
1176
|
+
const patternLower = pattern.toLowerCase();
|
|
1177
|
+
const wordLower = word.toLowerCase();
|
|
1178
|
+
if (!isPatternInWord(patternLower, 0, patternLength, wordLower, 0, wordLength)) {
|
|
1179
|
+
return EmptyMatches$1;
|
|
1180
|
+
}
|
|
1181
|
+
let strongMatch = false;
|
|
1182
|
+
for (let row = 1; row < patternLength + 1; row++) {
|
|
1183
|
+
const rowChar = pattern[row - 1];
|
|
1184
|
+
const rowCharLow = patternLower[row - 1];
|
|
1185
|
+
for (let column = 1; column < wordLength + 1; column++) {
|
|
1186
|
+
const columnChar = word[column - 1];
|
|
1187
|
+
const columnCharLow = wordLower[column - 1];
|
|
1188
|
+
const columnCharBefore = word[column - 2] || '';
|
|
1189
|
+
const isDiagonalMatch = arrows[row - 1][column - 1] === Diagonal;
|
|
1190
|
+
const score = getScore(rowCharLow, rowChar, columnCharBefore, columnCharLow, columnChar, isDiagonalMatch);
|
|
1191
|
+
if (row === 1 && score > 5) {
|
|
1192
|
+
strongMatch = true;
|
|
1193
|
+
}
|
|
1194
|
+
let diagonalScore = score + table[row - 1][column - 1];
|
|
1195
|
+
if (isDiagonalMatch && score !== -1) {
|
|
1196
|
+
diagonalScore += 2;
|
|
1197
|
+
}
|
|
1198
|
+
const leftScore = table[row][column - 1];
|
|
1199
|
+
if (leftScore > diagonalScore) {
|
|
1200
|
+
table[row][column] = leftScore;
|
|
1201
|
+
arrows[row][column] = Left;
|
|
1202
|
+
} else {
|
|
1203
|
+
table[row][column] = diagonalScore;
|
|
1204
|
+
arrows[row][column] = Diagonal;
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
if (!strongMatch) {
|
|
1209
|
+
return EmptyMatches$1;
|
|
1210
|
+
}
|
|
1211
|
+
const highlights = traceHighlights(table, arrows, patternLength, wordLength);
|
|
1212
|
+
return highlights;
|
|
1213
|
+
};
|
|
1214
|
+
|
|
1215
|
+
const EmptyString = '';
|
|
1216
|
+
|
|
1217
|
+
const Deprecated = 1 << 0;
|
|
1218
|
+
|
|
1219
|
+
const EmptyMatches = [];
|
|
1220
|
+
|
|
1221
|
+
const addEmptyMatch = item => {
|
|
1222
|
+
return {
|
|
1223
|
+
...item,
|
|
1224
|
+
matches: EmptyMatches
|
|
1225
|
+
};
|
|
1226
|
+
};
|
|
1227
|
+
const filterCompletionItems = (completionItems, word) => {
|
|
1228
|
+
if (word === EmptyString) {
|
|
1229
|
+
return completionItems.map(addEmptyMatch);
|
|
1230
|
+
}
|
|
1231
|
+
const filteredCompletions = [];
|
|
1232
|
+
const deprecated = [];
|
|
1233
|
+
for (const completionItem of completionItems) {
|
|
1234
|
+
const {
|
|
1235
|
+
label,
|
|
1236
|
+
flags
|
|
1237
|
+
} = completionItem;
|
|
1238
|
+
const result = fuzzySearch(word, label);
|
|
1239
|
+
if (result.length > 0) {
|
|
1240
|
+
if (flags & Deprecated) {
|
|
1241
|
+
// TODO avoid mutation
|
|
1242
|
+
completionItem.matches = EmptyMatches;
|
|
1243
|
+
deprecated.push(completionItem);
|
|
1244
|
+
} else {
|
|
1245
|
+
// TODO avoid mutation
|
|
1246
|
+
completionItem.matches = result;
|
|
1247
|
+
filteredCompletions.push(completionItem);
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
if (deprecated.length > 0) {
|
|
1252
|
+
filteredCompletions.push(...deprecated);
|
|
1253
|
+
}
|
|
1254
|
+
return filteredCompletions;
|
|
1255
|
+
};
|
|
1256
|
+
|
|
1257
|
+
const getFinalDeltaY = (height, itemHeight, itemsLength) => {
|
|
1258
|
+
const contentHeight = itemsLength * itemHeight;
|
|
1259
|
+
const finalDeltaY = Math.max(contentHeight - height, 0);
|
|
1260
|
+
return finalDeltaY;
|
|
1261
|
+
};
|
|
1262
|
+
|
|
1263
|
+
const getListHeight = (itemsLength, itemHeight, maxHeight) => {
|
|
1264
|
+
number(itemsLength);
|
|
1265
|
+
number(itemHeight);
|
|
1266
|
+
number(maxHeight);
|
|
1267
|
+
if (itemsLength === 0) {
|
|
1268
|
+
return itemHeight;
|
|
1269
|
+
}
|
|
1270
|
+
const totalHeight = itemsLength * itemHeight;
|
|
1271
|
+
return Math.min(totalHeight, maxHeight);
|
|
1272
|
+
};
|
|
1273
|
+
|
|
1274
|
+
const {
|
|
1275
|
+
invoke} = EditorWorker;
|
|
1276
|
+
|
|
1277
|
+
const getPositionAtCursor = async parentUid => {
|
|
1278
|
+
const position = await invoke('Editor.getPositionAtCursor', parentUid);
|
|
1279
|
+
return position;
|
|
1280
|
+
};
|
|
1281
|
+
|
|
1282
|
+
const RE_WORD = /[\w\-]+$/;
|
|
1283
|
+
const getWordAtOffset = editor => {
|
|
1284
|
+
const {
|
|
1285
|
+
lines,
|
|
1286
|
+
selections
|
|
1287
|
+
} = editor;
|
|
1288
|
+
const rowIndex = selections[0];
|
|
1289
|
+
const columnIndex = selections[1];
|
|
1290
|
+
const line = lines[rowIndex];
|
|
1291
|
+
const part = line.slice(0, columnIndex);
|
|
1292
|
+
const wordMatch = part.match(RE_WORD);
|
|
1293
|
+
if (wordMatch) {
|
|
1294
|
+
return wordMatch[0];
|
|
1295
|
+
}
|
|
1296
|
+
return '';
|
|
1297
|
+
};
|
|
1298
|
+
|
|
1299
|
+
const loadContent = async state => {
|
|
1300
|
+
const editor = {}; // TODO
|
|
1301
|
+
const {
|
|
1302
|
+
itemHeight,
|
|
1303
|
+
maxHeight
|
|
1304
|
+
} = state;
|
|
1305
|
+
const unfilteredItems = await getCompletions(editor);
|
|
1306
|
+
const wordAtOffset = getWordAtOffset(editor);
|
|
1307
|
+
const items = filterCompletionItems(unfilteredItems, wordAtOffset);
|
|
1308
|
+
const {
|
|
1309
|
+
rowIndex,
|
|
1310
|
+
columnIndex,
|
|
1311
|
+
x,
|
|
1312
|
+
y
|
|
1313
|
+
} = await getPositionAtCursor(editor);
|
|
1314
|
+
const newMaxLineY = Math.min(items.length, 8);
|
|
1315
|
+
editor.widgets = editor.widgets || [];
|
|
1316
|
+
// editor.widgets.push(ViewletModuleId.EditorCompletion)
|
|
1317
|
+
const itemsLength = items.length;
|
|
1318
|
+
const newFocusedIndex = itemsLength === 0 ? -1 : 0;
|
|
1319
|
+
const total = items.length;
|
|
1320
|
+
const height = getListHeight(items.length, itemHeight, maxHeight);
|
|
1321
|
+
const finalDeltaY = getFinalDeltaY(height, itemHeight, total);
|
|
1322
|
+
return {
|
|
1323
|
+
...state,
|
|
1324
|
+
unfilteredItems,
|
|
1325
|
+
items,
|
|
1326
|
+
x,
|
|
1327
|
+
y,
|
|
1328
|
+
maxLineY: newMaxLineY,
|
|
1329
|
+
focusedIndex: newFocusedIndex,
|
|
1330
|
+
finalDeltaY,
|
|
1331
|
+
leadingWord: wordAtOffset,
|
|
1332
|
+
height,
|
|
1333
|
+
// @ts-ignore
|
|
1334
|
+
rowIndex,
|
|
1335
|
+
columnIndex,
|
|
1336
|
+
// editorUid,
|
|
1337
|
+
width: 200
|
|
1338
|
+
};
|
|
1339
|
+
};
|
|
1340
|
+
|
|
1341
|
+
const RenderContent = 13;
|
|
1342
|
+
|
|
1343
|
+
const SetDom2 = 'Viewlet.setDom2';
|
|
1344
|
+
|
|
1345
|
+
const renderContent = (oldState, newState) => {
|
|
1346
|
+
const {
|
|
1347
|
+
uid
|
|
1348
|
+
} = newState;
|
|
1349
|
+
const dom = [];
|
|
1350
|
+
return [SetDom2, uid, dom];
|
|
1351
|
+
};
|
|
1352
|
+
|
|
1353
|
+
const getRenderer = diffType => {
|
|
1354
|
+
switch (diffType) {
|
|
1355
|
+
case RenderContent:
|
|
1356
|
+
return renderContent;
|
|
1357
|
+
default:
|
|
1358
|
+
throw new Error('unknown renderer');
|
|
1359
|
+
}
|
|
1360
|
+
};
|
|
1361
|
+
|
|
1362
|
+
const applyRender = (oldState, newState, diffResult) => {
|
|
1363
|
+
const commands = [];
|
|
1364
|
+
for (const item of diffResult) {
|
|
1365
|
+
const fn = getRenderer(item);
|
|
1366
|
+
commands.push(fn(oldState, newState));
|
|
1367
|
+
}
|
|
1368
|
+
return commands;
|
|
1369
|
+
};
|
|
1370
|
+
|
|
1371
|
+
const render2 = (uid, diffResult) => {
|
|
1372
|
+
const {
|
|
1373
|
+
oldState,
|
|
1374
|
+
newState
|
|
1375
|
+
} = get$1(uid);
|
|
1376
|
+
set$1(uid, newState, newState);
|
|
1377
|
+
const commands = applyRender(oldState, newState, diffResult);
|
|
1378
|
+
return commands;
|
|
1379
|
+
};
|
|
1380
|
+
|
|
1381
|
+
const terminate = () => {
|
|
1382
|
+
globalThis.close();
|
|
1383
|
+
};
|
|
1384
|
+
|
|
1385
|
+
const commandMap = {
|
|
1386
|
+
'Completions.create': create$1,
|
|
1387
|
+
'Completions.diff2': diff2,
|
|
1388
|
+
'Completions.getCommandIds': getCommandIds,
|
|
1389
|
+
'Completions.loadContent': wrapCommand(loadContent),
|
|
1390
|
+
'Completions.render2': render2,
|
|
1391
|
+
'Completions.terminate': terminate,
|
|
1392
|
+
'Completions.intialize': initialize
|
|
1393
|
+
};
|
|
1394
|
+
|
|
1395
|
+
const listen = async () => {
|
|
1396
|
+
const rpc = await WebWorkerRpcClient.create({
|
|
1397
|
+
commandMap: commandMap
|
|
1398
|
+
});
|
|
1399
|
+
set(rpc);
|
|
1400
|
+
};
|
|
1401
|
+
|
|
1402
|
+
const main = async () => {
|
|
1403
|
+
await listen();
|
|
1404
|
+
};
|
|
1405
|
+
|
|
1406
|
+
main();
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lvce-editor/completion-worker",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Web Worker for the find widget in Lvce Editor",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git@github.com:lvce-editor/completion-worker.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Lvce Editor",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "dist/completionWorkerMain.js"
|
|
13
|
+
}
|