Skip to content

Commit 73f630d

Browse files
authored
fix: bundle grammars from shiki (Kingwl#29)
1 parent 99f6ee9 commit 73f630d

14 files changed

+98
-32739
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"types": "./dist/vue.worker.d.ts"
1515
}
1616
},
17-
"packageManager": "pnpm@7.2.1",
17+
"packageManager": "pnpm@8.11.0",
1818
"repository": {
1919
"type": "git",
2020
"url": "git+https://github.com/Kingwl/monaco-volar.git"
@@ -42,6 +42,7 @@
4242
"monaco-textmate": "^3.0.1",
4343
"path-browserify": "^1.0.1",
4444
"prettier": "^2.8.8",
45+
"shiki": "^0.14.6",
4546
"typescript": "^5.1.3",
4647
"vite": "^4.3.9",
4748
"volar-service-typescript": "0.0.8",

pnpm-lock.yaml

Lines changed: 28 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/grammars.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { wireTmGrammars } from "monaco-editor-textmate";
2+
import { Registry, type IGrammarDefinition } from "monaco-textmate";
3+
4+
async function dispatchGrammars(
5+
scopeName: string
6+
): Promise<IGrammarDefinition> {
7+
switch (scopeName) {
8+
case "source.vue":
9+
return {
10+
format: "json",
11+
content: await import("shiki/languages/vue.tmLanguage.json"),
12+
};
13+
case "source.ts":
14+
return {
15+
format: "json",
16+
content: await import("shiki/languages/typescript.tmLanguage.json"),
17+
};
18+
case "source.js":
19+
return {
20+
format: "json",
21+
content: await import("shiki/languages/javascript.tmLanguage.json"),
22+
};
23+
case "text.html.basic":
24+
return {
25+
format: "json",
26+
content: await import("shiki/languages/html.tmLanguage.json"),
27+
};
28+
case "source.css":
29+
return {
30+
format: "json",
31+
content: await import("shiki/languages/css.tmLanguage.json"),
32+
};
33+
default:
34+
return {
35+
format: "json",
36+
content: {
37+
scopeName: "source",
38+
patterns: [],
39+
},
40+
};
41+
}
42+
}
43+
44+
export async function loadGrammars(
45+
monaco: typeof import("monaco-editor-core"),
46+
editor: import("monaco-editor-core").editor.IStandaloneCodeEditor
47+
) {
48+
const registry = new Registry({
49+
getGrammarDefinition: async (scopeName) => {
50+
const dispatch = await dispatchGrammars(scopeName);
51+
return JSON.parse(JSON.stringify(dispatch));
52+
},
53+
});
54+
const grammars = new Map();
55+
grammars.set("vue", "source.vue");
56+
grammars.set("javascript", "source.js");
57+
grammars.set("typescript", "source.ts");
58+
grammars.set("css", "source.css");
59+
grammars.set("html", "text.html.basic");
60+
61+
for (const lang of grammars.keys()) {
62+
monaco.languages.register({
63+
id: lang,
64+
});
65+
}
66+
67+
await wireTmGrammars(monaco as any, registry, grammars, editor as any);
68+
}

0 commit comments

Comments
 (0)