emacs-format-all-the-code icon indicating copy to clipboard operation
emacs-format-all-the-code copied to clipboard

.dir-locals question

Open fapapa opened this issue 4 years ago • 7 comments

I've got forma-all working really well. Thank you for a great package! I'm a bit of a noob, and I can't figure out how to set format-all-formatters in .dir-locals. Can you point me in the right direction?

fapapa avatar Sep 17 '21 05:09 fapapa

Here's a simple .dir-locals.el file:

((nil
  (format-all-formatters
   ("JavaScript" prettier)
   ("Python" (yapf "--style" "google")))))

It formats all JavaScript files in that directory (and subdirectories) using Prettier with the default command line flags, and all Python files using yapf with the --style google flags. In both cases, if Prettier and yapf support their own configuration files, those are read as well.

(yapf has parentheses around it because it takes command line arguments. Format-All supports chaining multiple formatters for the same language, so the parentheses are needed to distinguish between a new formatter, and a new command line argument for the same formatter.)

lassik avatar Sep 17 '21 07:09 lassik

The nil at the top means that it applies to all file types in that directory; the details of .dir-locals.el are explained in the Emacs manual.

lassik avatar Sep 17 '21 07:09 lassik

Cool. I'll give that a try. I tried so many things, but not that exactly. I thought there had to be a "." after the "nil" for example. I did look at the manual, but it lacks any kind of examples to show the actual syntax. I use .dir-locals to set a couple of other settings, but this was more complicated.

I really appreciate the time you took to give a detailed example. Thank you.

fapapa avatar Sep 17 '21 13:09 fapapa

You're welcome. The file format is confusing even to me. The . is an unfortunate complication in Lisp syntax (the "consing dot") and isn't needed in this case.

lassik avatar Sep 18 '21 06:09 lassik

This is very helpful. It would be even better, if this can be found the REAME ;)

junwei-wang avatar Nov 29 '21 16:11 junwei-wang

Can you make a PR that updates the readme?

lassik avatar Nov 29 '21 17:11 lassik

hey, just a branched question. is there a way to specify the format-all-formatters value globally instead of using dir-locals? I had the following use-package config:

(use-package format-all
  :bind
  (("<f8>" . format-all-buffer)
   ("<f9>" . format-all-mode))
  :custom
  ((format-all-formatters (("Python" black)
                           ("Ruby" rubocop))))
  :delight
  :hook
  ((format-all-mode . format-all-ensure-formatter)
   (prog-mode . format-all-mode)))

but apparently it doesn't set rubocop as the default for all ruby files.

hjpotter92 avatar Apr 20 '22 04:04 hjpotter92