From bb511b868b6168bb7850adae4903761022497e54 Mon Sep 17 00:00:00 2001 From: Jonatan Heyman Date: Tue, 26 Dec 2023 00:27:43 +0100 Subject: [PATCH] Add support for more languages (#69) * Contain language selection dialog in an element that can be scrolled, and automatically scroll it if needed when navigating the list with arrow keys * Add support for more languages: Clojure, Erlang, Golang, Lezer, Ruby, Shell, YAML * Move prettier auto format settings for languages into Language() class * Remove invalid import * Fix bug that could cause auto formatting to fail for the last block. Add tests for language auto detection and formatting. * Fix broken tests * Fix language auto detection on Safari Webkit which was broken * Remove unnecessary wait time --- README.md | 7 + package-lock.json | 10 + package.json | 1 + playwright.config.ts | 2 +- public/langdetect-worker.js | 6 + src/components/LanguageSelector.vue | 59 ++++-- src/editor/block/format-code.js | 33 +--- src/editor/lang-heynote/heynote.grammar | 2 +- src/editor/lang-heynote/parser.js | 2 +- src/editor/language-detection/autodetect.js | 19 +- src/editor/languages.js | 194 ++++++++++++++++++-- tests/formatting.spec.js | 46 +++++ tests/language-detection.spec.js | 39 ++++ tests/markdown.spec.js | 9 +- tests/test-utils.js | 2 +- 15 files changed, 361 insertions(+), 70 deletions(-) create mode 100644 tests/formatting.spec.js create mode 100644 tests/language-detection.spec.js diff --git a/README.md b/README.md index be5a51b..eefd3fc 100644 --- a/README.md +++ b/README.md @@ -17,17 +17,24 @@ Available for Mac, Windows, and Linux. - Syntax highlighting - C++ - C# + - Clojure - CSS + - Erlang + - Go - HTML - Java - JavaScript - JSON + - Lezer - Markdown - PHP - Python + - Ruby - Rust + - Shell - SQL - XML + - YAML - Language auto-detection - Auto-formatting - Math/Calculator mode diff --git a/package-lock.json b/package-lock.json index 8500eb5..b30b3c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ "@codemirror/lang-sql": "^6.5.4", "@codemirror/lang-xml": "^6.0.2", "@codemirror/language": "^6.9.3", + "@codemirror/legacy-modes": "^6.3.3", "@codemirror/lint": "^6.4.2", "@codemirror/search": "^6.5.5", "@codemirror/state": "^6.3.3", @@ -453,6 +454,15 @@ "style-mod": "^4.0.0" } }, + "node_modules/@codemirror/legacy-modes": { + "version": "6.3.3", + "resolved": "https://registry.npmjs.org/@codemirror/legacy-modes/-/legacy-modes-6.3.3.tgz", + "integrity": "sha512-X0Z48odJ0KIoh/HY8Ltz75/4tDYc9msQf1E/2trlxFaFFhgjpVHjZ/BCXe1Lk7s4Gd67LL/CeEEHNI+xHOiESg==", + "dev": true, + "dependencies": { + "@codemirror/language": "^6.0.0" + } + }, "node_modules/@codemirror/lint": { "version": "6.4.2", "resolved": "https://registry.npmjs.org/@codemirror/lint/-/lint-6.4.2.tgz", diff --git a/package.json b/package.json index 51867ff..65fce09 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "@codemirror/lang-sql": "^6.5.4", "@codemirror/lang-xml": "^6.0.2", "@codemirror/language": "^6.9.3", + "@codemirror/legacy-modes": "^6.3.3", "@codemirror/lint": "^6.4.2", "@codemirror/search": "^6.5.5", "@codemirror/state": "^6.3.3", diff --git a/playwright.config.ts b/playwright.config.ts index 22c3505..d167fb1 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -41,7 +41,7 @@ export default defineConfig({ name: 'firefox', use: { ...devices['Desktop Firefox'] }, }, - + { name: 'webkit', use: { ...devices['Desktop Safari'] }, diff --git a/public/langdetect-worker.js b/public/langdetect-worker.js index b1c5b9b..3f93c6d 100644 --- a/public/langdetect-worker.js +++ b/public/langdetect-worker.js @@ -14,6 +14,12 @@ GUESSLANG_LANGUAGES = [ "rs", "md", "cs", + "rb", + "sh", + "yaml", + "go", + "clj", + "erl", ] const guessLang = new self.GuessLang() diff --git a/src/components/LanguageSelector.vue b/src/components/LanguageSelector.vue index de4a848..b43373f 100644 --- a/src/components/LanguageSelector.vue +++ b/src/components/LanguageSelector.vue @@ -37,9 +37,20 @@ if (event.key === "ArrowDown") { this.selected = Math.min(this.selected + 1, this.filteredItems.length - 1) event.preventDefault() + if (this.selected === this.filteredItems.length - 1) { + this.$refs.container.scrollIntoView({block: "end"}) + } else { + this.$refs.item[this.selected].scrollIntoView({block: "nearest"}) + } + } else if (event.key === "ArrowUp") { this.selected = Math.max(this.selected - 1, 0) event.preventDefault() + if (this.selected === 0) { + this.$refs.container.scrollIntoView({block: "start"}) + } else { + this.$refs.item[this.selected].scrollIntoView({block: "nearest"}) + } } else if (event.key === "Enter") { this.selectItem(this.filteredItems[this.selected].token) event.preventDefault() @@ -69,28 +80,38 @@