From 475f3465fe17ebc9932ca94d178818015e4372d3 Mon Sep 17 00:00:00 2001 From: Espen Jacobsson Date: Tue, 26 Dec 2023 00:47:28 +0100 Subject: [PATCH] Add TypeScript, JSX and TSX language support (#64) * Add language support for TypeScript * Add formatter for TypeScript * Add language autodetection for TypeScript * Add TypeScript to list of featured languages * Add language support for JSX and TSX * Add JSX and TSX to list of featured languages * Remove guesslang token from JSX and TSX * Fix syntax error --------- Co-authored-by: Jonatan Heyman --- README.md | 3 +++ public/langdetect-worker.js | 1 + src/editor/lang-heynote/heynote.grammar | 2 +- src/editor/lang-heynote/parser.js | 2 +- src/editor/languages.js | 24 +++++++++++++++++++++++- 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index eefd3fc..b9e8fa6 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ Available for Mac, Windows, and Linux. - HTML - Java - JavaScript + - JSX + - TypeScript + - TSX - JSON - Lezer - Markdown diff --git a/public/langdetect-worker.js b/public/langdetect-worker.js index 3f93c6d..127e430 100644 --- a/public/langdetect-worker.js +++ b/public/langdetect-worker.js @@ -4,6 +4,7 @@ GUESSLANG_LANGUAGES = [ "json", "py", "js", + "ts", "html", "sql", "java", diff --git a/src/editor/lang-heynote/heynote.grammar b/src/editor/lang-heynote/heynote.grammar index fe4ebe4..84ae21e 100644 --- a/src/editor/lang-heynote/heynote.grammar +++ b/src/editor/lang-heynote/heynote.grammar @@ -11,7 +11,7 @@ NoteDelimiter { @tokens { noteDelimiterMark { "∞∞∞" } - NoteLanguage { "text" | "math" | "javascript" | "json" | "python" | "html" | "sql" | "markdown" | "java" | "php" | "css" | "xml" | "cpp" | "rust" | "csharp" | "ruby" | "shell" | "yaml" | "golang" | "clojure" | "erlang" | "lezer" } + NoteLanguage { "text" | "math" | "javascript" | "typescript" | "jsx" | "tsx" | "json" | "python" | "html" | "sql" | "markdown" | "java" | "php" | "css" | "xml" | "cpp" | "rust" | "csharp" | "ruby" | "shell" | "yaml" | "golang" | "clojure" | "erlang" | "lezer" } Auto { "-a" } noteDelimiterEnter { "\n" } //NoteContent { String } diff --git a/src/editor/lang-heynote/parser.js b/src/editor/lang-heynote/parser.js index b7e2c55..908676d 100644 --- a/src/editor/lang-heynote/parser.js +++ b/src/editor/lang-heynote/parser.js @@ -10,7 +10,7 @@ export const parser = LRParser.deserialize({ maxTerm: 10, skippedNodes: [0], repeatNodeCount: 1, - tokenData: "*c~R`YZ!T}!O!Y#V#W!e#X#Y$R#Z#[$q#[#]$w#^#_%Z#`#a&t#a#b'^#d#e(]#f#g(r#g#h)X#h#i)n#l#m$}#m#n)z%&x%&y*Q~!YOX~~!]P#T#U!`~!eOU~~!hR#`#a!q#d#e#f#g#h#l~!tP#c#d!w~!zP#^#_!}~#QP#i#j#T~#WP#f#g#Z~#^P#X#Y#a~#fOT~~#iP#d#e#a~#oQ#[#]#u#g#h#a~#xP#T#U#{~$OP#f#g#f~$UP#f#g$X~$[P#`#a$_~$bP#T#U$e~$hP#b#c$k~$nP#Z#[#a~$tP#c#d$X~$zP#h#i$}~%QP#a#b%T~%WP#`#a#a~%^Q#T#U%d#g#h&h~%gP#j#k%j~%mP#T#U%p~%uPT~#g#h%x~%{P#V#W&O~&RP#f#g&U~&XP#]#^&[~&_P#d#e&b~&eP#h#i#a~&kP#c#d&n~&qP#b#c#a~&wP#X#Y&z~&}P#n#o'Q~'TP#X#Y'W~'ZP#f#g#a~'aP#T#U'd~'gQ#f#g'm#h#i(V~'pP#_#`'s~'vP#W#X'y~'|P#c#d(P~(SP#k#l&n~(YP#[#]#a~(`Q#[#]#f#m#n(f~(iP#h#i(l~(oP#[#]&h~(uP#i#j(x~({Q#U#V)R#g#h&b~)UP#m#n#a~)[Q#[#])b#e#f%T~)eP#X#Y)h~)kP#`#a%T~)qP#X#Y)t~)wP#l#m&b~)}P#T#U$}~*TP%&x%&y*W~*ZP%&x%&y*^~*cOY~", + tokenData: "+[~R`YZ!T}!O!Y#V#W!e#X#Y$R#Z#[$q#[#]$w#^#_%Z#`#a&w#a#b'a#d#e(`#f#g({#g#h)b#h#i)w#l#m$}#m#n*s%&x%&y*y~!YOX~~!]P#T#U!`~!eOU~~!hR#`#a!q#d#e#f#g#h#l~!tP#c#d!w~!zP#^#_!}~#QP#i#j#T~#WP#f#g#Z~#^P#X#Y#a~#fOT~~#iP#d#e#a~#oQ#[#]#u#g#h#a~#xP#T#U#{~$OP#f#g#f~$UP#f#g$X~$[P#`#a$_~$bP#T#U$e~$hP#b#c$k~$nP#Z#[#a~$tP#c#d$X~$zP#h#i$}~%QP#a#b%T~%WP#`#a#a~%^Q#T#U%d#g#h&h~%gP#j#k%j~%mP#T#U%p~%uPT~#g#h%x~%{P#V#W&O~&RP#f#g&U~&XP#]#^&[~&_P#d#e&b~&eP#h#i#a~&kQ#c#d&q#l#m#a~&tP#b#c#a~&zP#X#Y&}~'QP#n#o'T~'WP#X#Y'Z~'^P#f#g#a~'dP#T#U'g~'jQ#f#g'p#h#i(Y~'sP#_#`'v~'yP#W#X'|~(PP#c#d(S~(VP#k#l&q~(]P#[#]#a~(cQ#[#]#f#m#n(i~(lP#h#i(o~(rP#[#](u~(xP#c#d&q~)OP#i#j)R~)UQ#U#V)[#g#h&b~)_P#m#n#a~)eQ#[#])k#e#f%T~)nP#X#Y)q~)tP#`#a%T~)zR#X#Y*T#g#h*Z#m#n*a~*WP#l#m&b~*^P#l#m#a~*dP#d#e*g~*jP#X#Y*m~*pP#g#h%x~*vP#T#U$}~*|P%&x%&y+P~+SP%&x%&y+V~+[OY~", tokenizers: [0, noteContent], topRules: {"Document":[0,2]}, tokenPrec: 0 diff --git a/src/editor/languages.js b/src/editor/languages.js index 6e8a1ec..f07824a 100644 --- a/src/editor/languages.js +++ b/src/editor/languages.js @@ -1,6 +1,6 @@ import { jsonLanguage } from "@codemirror/lang-json" import { pythonLanguage } from "@codemirror/lang-python" -import { javascriptLanguage } from "@codemirror/lang-javascript" +import { javascriptLanguage, jsxLanguage, tsxLanguage, typescriptLanguage } from "@codemirror/lang-javascript" import { htmlLanguage } from "@codemirror/lang-html" import { StandardSQL } from "@codemirror/lang-sql" import { markdownLanguage } from "@codemirror/lang-markdown" @@ -21,6 +21,7 @@ import { go } from "@codemirror/legacy-modes/mode/go" import { clojure } from "@codemirror/legacy-modes/mode/clojure" import { erlang } from "@codemirror/legacy-modes/mode/erlang" +import typescriptPlugin from "prettier/plugins/typescript.mjs" import babelPrettierPlugin from "prettier/plugins/babel.mjs" import htmlPrettierPlugin from "prettier/esm/parser-html.mjs" import cssPrettierPlugin from "prettier/esm/parser-postcss.mjs" @@ -189,6 +190,27 @@ export const LANGUAGES = [ guesslang: "js", prettier: {parser:"babel", plugins: [babelPrettierPlugin, prettierPluginEstree]}, }), + new Language({ + token: "jsx", + name: "JSX", + parser: jsxLanguage.parser, + guesslang: null, + prettier: {parser:"babel", plugins: [babelPrettierPlugin, prettierPluginEstree]}, + }), + new Language({ + token: "typescript", + name: "TypeScript", + parser: typescriptLanguage.parser, + guesslang: "ts", + prettier: {parser:"typescript", plugins: [typescriptPlugin, prettierPluginEstree]}, + }), + new Language({ + token: "tsx", + name: "TSX", + parser: tsxLanguage.parser, + guesslang: null, + prettier: {parser:"typescript", plugins: [typescriptPlugin, prettierPluginEstree]}, + }), ]