-
Notifications
You must be signed in to change notification settings - Fork 26
/
env.ts
77 lines (67 loc) · 2.09 KB
/
env.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
import editorWorker from "monaco-editor-core/esm/vs/editor/editor.worker?worker";
import vueWorker from "../src/vue.worker?worker";
import * as onigasm from "onigasm";
import onigasmWasm from "onigasm/lib/onigasm.wasm?url";
import type { LanguageService } from "@vue/language-service";
import { editor, languages } from "monaco-editor-core";
import * as volar from "@volar/monaco";
export function loadOnigasm() {
return onigasm.loadWASM(onigasmWasm);
}
export function setupMonacoEnv(takeoverMode = false) {
let initialized = false;
languages.register({ id: "vue", extensions: [".vue"] });
languages.onLanguage("vue", setup);
if (takeoverMode) {
languages.onLanguage("javascript", setup);
languages.onLanguage("typescript", setup);
languages.onLanguage("javascriptreact", setup);
languages.onLanguage("typescriptreact", setup);
languages.onLanguage("json", setup);
}
async function setup() {
if (initialized) {
return;
}
initialized = true;
(self as any).MonacoEnvironment ??= {};
(self as any).MonacoEnvironment.getWorker ??= () => new editorWorker();
const getWorker = (self as any).MonacoEnvironment.getWorker;
(self as any).MonacoEnvironment.getWorker = (_: any, label: string) => {
if (label === "vue") {
return new vueWorker();
}
return getWorker();
};
const worker = editor.createWebWorker<LanguageService>({
moduleId: "vs/language/vue/vueWorker",
label: "vue",
createData: {},
});
const languageId = takeoverMode
? [
"vue",
"javascript",
"typescript",
"javascriptreact",
"typescriptreact",
"json",
]
: ["vue"];
const getSyncUris = () => editor.getModels().map((model) => model.uri);
volar.editor.activateMarkers(
worker,
languageId,
"vue",
getSyncUris,
editor
);
volar.editor.activateAutoInsertion(worker, languageId, getSyncUris, editor);
await volar.languages.registerProvides(
worker,
languageId,
getSyncUris,
languages
);
}
}