Adding format on save to javascript layer.

Adding formatting on save option to javascript layer, similar to typescript
layer. One small syntax error fix in typescript layer.
This commit is contained in:
Trapez Breen 2019-04-24 14:03:27 +02:00 committed by duianto
parent 785c155ca1
commit 9665670f8e
6 changed files with 36 additions and 2 deletions

View file

@ -1546,6 +1546,7 @@ Other:
- Added ES6 module extension to =init-js2-mode= (thanks to Nathan Lloyd) - Added ES6 module extension to =init-js2-mode= (thanks to Nathan Lloyd)
- Added =org-babel= support (thanks to Michael Rohleder) - Added =org-babel= support (thanks to Michael Rohleder)
- Added debug support via =dap= layer (Firefox and Google Chrome) - Added debug support via =dap= layer (Firefox and Google Chrome)
- Added format on save option. (thanks to Trapez Breen)
- Key bindings - Key bindings
- Improved coffeescript support: - Improved coffeescript support:
- ~SPC m '~ to create or go to REPL - ~SPC m '~ to create or go to REPL

View file

@ -118,6 +118,16 @@ 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~. *Note:* you can easily add a directory local variable with ~SPC f v d~.
You can choose formatting tool:
#+BEGIN_SRC elisp
(setq-default dotspacemacs-configuration-layers '(
(javascript :variables
javascript-fmt-tool 'prettier)))
#+END_SRC
Default is =web-beautify=.
* Backends * Backends
** Tern ** Tern
See [[file:../../+tools/tern/README.org][tern layer]] documentation. See [[file:../../+tools/tern/README.org][tern layer]] documentation.

View file

@ -21,3 +21,6 @@
(defvar javascript-import-tool nil (defvar javascript-import-tool nil
"The import backend to import modules. Possible values are `import-js' and `nil' to disable.") "The import backend to import modules. Possible values are `import-js' and `nil' to disable.")
(defvar javascript-fmt-on-save nil
"Run formatter on buffer save.")

View file

@ -130,3 +130,21 @@
(spacemacs/skewer-eval-region beg end) (spacemacs/skewer-eval-region beg end)
(skewer-repl) (skewer-repl)
(evil-insert-state)) (evil-insert-state))
;; Others
(defun spacemacs/javascript-format ()
"Call formatting tool specified in `javascript-fmt-tool'."
(interactive)
(cond
((eq javascript-fmt-tool 'prettier)
(call-interactively 'prettier-js))
((eq javascript-fmt-tool 'web-beautify)
(call-interactively 'web-beautify-js))
(t (error (concat "%s isn't valid javascript-fmt-tool value."
" It should be 'web-beutify or 'prettier.")
(symbol-name javascript-fmt-tool)))))
(defun spacemacs/javascript-fmt-before-save-hook ()
(add-hook 'before-save-hook 'spacemacs/javascript-format t t))

View file

@ -84,6 +84,8 @@
(cons 'javascript-backend value)))) (cons 'javascript-backend value))))
:config :config
(progn (progn
(when javascript-fmt-on-save
(add-hook 'js2-mode-local-vars-hook 'spacemacs/javascript-fmt-before-save-hook))
;; prefixes ;; prefixes
(spacemacs/declare-prefix-for-mode 'js2-mode "mh" "documentation") (spacemacs/declare-prefix-for-mode 'js2-mode "mh" "documentation")
(spacemacs/declare-prefix-for-mode 'js2-mode "mg" "goto") (spacemacs/declare-prefix-for-mode 'js2-mode "mg" "goto")

View file

@ -131,8 +131,8 @@
((eq typescript-fmt-tool 'prettier) ((eq typescript-fmt-tool 'prettier)
(call-interactively 'prettier-js)) (call-interactively 'prettier-js))
(t (error (concat "%s isn't valid typescript-fmt-tool value." (t (error (concat "%s isn't valid typescript-fmt-tool value."
" It should be 'tide, 'typescript-formatter or 'prettier." " It should be 'tide, 'typescript-formatter or 'prettier.")
(symbol-name typescript-fmt-tool)))))) (symbol-name typescript-fmt-tool)))))
(defun spacemacs/typescript-fmt-before-save-hook () (defun spacemacs/typescript-fmt-before-save-hook ()
(add-hook 'before-save-hook 'spacemacs/typescript-format t t)) (add-hook 'before-save-hook 'spacemacs/typescript-format t t))