Skip to content

Commit

Permalink
fix: bundle grammars from shiki (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Dec 16, 2023
1 parent 99f6ee9 commit 73f630d
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 32,739 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"types": "./dist/vue.worker.d.ts"
}
},
"packageManager": "pnpm@7.2.1",
"packageManager": "pnpm@8.11.0",
"repository": {
"type": "git",
"url": "git+https://github.com/Kingwl/monaco-volar.git"
Expand Down Expand Up @@ -42,6 +42,7 @@
"monaco-textmate": "^3.0.1",
"path-browserify": "^1.0.1",
"prettier": "^2.8.8",
"shiki": "^0.14.6",
"typescript": "^5.1.3",
"vite": "^4.3.9",
"volar-service-typescript": "0.0.8",
Expand Down
32 changes: 28 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions src/grammars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { wireTmGrammars } from "monaco-editor-textmate";
import { Registry, type IGrammarDefinition } from "monaco-textmate";

async function dispatchGrammars(
scopeName: string
): Promise<IGrammarDefinition> {
switch (scopeName) {
case "source.vue":
return {
format: "json",
content: await import("shiki/languages/vue.tmLanguage.json"),
};
case "source.ts":
return {
format: "json",
content: await import("shiki/languages/typescript.tmLanguage.json"),
};
case "source.js":
return {
format: "json",
content: await import("shiki/languages/javascript.tmLanguage.json"),
};
case "text.html.basic":
return {
format: "json",
content: await import("shiki/languages/html.tmLanguage.json"),
};
case "source.css":
return {
format: "json",
content: await import("shiki/languages/css.tmLanguage.json"),
};
default:
return {
format: "json",
content: {
scopeName: "source",
patterns: [],
},
};
}
}

export async function loadGrammars(
monaco: typeof import("monaco-editor-core"),
editor: import("monaco-editor-core").editor.IStandaloneCodeEditor
) {
const registry = new Registry({
getGrammarDefinition: async (scopeName) => {
const dispatch = await dispatchGrammars(scopeName);
return JSON.parse(JSON.stringify(dispatch));
},
});
const grammars = new Map();
grammars.set("vue", "source.vue");
grammars.set("javascript", "source.js");
grammars.set("typescript", "source.ts");
grammars.set("css", "source.css");
grammars.set("html", "text.html.basic");

for (const lang of grammars.keys()) {
monaco.languages.register({
id: lang,
});
}

await wireTmGrammars(monaco as any, registry, grammars, editor as any);
}
Loading

0 comments on commit 73f630d

Please sign in to comment.