aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md7
-rw-r--r--apheleia-formatters.el19
-rw-r--r--apheleia-utils.el49
l---------test/formatters/installers/css-beautify.bash1
l---------test/formatters/installers/html-beautify.bash1
-rw-r--r--test/formatters/installers/js-beautify.bash1
l---------test/formatters/samplecode/css-beautify/in.css1
l---------test/formatters/samplecode/css-beautify/in.scss1
-rw-r--r--test/formatters/samplecode/css-beautify/out.css10
-rw-r--r--test/formatters/samplecode/css-beautify/out.scss11
l---------test/formatters/samplecode/html-beautify/in.html1
-rw-r--r--test/formatters/samplecode/html-beautify/out.html1
l---------test/formatters/samplecode/js-beautify/in.js1
-rw-r--r--test/formatters/samplecode/js-beautify/out.js11
14 files changed, 86 insertions, 29 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 29f1873..b64bf82 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 bb20b8b..d1947ec 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 7ca30bb..f3f6fac 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 0000000..1797a1c
--- /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 0000000..1797a1c
--- /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 0000000..39b865e
--- /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 0000000..af14e8b
--- /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 0000000..8b93aa4
--- /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 0000000..76e923d
--- /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 0000000..7d18aad
--- /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 0000000..540176f
--- /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 0000000..87c9e3e
--- /dev/null
+++ b/test/formatters/samplecode/html-beautify/out.html
@@ -0,0 +1 @@
+<h2>Minify <abbr title="HyperText Markup Language">HTML</abbr> and any <abbr title="Cascading Style Sheets">CSS</abbr> or <abbr title="JavaScript">JS</abbr> included in your markup</h2>
diff --git a/test/formatters/samplecode/js-beautify/in.js b/test/formatters/samplecode/js-beautify/in.js
new file mode 120000
index 0000000..6153301
--- /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 0000000..69c7457
--- /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
+ };
+}