Skip to content

Commit

Permalink
Add js-beautify based formatters (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohkale authored Nov 26, 2023
1 parent 731edd2 commit 01ca22b
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 29 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 11 additions & 8 deletions apheleia-formatters.el
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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"
Expand All @@ -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"))
Expand Down
49 changes: 28 additions & 21 deletions apheleia-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,49 @@
: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."
(cond
((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."
Expand Down
1 change: 1 addition & 0 deletions test/formatters/installers/css-beautify.bash
1 change: 1 addition & 0 deletions test/formatters/installers/html-beautify.bash
1 change: 1 addition & 0 deletions test/formatters/installers/js-beautify.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npm install -g js-beautify
1 change: 1 addition & 0 deletions test/formatters/samplecode/css-beautify/in.css
1 change: 1 addition & 0 deletions test/formatters/samplecode/css-beautify/in.scss
10 changes: 10 additions & 0 deletions test/formatters/samplecode/css-beautify/out.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
body {
padding-left: 11em;
font-family: Georgia,

"Times New Roman",
Times, serif;
color: purple;
background-color:
#d8da3d
}
11 changes: 11 additions & 0 deletions test/formatters/samplecode/css-beautify/out.scss
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions test/formatters/samplecode/html-beautify/in.html
1 change: 1 addition & 0 deletions test/formatters/samplecode/html-beautify/out.html
Original file line number Diff line number Diff line change
@@ -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>
1 change: 1 addition & 0 deletions test/formatters/samplecode/js-beautify/in.js
11 changes: 11 additions & 0 deletions test/formatters/samplecode/js-beautify/out.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function HelloWorld({
greeting = "hello",
greeted = '"World"',
silent = false,
onMouseOver,
}) {

if (!greeting) {
return null
};
}

0 comments on commit 01ca22b

Please sign in to comment.