|
| 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