gradio/frontend
1403k
1import type { Extension } from "@codemirror/state";2import { StreamLanguage } from "@codemirror/language";3import { _ } from "svelte-i18n";4 5const sql_dialects = [6 "standardSQL",7 "msSQL",8 "mySQL",9 "mariaDB",10 "sqlite",11 "cassandra",12 "plSQL",13 "hive",14 "pgSQL",15 "gql",16 "gpSQL",17 "sparkSQL",18 "esper"19] as const;20 21const lang_map: Record<string, (() => Promise<Extension>) | undefined> = {22 python: () => import("@codemirror/lang-python").then((m) => m.python()),23 c: () =>24 import("@codemirror/legacy-modes/mode/clike").then((m) =>25 StreamLanguage.define(m.c)26 ),27 cpp: () =>28 import("@codemirror/legacy-modes/mode/clike").then((m) =>29 StreamLanguage.define(m.cpp)30 ),31 markdown: async () => {32 const [md, frontmatter] = await Promise.all([33 import("@codemirror/lang-markdown"),34 import("./frontmatter")35 ]);36 return md.markdown({ extensions: [frontmatter.frontmatter] });37 },38 latex: () =>39 import("@codemirror/legacy-modes/mode/stex").then((m) =>40 StreamLanguage.define(m.stex)41 ),42 json: () => import("@codemirror/lang-json").then((m) => m.json()),43 html: () => import("@codemirror/lang-html").then((m) => m.html()),44 css: () => import("@codemirror/lang-css").then((m) => m.css()),45 javascript: () =>46 import("@codemirror/lang-javascript").then((m) => m.javascript()),47 jinja2: () =>48 import("@codemirror/legacy-modes/mode/jinja2").then((m) =>49 StreamLanguage.define(m.jinja2)50 ),51 typescript: () =>52 import("@codemirror/lang-javascript").then((m) =>53 m.javascript({ typescript: true })54 ),55 yaml: () =>56 import("@codemirror/legacy-modes/mode/yaml").then((m) =>57 StreamLanguage.define(m.yaml)58 ),59 dockerfile: () =>60 import("@codemirror/legacy-modes/mode/dockerfile").then((m) =>61 StreamLanguage.define(m.dockerFile)62 ),63 shell: () =>64 import("@codemirror/legacy-modes/mode/shell").then((m) =>65 StreamLanguage.define(m.shell)66 ),67 r: () =>68 import("@codemirror/legacy-modes/mode/r").then((m) =>69 StreamLanguage.define(m.r)70 ),71 sql: () =>72 import("@codemirror/legacy-modes/mode/sql").then((m) =>73 StreamLanguage.define(m.standardSQL)74 ),75 ...Object.fromEntries(76 sql_dialects.map((dialect) => [77 "sql-" + dialect,78 () =>79 import("@codemirror/legacy-modes/mode/sql").then((m) =>80 StreamLanguage.define(m[dialect])81 )82 ])83 )84} as const;85 86const alias_map: Record<string, string> = {87 py: "python",88 md: "markdown",89 js: "javascript",90 ts: "typescript",91 sh: "shell"92};93 94export async function getLanguageExtension(95 lang: string96): Promise<Extension | undefined> {97 const _lang = lang_map[lang] || lang_map[alias_map[lang]] || undefined;98 if (_lang) {99 return _lang();100 }101 return undefined;102}103 