-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
vsctm.ts
84 lines (72 loc) · 1.92 KB
/
vsctm.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
import { promises as fs } from "node:fs";
import path from "node:path";
import _ from "lodash";
import * as vscodeOniguruma from "vscode-oniguruma";
import { readFile } from "./util";
export class VscodeTextmate {
oniguruma: any;
registry: any;
vsctm: any;
initCalled: any;
constructor(vsctm: any, oniguruma: any) {
// @ts-ignore
// biome-ignore lint/correctness/noConstructorReturn: <explanation>
return (async () => {
this.vsctm = vsctm.default ?? vsctm;
// @ts-ignore
this.oniguruma = oniguruma || vscodeOniguruma?.default || vscodeOniguruma;
await this.loadWasm();
return this;
})();
}
async loadWasm() {
const wasm = await fs.readFile(
// @ts-ignore
// eslint-disable-next-line
require.resolve("vscode-oniguruma/release/onig.wasm"),
);
await this.oniguruma?.loadWASM(wasm.buffer);
if (!this.initCalled) {
try {
this.oniguruma.loadWASM(wasm.buffer);
} catch (error) {
this.initCalled = true;
}
this.initCalled = true;
}
}
createRegistry() {
this.registry = new this.vsctm.Registry({
loadGrammar: (scopeName: any) => {
if (scopeName === "text.html.php.blade") {
// https://github.com/onecentlin/
// laravel-blade-snippets-vscode/
// blob/master/syntaxes/blade.tmLanguage.json
return readFile(
path.resolve(__dirname, "../syntaxes/blade.tmLanguage.json"),
).then((content: any) =>
this.vsctm.parseRawGrammar(
content.toString(),
"./blade.tmLanguage.json",
),
);
}
return null;
},
onigLib: Promise.resolve({
createOnigScanner: (sources: any) =>
new this.oniguruma.OnigScanner(sources),
createOnigString: (str: any) => new this.oniguruma.OnigString(str),
}),
});
return this.registry;
}
tokenizeLines(splitedLines: any, grammar: any) {
return _.map(splitedLines, (line: any) =>
grammar.tokenizeLine(line, this.vsctm?.INITIAL),
);
}
}
export default {
VscodeTextmate,
};