import type { Extension } from "@codemirror/state"; import { StreamLanguage } from "@codemirror/language"; import { markdown, markdownLanguage } from "@codemirror/lang-markdown"; import { extensionOf } from "../workspace/chrome"; import { languageFor } from "./languageFor"; import { noteFormats } from "./noteFormats"; /** The Markdown language of Write and Source, with the RuneStone note formats. */ export const noteMarkdown = () => markdown({ base: markdownLanguage, extensions: [noteFormats] }); // Each language loads when the first file of that language opens. const loaders: Record Promise> = { markdown: async () => noteMarkdown(), javascript: async extension => (await import("@codemirror/lang-javascript")).javascript({ jsx: extension === "jsx" }), typescript: async extension => (await import("@codemirror/lang-javascript")).javascript({ typescript: true, jsx: extension === "tsx" }), python: async () => (await import("@codemirror/lang-python")).python(), json: async () => (await import("@codemirror/lang-json")).json(), css: async () => (await import("@codemirror/lang-css")).css(), scss: async () => (await import("@codemirror/lang-sass")).sass(), html: async () => (await import("@codemirror/lang-html")).html(), rust: async () => (await import("@codemirror/lang-rust")).rust(), shell: async () => StreamLanguage.define((await import("@codemirror/legacy-modes/mode/shell")).shell), yaml: async () => (await import("@codemirror/lang-yaml")).yaml(), ini: async () => StreamLanguage.define((await import("@codemirror/legacy-modes/mode/toml")).toml), xml: async () => (await import("@codemirror/lang-xml")).xml(), }; /** The syntax support for a file. Plain text gets none. */ export async function languageSupport(relPath: string): Promise { return await loaders[languageFor(relPath)]?.(extensionOf(relPath)) ?? []; }