prettier-emacs
prettier-emacs copied to clipboard
Convenient way to enable/disable per project
I love how little setup this package has, especially how it reads my existing Prettier config, but I'm struggling with finding a good way to disable this on my JavaScript projects that aren't using Prettier. Calling prettier-js-mode
isn't very convenient because it's buffer local (I have to run it on every single buffer I want to disable formatting in). It would be great to have some sort of configuration variable to only enable this in certain projects, automatic enable/disable based on if the current project has a Prettier binary or config, or even a global minor mode. I'm not really sure what's best, so I'd appreciate feedback.
As a temporary workaround, I uninstalled Prettier globally (npm r -g prettier
) after setting up add-node-modules-path
. Now prettier-emacs-mode
will only format buffers if it's in a project with prettier
installed locally. It seems to be working well so far, but I'd still like more fine grained control so I can use prettier-js
on non-project buffers manually without forcing prettier-emacs-mode
to be enabled on all buffers.
I check if .prettierrc
file exists before enabling prettier-mode
(if (locate-dominating-file default-directory ".prettierrc")
(prettier-js-mode +1)
(add-hook 'before-save-hook 'tide-format-before-save))
This seems to work for me, put ((nil . ((eval . (prettier-js-mode 0)))))
in your .dir-locals.el
.
https://emacs.stackexchange.com/a/20340
Nice solution @danielpa9708. FTR, I ended up with:
(defun maybe-use-prettier ()
"Enable prettier-js-mode if an rc file is located."
(if (locate-dominating-file default-directory ".prettierrc")
(prettier-js-mode +1)))
(add-hook 'typescript-mode-hook 'maybe-use-prettier)
(add-hook 'js2-mode-hook 'maybe-use-prettier)