From 01ca22bcd7264fb4ec64397e049db8b52db4f0fc Mon Sep 17 00:00:00 2001 From: Mohsin Kaleem Date: Sun, 26 Nov 2023 10:49:21 +0000 Subject: [PATCH] Add js-beautify based formatters (#229) --- CHANGELOG.md | 7 +++ apheleia-formatters.el | 19 ++++--- apheleia-utils.el | 49 +++++++++++-------- test/formatters/installers/css-beautify.bash | 1 + test/formatters/installers/html-beautify.bash | 1 + test/formatters/installers/js-beautify.bash | 1 + .../formatters/samplecode/css-beautify/in.css | 1 + .../samplecode/css-beautify/in.scss | 1 + .../samplecode/css-beautify/out.css | 10 ++++ .../samplecode/css-beautify/out.scss | 11 +++++ .../samplecode/html-beautify/in.html | 1 + .../samplecode/html-beautify/out.html | 1 + test/formatters/samplecode/js-beautify/in.js | 1 + test/formatters/samplecode/js-beautify/out.js | 11 +++++ 14 files changed, 86 insertions(+), 29 deletions(-) create mode 120000 test/formatters/installers/css-beautify.bash create mode 120000 test/formatters/installers/html-beautify.bash create mode 100644 test/formatters/installers/js-beautify.bash create mode 120000 test/formatters/samplecode/css-beautify/in.css create mode 120000 test/formatters/samplecode/css-beautify/in.scss create mode 100644 test/formatters/samplecode/css-beautify/out.css create mode 100644 test/formatters/samplecode/css-beautify/out.scss create mode 120000 test/formatters/samplecode/html-beautify/in.html create mode 100644 test/formatters/samplecode/html-beautify/out.html create mode 120000 test/formatters/samplecode/js-beautify/in.js create mode 100644 test/formatters/samplecode/js-beautify/out.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 29f1873e..b64bf823 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -96,6 +96,12 @@ The format is based on [Keep a Changelog]. * [`xmllint`](https://gitlab.gnome.org/GNOME/libxml2) for XML ([#251]). * [`yapf`](https://github.com/google/yapf) for [Python](https://www.python.org/) ([#196]) * [`yq`](https://mikefarah.gitbook.io/yq/) for YAML, JSON, CSV, TSV, XML and [.properties](https://en.wikipedia.org/wiki/.properties) ([#250]). +* [`js-beautify`](https://github.com/beautify-web/js-beautify) for + [JavaScript](https://www.javascript.com/), + [JSON](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON), + [HTML](https://en.wikipedia.org/wiki/HTML) and + [CSS](https://www.google.com/search?q=css) + ([#229]) [#167]: https://github.com/radian-software/apheleia/pull/167 [#168]: https://github.com/radian-software/apheleia/pull/168 @@ -118,6 +124,7 @@ The format is based on [Keep a Changelog]. [#214]: https://github.com/radian-software/apheleia/pull/214 [#215]: https://github.com/radian-software/apheleia/pull/215 [#223]: https://github.com/radian-software/apheleia/pull/223 +[#229]: https://github.com/radian-software/apheleia/pull/229 [#231]: https://github.com/radian-software/apheleia/pull/231 [#232]: https://github.com/radian-software/apheleia/issues/232 [#236]: https://github.com/radian-software/apheleia/pull/236 diff --git a/apheleia-formatters.el b/apheleia-formatters.el index bb20b8bc..d1947ec8 100644 --- a/apheleia-formatters.el +++ b/apheleia-formatters.el @@ -43,6 +43,9 @@ ".c"))) (cmake-format . ("cmake-format" "-")) (crystal-tool-format . ("crystal" "tool" "format" "-")) + (css-beautify "css-beautify" "--file" "-" "--end-with-newline" + (apheleia-formatters-indent + "--indent-with-tabs" "--indent-size")) (dart-format . ("dart" "format")) (elm-format . ("elm-format" "--yes" "--stdin")) (fish-indent . ("fish_indent")) @@ -53,6 +56,9 @@ (goimports . ("goimports")) (google-java-format . ("google-java-format" "-")) (hclfmt . ("hclfmt")) + (html-beautify "html-beautify" "--file" "-" "--end-with-newline" + (apheleia-formatters-indent + "--indent-with-tabs" "--indent-size")) (html-tidy "tidy" "--quiet" "yes" "--tidy-mark" "no" @@ -61,17 +67,14 @@ (when (derived-mode-p 'nxml-mode) "-xml") (apheleia-formatters-indent - "--indent-with-tabs" - "--indent-spaces" - (cond - ((derived-mode-p 'nxml-mode) - 'nxml-child-indent) - ((derived-mode-p 'web-mode) - 'web-mode-indent-style))) + "--indent-with-tabs" "--indent-spaces") (apheleia-formatters-fill-column "-wrap")) (isort . ("isort" "-")) + (js-beautify "js-beautify" "--file" "-" "--end-with-newline" + (apheleia-formatters-indent + "--indent-with-tabs" "--indent-size")) (jq "jq" "." "-M" - (apheleia-formatters-js-indent "--tab" "--indent")) + (apheleia-formatters-indent "--tab" "--indent")) (lisp-indent . apheleia-indent-lisp-buffer) (ktlint . ("ktlint" "--log-level=none" "--stdin" "-F" "-")) (latexindent . ("latexindent" "--logfile=/dev/null")) diff --git a/apheleia-utils.el b/apheleia-utils.el index 7ca30bb1..f3f6fac6 100644 --- a/apheleia-utils.el +++ b/apheleia-utils.el @@ -16,13 +16,15 @@ :type 'boolean :group 'apheleia) -(defun apheleia-formatters-indent (tab-flag indent-flag indent-var) +(defun apheleia-formatters-indent (tab-flag indent-flag &optional indent-var) "Set flag for indentation. Helper function for `apheleia-formatters' which allows you to supply alternating flags based on the current buffers indent configuration. If the buffer is indented with tabs then returns TAB-FLAG. Otherwise if INDENT-VAR is set in the buffer return INDENT-FLAG and the value of INDENT-VAR. Use this -to easily configure the indentation level of a formatter. +to easily configure the indentation level of a formatter. If INDENT-VAR is +unset then intelligently try to determine the indentation variable based on +the current mode. If `apheleia-formatters-respect-indent-level' is nil then this always returns nil to defer to the formatter." @@ -30,28 +32,33 @@ always returns nil to defer to the formatter." ((not apheleia-formatters-respect-indent-level) nil) (indent-tabs-mode tab-flag) (indent-var - (when-let ((indent (and (boundp indent-var) + (unless indent-var + (setq indent-var + (cl-case major-mode + (css-mode 'css-indent-offset) + (css-ts-mode 'css-indent-offset) + (js-jsx-mode 'js-indent-level) + (js-ts-mode 'js-indent-level) + (js-mode 'js-indent-level) + (js2-jsx-mode 'js2-basic-offset) + (js2-mode 'js2-basic-offset) + (js3-mode 'js3-indent-level) + (json-mode 'js-indent-level) + (json-ts-mode 'json-ts-mode-indent-offset) + (nxml-mode 'nxml-child-indent) + (scss-mode 'css-indent-offset) + (web-mode 'web-mode-indent-style) + (tsx-ts-mode 'typescript-ts-mode-indent-offset) + (typescript-mode 'typescript-indent-level) + (typescript-ts-mode 'typescript-ts-mode-indent-offset)))) + + (when-let ((indent (and indent-var + (boundp indent-var) (symbol-value indent-var)))) (list indent-flag (number-to-string indent)))))) -(defun apheleia-formatters-js-indent (tab-flag indent-flag) - "Variant of `apheleia-formatters-indent' for JavaScript like modes. -See `apheleia-formatters-indent' for a description of TAB-FLAG and -INDENT-FLAG." - (apheleia-formatters-indent - tab-flag indent-flag - (cl-case major-mode - (json-mode 'js-indent-level) - (json-ts-mode 'json-ts-mode-indent-offset) - (js-mode 'js-indent-level) - (js-jsx-mode 'js-indent-level) - (js-ts-mode 'js-indent-level) - (js2-mode 'js2-basic-offset) - (js2-jsx-mode 'js2-basic-offset) - (js3-mode 'js3-indent-level) - (tsx-ts-mode 'typescript-ts-mode-indent-offset) - (typescript-mode 'typescript-indent-level) - (typescript-ts-mode 'typescript-ts-mode-indent-offset)))) +(define-obsolete-function-alias 'apheleia-formatters-js-indent + 'apheleia-formatters-indent "3.2") (defcustom apheleia-formatters-respect-fill-column nil "Whether formatters should set `fill-column' related flags." diff --git a/test/formatters/installers/css-beautify.bash b/test/formatters/installers/css-beautify.bash new file mode 120000 index 00000000..1797a1ce --- /dev/null +++ b/test/formatters/installers/css-beautify.bash @@ -0,0 +1 @@ +js-beautify.bash \ No newline at end of file diff --git a/test/formatters/installers/html-beautify.bash b/test/formatters/installers/html-beautify.bash new file mode 120000 index 00000000..1797a1ce --- /dev/null +++ b/test/formatters/installers/html-beautify.bash @@ -0,0 +1 @@ +js-beautify.bash \ No newline at end of file diff --git a/test/formatters/installers/js-beautify.bash b/test/formatters/installers/js-beautify.bash new file mode 100644 index 00000000..39b865e2 --- /dev/null +++ b/test/formatters/installers/js-beautify.bash @@ -0,0 +1 @@ +npm install -g js-beautify diff --git a/test/formatters/samplecode/css-beautify/in.css b/test/formatters/samplecode/css-beautify/in.css new file mode 120000 index 00000000..af14e8b1 --- /dev/null +++ b/test/formatters/samplecode/css-beautify/in.css @@ -0,0 +1 @@ +../prettier/in.css \ No newline at end of file diff --git a/test/formatters/samplecode/css-beautify/in.scss b/test/formatters/samplecode/css-beautify/in.scss new file mode 120000 index 00000000..8b93aa48 --- /dev/null +++ b/test/formatters/samplecode/css-beautify/in.scss @@ -0,0 +1 @@ +../prettier/in.scss \ No newline at end of file diff --git a/test/formatters/samplecode/css-beautify/out.css b/test/formatters/samplecode/css-beautify/out.css new file mode 100644 index 00000000..76e923da --- /dev/null +++ b/test/formatters/samplecode/css-beautify/out.css @@ -0,0 +1,10 @@ + body { + padding-left: 11em; + font-family: Georgia, + + "Times New Roman", + Times, serif; + color: purple; + background-color: + #d8da3d + } diff --git a/test/formatters/samplecode/css-beautify/out.scss b/test/formatters/samplecode/css-beautify/out.scss new file mode 100644 index 00000000..7d18aad4 --- /dev/null +++ b/test/formatters/samplecode/css-beautify/out.scss @@ -0,0 +1,11 @@ +/* Define standard variables and values for website */ +$bgcolor: lightblue; +$textcolor: darkblue; +$fontsize: 18px; + +/* Use the variables */ +body { + background-color: $bgcolor; + color: $textcolor; + font-size: $fontsize; +} diff --git a/test/formatters/samplecode/html-beautify/in.html b/test/formatters/samplecode/html-beautify/in.html new file mode 120000 index 00000000..540176f1 --- /dev/null +++ b/test/formatters/samplecode/html-beautify/in.html @@ -0,0 +1 @@ +../prettier/in.html \ No newline at end of file diff --git a/test/formatters/samplecode/html-beautify/out.html b/test/formatters/samplecode/html-beautify/out.html new file mode 100644 index 00000000..87c9e3e6 --- /dev/null +++ b/test/formatters/samplecode/html-beautify/out.html @@ -0,0 +1 @@ +

Minify HTML and any CSS or JS included in your markup

diff --git a/test/formatters/samplecode/js-beautify/in.js b/test/formatters/samplecode/js-beautify/in.js new file mode 120000 index 00000000..61533015 --- /dev/null +++ b/test/formatters/samplecode/js-beautify/in.js @@ -0,0 +1 @@ +../prettier/in.js \ No newline at end of file diff --git a/test/formatters/samplecode/js-beautify/out.js b/test/formatters/samplecode/js-beautify/out.js new file mode 100644 index 00000000..69c74579 --- /dev/null +++ b/test/formatters/samplecode/js-beautify/out.js @@ -0,0 +1,11 @@ +function HelloWorld({ + greeting = "hello", + greeted = '"World"', + silent = false, + onMouseOver, +}) { + + if (!greeting) { + return null + }; +}