prettier-emacs icon indicating copy to clipboard operation
prettier-emacs copied to clipboard

Convenient way to enable/disable per project

Open nickserv opened this issue 6 years ago • 4 comments

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.

nickserv avatar Feb 28 '18 17:02 nickserv

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.

nickserv avatar Mar 01 '18 15:03 nickserv

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))

danielpza avatar May 08 '18 17:05 danielpza

This seems to work for me, put ((nil . ((eval . (prettier-js-mode 0))))) in your .dir-locals.el. https://emacs.stackexchange.com/a/20340

rnons avatar Dec 06 '18 06:12 rnons

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)

gamb avatar Jan 19 '19 16:01 gamb