mirror of
https://github.com/vale981/apheleia
synced 2025-03-04 09:01:42 -05:00
Add js-beautify based formatters (#229)
This commit is contained in:
parent
731edd2954
commit
01ca22bcd7
14 changed files with 86 additions and 29 deletions
|
@ -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
|
||||
|
|
|
@ -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"))
|
||||
|
|
|
@ -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."
|
||||
|
|
1
test/formatters/installers/css-beautify.bash
Symbolic link
1
test/formatters/installers/css-beautify.bash
Symbolic link
|
@ -0,0 +1 @@
|
|||
js-beautify.bash
|
1
test/formatters/installers/html-beautify.bash
Symbolic link
1
test/formatters/installers/html-beautify.bash
Symbolic link
|
@ -0,0 +1 @@
|
|||
js-beautify.bash
|
1
test/formatters/installers/js-beautify.bash
Normal file
1
test/formatters/installers/js-beautify.bash
Normal file
|
@ -0,0 +1 @@
|
|||
npm install -g js-beautify
|
1
test/formatters/samplecode/css-beautify/in.css
Symbolic link
1
test/formatters/samplecode/css-beautify/in.css
Symbolic link
|
@ -0,0 +1 @@
|
|||
../prettier/in.css
|
1
test/formatters/samplecode/css-beautify/in.scss
Symbolic link
1
test/formatters/samplecode/css-beautify/in.scss
Symbolic link
|
@ -0,0 +1 @@
|
|||
../prettier/in.scss
|
10
test/formatters/samplecode/css-beautify/out.css
Normal file
10
test/formatters/samplecode/css-beautify/out.css
Normal file
|
@ -0,0 +1,10 @@
|
|||
body {
|
||||
padding-left: 11em;
|
||||
font-family: Georgia,
|
||||
|
||||
"Times New Roman",
|
||||
Times, serif;
|
||||
color: purple;
|
||||
background-color:
|
||||
#d8da3d
|
||||
}
|
11
test/formatters/samplecode/css-beautify/out.scss
Normal file
11
test/formatters/samplecode/css-beautify/out.scss
Normal file
|
@ -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
test/formatters/samplecode/html-beautify/in.html
Symbolic link
1
test/formatters/samplecode/html-beautify/in.html
Symbolic link
|
@ -0,0 +1 @@
|
|||
../prettier/in.html
|
1
test/formatters/samplecode/html-beautify/out.html
Normal file
1
test/formatters/samplecode/html-beautify/out.html
Normal file
|
@ -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
test/formatters/samplecode/js-beautify/in.js
Symbolic link
1
test/formatters/samplecode/js-beautify/in.js
Symbolic link
|
@ -0,0 +1 @@
|
|||
../prettier/in.js
|
11
test/formatters/samplecode/js-beautify/out.js
Normal file
11
test/formatters/samplecode/js-beautify/out.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
function HelloWorld({
|
||||
greeting = "hello",
|
||||
greeted = '"World"',
|
||||
silent = false,
|
||||
onMouseOver,
|
||||
}) {
|
||||
|
||||
if (!greeting) {
|
||||
return null
|
||||
};
|
||||
}
|
Loading…
Add table
Reference in a new issue