2022-08-03 14:40:16 -04:00
|
|
|
import {StreamLanguage} from "@codemirror/language"
|
|
|
|
|
2023-02-17 17:14:34 -05:00
|
|
|
import {css} from '@codemirror/legacy-modes/mode/css';
|
|
|
|
import {c, cpp, csharp, java, kotlin, scala, dart} from '@codemirror/legacy-modes/mode/clike';
|
2022-08-03 14:40:16 -04:00
|
|
|
import {diff} from '@codemirror/legacy-modes/mode/diff';
|
|
|
|
import {fortran} from '@codemirror/legacy-modes/mode/fortran';
|
|
|
|
import {go} from '@codemirror/legacy-modes/mode/go';
|
|
|
|
import {haskell} from '@codemirror/legacy-modes/mode/haskell';
|
2023-02-17 17:14:34 -05:00
|
|
|
import {javascript, json, typescript} from '@codemirror/legacy-modes/mode/javascript';
|
2022-08-03 14:40:16 -04:00
|
|
|
import {julia} from '@codemirror/legacy-modes/mode/julia';
|
|
|
|
import {lua} from '@codemirror/legacy-modes/mode/lua';
|
|
|
|
import {markdown} from '@codemirror/lang-markdown';
|
|
|
|
import {oCaml, fSharp, sml} from '@codemirror/legacy-modes/mode/mllike';
|
|
|
|
import {nginx} from '@codemirror/legacy-modes/mode/nginx';
|
2023-02-17 17:14:34 -05:00
|
|
|
import {octave} from '@codemirror/legacy-modes/mode/octave';
|
2022-08-03 14:40:16 -04:00
|
|
|
import {perl} from '@codemirror/legacy-modes/mode/perl';
|
|
|
|
import {pascal} from '@codemirror/legacy-modes/mode/pascal';
|
|
|
|
import {php} from '@codemirror/lang-php';
|
|
|
|
import {powerShell} from '@codemirror/legacy-modes/mode/powershell';
|
|
|
|
import {properties} from '@codemirror/legacy-modes/mode/properties';
|
2023-02-17 17:14:34 -05:00
|
|
|
import {python} from '@codemirror/legacy-modes/mode/python';
|
2022-08-03 14:40:16 -04:00
|
|
|
import {ruby} from '@codemirror/legacy-modes/mode/ruby';
|
2023-02-17 17:14:34 -05:00
|
|
|
import {rust} from '@codemirror/legacy-modes/mode/rust';
|
|
|
|
import {scheme} from '@codemirror/legacy-modes/mode/scheme';
|
2022-08-03 14:40:16 -04:00
|
|
|
import {shell} from '@codemirror/legacy-modes/mode/shell';
|
2023-02-17 17:14:34 -05:00
|
|
|
import {standardSQL, pgSQL, msSQL, mySQL, sqlite, plSQL} from '@codemirror/legacy-modes/mode/sql';
|
2022-08-03 14:40:16 -04:00
|
|
|
import {stex} from '@codemirror/legacy-modes/mode/stex';
|
|
|
|
import {toml} from '@codemirror/legacy-modes/mode/toml';
|
2023-02-17 17:14:34 -05:00
|
|
|
// import {twig, smarty} from '@codemirror/legacy-modes/mode/php'; // TODO
|
2022-08-03 14:40:16 -04:00
|
|
|
import {vb} from '@codemirror/legacy-modes/mode/vb';
|
|
|
|
import {vbScript} from '@codemirror/legacy-modes/mode/vbscript';
|
2023-02-17 17:14:34 -05:00
|
|
|
import {xml, html} from '@codemirror/legacy-modes/mode/xml';
|
2022-08-03 14:40:16 -04:00
|
|
|
import {yaml} from '@codemirror/legacy-modes/mode/yaml';
|
2023-02-17 17:14:34 -05:00
|
|
|
import {swift} from "@codemirror/legacy-modes/mode/swift";
|
2022-08-03 14:40:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
// Mapping of possible languages or formats from user input to their codemirror modes.
|
|
|
|
// Value can be a mode string or a function that will receive the code content & return the mode string.
|
|
|
|
// The function option is used in the event the exact mode could be dynamic depending on the code.
|
|
|
|
const modeMap = {
|
|
|
|
bash: () => StreamLanguage.define(shell),
|
|
|
|
c: () => StreamLanguage.define(c),
|
2023-02-17 17:14:34 -05:00
|
|
|
css: () => StreamLanguage.define(css),
|
|
|
|
'c++': () => StreamLanguage.define(cpp),
|
2022-08-03 14:40:16 -04:00
|
|
|
'c#': () => StreamLanguage.define(csharp),
|
|
|
|
csharp: () => StreamLanguage.define(csharp),
|
2023-02-17 17:14:34 -05:00
|
|
|
dart: () => StreamLanguage.define(dart),
|
2022-08-03 14:40:16 -04:00
|
|
|
diff: () => StreamLanguage.define(diff),
|
|
|
|
for: () => StreamLanguage.define(fortran),
|
|
|
|
fortran: () => StreamLanguage.define(fortran),
|
|
|
|
'f#': () => StreamLanguage.define(fSharp),
|
|
|
|
fsharp: () => StreamLanguage.define(fSharp),
|
|
|
|
go: () => StreamLanguage.define(go),
|
|
|
|
haskell: () => StreamLanguage.define(haskell),
|
|
|
|
hs: () => StreamLanguage.define(haskell),
|
2023-02-17 17:14:34 -05:00
|
|
|
html: () => StreamLanguage.define(html),
|
2022-08-03 14:40:16 -04:00
|
|
|
ini: () => StreamLanguage.define(properties),
|
2023-02-17 17:14:34 -05:00
|
|
|
java: () => StreamLanguage.define(java),
|
|
|
|
javascript: () => StreamLanguage.define(javascript),
|
|
|
|
json: () => StreamLanguage.define(json),
|
|
|
|
js: () => StreamLanguage.define(javascript),
|
2022-08-03 14:40:16 -04:00
|
|
|
jl: () => StreamLanguage.define(julia),
|
|
|
|
julia: () => StreamLanguage.define(julia),
|
2022-08-04 08:33:51 -04:00
|
|
|
kotlin: () => StreamLanguage.define(kotlin),
|
2022-08-03 14:40:16 -04:00
|
|
|
latex: () => StreamLanguage.define(stex),
|
|
|
|
lua: () => StreamLanguage.define(lua),
|
2022-08-04 08:33:51 -04:00
|
|
|
markdown: () => markdown(),
|
2023-02-17 17:14:34 -05:00
|
|
|
matlab: () => StreamLanguage.define(octave),
|
2022-08-04 08:33:51 -04:00
|
|
|
md: () => markdown(),
|
|
|
|
mdown: () => markdown(),
|
2022-08-03 14:40:16 -04:00
|
|
|
ml: () => StreamLanguage.define(sml),
|
2023-02-17 17:14:34 -05:00
|
|
|
mssql: () => StreamLanguage.define(msSQL),
|
|
|
|
mysql: () => StreamLanguage.define(mySQL),
|
2022-08-03 14:40:16 -04:00
|
|
|
nginx: () => StreamLanguage.define(nginx),
|
2023-02-17 17:14:34 -05:00
|
|
|
octave: () => StreamLanguage.define(octave),
|
2022-08-03 14:40:16 -04:00
|
|
|
pas: () => StreamLanguage.define(pascal),
|
2022-08-04 08:33:51 -04:00
|
|
|
pascal: () => StreamLanguage.define(pascal),
|
|
|
|
perl: () => StreamLanguage.define(perl),
|
2023-02-17 17:14:34 -05:00
|
|
|
pgsql: () => StreamLanguage.define(pgSQL),
|
2022-08-03 14:40:16 -04:00
|
|
|
php: (code) => {
|
|
|
|
const hasTags = code.includes('<?php');
|
|
|
|
return php({plain: !hasTags});
|
|
|
|
},
|
2022-08-04 08:33:51 -04:00
|
|
|
pl: () => StreamLanguage.define(perl),
|
2023-02-17 17:14:34 -05:00
|
|
|
'pl/sql': () => StreamLanguage.define(plSQL),
|
|
|
|
postgresql: () => StreamLanguage.define(pgSQL),
|
2022-08-04 08:33:51 -04:00
|
|
|
powershell: () => StreamLanguage.define(powerShell),
|
|
|
|
properties: () => StreamLanguage.define(properties),
|
|
|
|
ocaml: () => StreamLanguage.define(oCaml),
|
2023-02-17 17:14:34 -05:00
|
|
|
py: () => StreamLanguage.define(python),
|
|
|
|
python: () => StreamLanguage.define(python),
|
2022-08-03 14:40:16 -04:00
|
|
|
rb: () => StreamLanguage.define(ruby),
|
2023-02-17 17:14:34 -05:00
|
|
|
rs: () => StreamLanguage.define(rust),
|
2022-08-04 08:33:51 -04:00
|
|
|
ruby: () => StreamLanguage.define(ruby),
|
2023-02-17 17:14:34 -05:00
|
|
|
rust: () => StreamLanguage.define(rust),
|
2022-08-04 08:33:51 -04:00
|
|
|
scala: () => StreamLanguage.define(scala),
|
2023-02-17 17:14:34 -05:00
|
|
|
scheme: () => StreamLanguage.define(scheme),
|
2022-08-03 14:40:16 -04:00
|
|
|
shell: () => StreamLanguage.define(shell),
|
|
|
|
sh: () => StreamLanguage.define(shell),
|
|
|
|
stext: () => StreamLanguage.define(stex),
|
2023-02-17 17:14:34 -05:00
|
|
|
swift: () => StreamLanguage.define(swift),
|
2022-08-03 14:40:16 -04:00
|
|
|
toml: () => StreamLanguage.define(toml),
|
2023-02-17 17:14:34 -05:00
|
|
|
ts: () => StreamLanguage.define(typescript),
|
|
|
|
typescript: () => StreamLanguage.define(typescript),
|
|
|
|
sql: () => StreamLanguage.define(standardSQL),
|
|
|
|
sqlite: () => StreamLanguage.define(sqlite),
|
2022-08-03 14:40:16 -04:00
|
|
|
vbs: () => StreamLanguage.define(vbScript),
|
|
|
|
vbscript: () => StreamLanguage.define(vbScript),
|
|
|
|
'vb.net': () => StreamLanguage.define(vb),
|
|
|
|
vbnet: () => StreamLanguage.define(vb),
|
2023-02-17 17:14:34 -05:00
|
|
|
xml: () => StreamLanguage.define(xml),
|
2022-08-03 14:40:16 -04:00
|
|
|
yaml: () => StreamLanguage.define(yaml),
|
|
|
|
yml: () => StreamLanguage.define(yaml),
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the relevant codemirror language extension based upon the given language
|
|
|
|
* suggestion and content.
|
|
|
|
* @param {String} langSuggestion
|
|
|
|
* @param {String} content
|
|
|
|
* @returns {StreamLanguage}
|
|
|
|
*/
|
|
|
|
export function getLanguageExtension(langSuggestion, content) {
|
|
|
|
const suggestion = langSuggestion.trim().replace(/^\./g, '').toLowerCase();
|
|
|
|
|
|
|
|
const language = modeMap[suggestion];
|
|
|
|
|
|
|
|
if (typeof language === 'undefined') {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return language(content);
|
|
|
|
}
|