Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/code-formatter.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45// common configuration for mapping programming languages (lower case) to formatters6// this is used by webapp and the project78// ideally, this is the "syntax", but for historic reasons it's what is being called "parsed" and9// hence there there are additional entries for backwards compatibility with older projects.10// this shouldn't be necessary any more and could be removed.11export type Syntax =12| "r"13| "c"14| "c++"15| "clang"16| "latex"17| "go"18| "CSS"19| "html"20| "xml"21| "bibtex"22| "markdown"23| "Markdown"24| "Quarto"25| "knitr"26| "javascript" // backwards compatibility27| "json"28| "JSON"29| "latex"30| "yaml"31| "python"32| "python3"33| "zig"34| "py"35| "R"36| "RMarkdown"37| "TypeScript"38| "JavaScript"39// the ones below are to be eliminated (they're in "Tool")40| "prettier"41| "typescript"42| "css"43| "babel"44| "gofmt"45| "clang-format"46| "rust"47| "rustfmt" // deprecated, should be rust48| "tsx"49| "jsx"50| "yapf"51| "yapf3"52| "formatR"53| "latexindent"54| "bib-biber"55| "xml-tidy"56| "DOES_NOT_EXIST";5758export type Parser = Syntax;5960export type Tool =61| "r" // to be removed62| "yapf"63| "yapf3" // for python 364| "python" // should be yapf65| "knitr"66| "formatR"67| "clang-format"68| "latex"69| "latexindent"70| "gofmt"71| "xml-tidy"72| "rustfmt"73| "bib-biber"74| "prettier" // always available75| "css" // via prettier76| "babel" // via prettier77| "typescript" // via prettier78| "json" // via prettier79| "yaml" // via prettier80| "markdown" // via prettier81| "html" // via prettier82| "zig"83| "DOES_NOT_EXIST"; // use this for testing;8485// the file extensions where we want to have formatting support86export const file_extensions = [87"bib",88"c",89"c++",90"cc",91"cml" /* that's xml */,92"cpp",93"css",94"go",95"h",96"html",97"js",98"json",99"jsx",100"kml" /* geodata keyhole markup, also xml */,101"md",102"ptx",103"py",104"qmd",105"r",106"rmd",107"rs",108"tex",109"ts",110"tsx",111"xml",112"xsl",113"yaml",114"yml",115"zig",116] as const;117118export const fileExtensionsSet = new Set(file_extensions);119120// convert to type121export type Exts = (typeof file_extensions)[number];122123// associating filename extensions with a specific type of syntax for a parser124type Ext2Syntax = { [s in Exts]: Parser };125export const ext2syntax: Readonly<Ext2Syntax> = {126js: "JavaScript",127jsx: "jsx",128md: "Markdown",129rmd: "RMarkdown",130qmd: "Quarto",131css: "CSS",132ts: "TypeScript",133tsx: "tsx",134json: "JSON",135yaml: "yaml",136yml: "yaml",137py: "python3",138tex: "latex",139html: "html",140r: "R",141rs: "rust",142go: "go",143c: "clang",144cc: "clang",145"c++": "clang",146cpp: "clang",147h: "clang",148xml: "xml",149cml: "xml",150kml: "xml",151xsl: "xml",152ptx: "xml",153zig: "zig",154bib: "bibtex", // via biber --tool155} as const;156157export const ext2parser = ext2syntax;158159// those syntaxes (parser) which aren't handled by "prettier" (the default),160// have these special tools (command-line interface)161// (several ones are added for backwards compatibility)162type SyntaxConfig = { [s in Parser]: Tool };163export const syntax2tool: Readonly<Partial<SyntaxConfig>> = {164"c++": "clang-format",165"clang-format": "clang-format",166babel: "prettier",167bibtex: "bib-biber",168c: "clang-format",169clang: "clang-format",170CSS: "css", // in prettier171go: "gofmt",172gofmt: "gofmt",173html: "html", // via prettier174JavaScript: "babel", // in prettier175json: "json", // in prettier176JSON: "json", // in prettier177jsx: "babel", // in prettier178latex: "latex", // should be "latexindent",179markdown: "markdown", // in prettier180Markdown: "markdown", // in prettier181py: "python", // should be yapf or whatever …182python: "python", // should be yapf or whatever …183python3: "python", // should be yapf or whatever …184Quarto: "markdown", // same as RMarkdown, at least for now185r: "formatR",186R: "formatR",187RMarkdown: "markdown", // same as markdown, at last for now!188rust: "rustfmt",189tsx: "typescript", // in prettier190typescript: "typescript", // in prettier191TypeScript: "typescript", // in prettier192xml: "xml-tidy",193yaml: "yaml", // in prettier194zig: "zig",195} as const;196197export const parser2tool = syntax2tool;198199// Map (a subset of) syntax (aka "parser") to a human-readable language200// in order to communicate what syntaxes can be formatted.201type Langs = { [s in Parser]?: string };202203export const syntax2display: Readonly<Langs> = {204"bib-biber": "Bibtex",205"c++": "C++",206c: "C",207css: "CSS",208gofmt: "Go",209html: "HTML",210javascript: "JavaScript",211JavaScript: "JavaScript",212json: "JSON",213latex: "LaTeX",214markdown: "Markdown",215py: "Python",216r: "R Language",217rust: "Rust",218typescript: "TypeScript",219xml: "XML",220yaml: "YAML",221zig: "Zig",222} as const;223224export const parser2display = syntax2display;225226// pre-process mapping of each tool to human-readable language or text type227type Tool2Display = { [s in Tool]?: string[] };228229const t2d: Tool2Display = {};230for (const parser of Object.keys(parser2tool)) {231const tool = parser2tool[parser];232if (t2d[tool] == null) t2d[tool] = [];233const lang = parser2display[parser];234if (lang != null) t2d[tool].push(lang);235}236237for (const tool of Object.keys(t2d)) {238t2d[tool] = t2d[tool].sort();239}240241export const tool2display: Readonly<Tool2Display> = Object.freeze(t2d);242243export interface Config {244syntax: Syntax;245tabWidth?: number;246useTabs?: boolean;247}248249export interface Options extends Omit<Config, "syntax"> {250parser: Syntax; // TODO refactor this to tool251tabWidth?: number;252}253254255