jscheid / prettier.el

Prettier code formatting for Emacs.
GNU General Public License v3.0
167 stars 12 forks source link

Need to document suggestion(s) for per-project configuration #17

Closed aspiers closed 4 years ago

aspiers commented 4 years ago

I work on many projects, only some of which use prettier. So enabling this mode globally, or per mode with specific hooks like js-mode-hook, are both unsuitable for me. I suspect I am in the majority rather than the minority here, or at least there will be a large number of people in this situation. Therefore it would be good to recommend one or more ways of enabling this mode per project. I think this should be doable using Directory Variables, and I'm currently trying to figure out how.

jscheid commented 4 years ago

That couldn't hurt. Perhaps you could send a PR against the documentation?

You should be able to simply set (prettier-mode t) in .dir-locals.el, but I haven't tested it myself recently.

You could also think about using prettier-mode-ignore-buffer-function for this purpose.

aspiers commented 4 years ago

Yes, I just tried .dir-locals.el and it worked:

((js-mode . ((eval . (prettier-mode)))))

However, the project I am currently working on (organice, if you're curious - it's awesome!) has a maintainer who already has his own way of running prettier, so I can't put .dir-locals.el within that repo otherwise it will pollute git status. Of course I could submit a PR adding .dir-locals.el to .gitignore, but that would prevent it from being used for anything else later on if the project had some other emacs settings which were shareable across all contributors.

But the good news is that (as usual) emacs provides another way of configuring this which doesn't interfere with the project repository: dir-locals-set-class-variables and dir-locals-set-directory-class. I'm currently trying to set this up.

aspiers commented 4 years ago

Got this working now! I define this globally in my emacs config:

(dir-locals-set-class-variables 'prettier-js
                                '((js-mode . ((eval . (prettier-mode t))))))

and then have a per-project setting for organice:

(dir-locals-set-directory-class "/home/adam/.GIT/3rd-party/organice/" 'prettier-js)
aspiers commented 3 years ago

I was checking the docs yesterday for how to do per-project configuration, and only just now was amused to find this and be reminded that I wrote those docs. I guess I'm getting old ;-)

aspiers commented 2 years ago

Addendum: just figured out that unfortunately you can only set one dir-locals class per directory, so it's not possible to combine multiple classes.