[json] Add option to format on save

This commit is contained in:
Caleb Williams 2019-09-04 22:44:13 -05:00 committed by smile13241324
parent 1299f262ca
commit fe77ade0f0
4 changed files with 20 additions and 2 deletions

View File

@ -1773,6 +1773,8 @@ Other:
- Added missing optional argument for the =jabber-alert-echo= function to fix
=wrong-number-of-arguments= error when jabber receives a new notification
(thanks to Aleksei Fedotov)
**** Json
- Added variable =json-fmt-on-save= to run the selected formatter on save.
**** Jsonnet
- Fixed flycheck support in jsonnet files (thanks to Vikash Balasubramanian)
**** Java

View File

@ -61,6 +61,13 @@ Formatter can be chosen on a per project basis using directory local variables
*Note:* you can easily add a directory local variable with ~SPC f v d~.
** Formatting on save
To enable using the selected formatter on save, set the layer variable =json-fmt-on-save=:
#+BEGIN_SRC elisp
(json :variables json-fmt-on-save t)
#+END_SRC
* Usage
** Reformat
~SPC m =~ will reformat the whole buffer or the active region. Use numerical

View File

@ -13,3 +13,6 @@
(defvar json-fmt-tool 'web-beautify
"The formatter to format a JSON file. Possible values are `web-beautify' and `prettier'.")
(defvar json-fmt-on-save nil
"Run formatter on buffer save.")

View File

@ -58,9 +58,15 @@
(defun json/pre-init-prettier-js ()
(when (eq json-fmt-tool 'prettier)
(add-to-list 'spacemacs--prettier-modes 'json-mode)
(add-hook 'json-mode-hook #'spacemacs/json-setup-prettier)))
(add-hook 'json-mode-hook #'spacemacs/json-setup-prettier)
(when (eq json-fmt-on-save t)
(add-hook 'json-mode-hook 'prettier-js-mode))))
(defun json/pre-init-web-beautify ()
(when (eq json-fmt-tool 'web-beautify)
(add-to-list 'spacemacs--web-beautify-modes
(cons 'json-mode 'web-beautify-js))))
(cons 'json-mode 'web-beautify-js))
(when (eq json-fmt-on-save t)
(add-hook 'json-mode-hook
(lambda ()
(add-hook 'before-save-hook 'web-beautify-js-buffer t t))))))