wbolster / emacs-python-black

Emacs package to reformat Python using black-macchiato
BSD 3-Clause "New" or "Revised" License
95 stars 11 forks source link

Is there a way to NOT format certain files/buffers? #5

Closed hartzell closed 4 years ago

hartzell commented 4 years ago

I have a project that's mostly under my control and I format all the things with black.

But, it has a subtree that is legacy code that has been copied in from another location. I'd like keep it in it's original formatting so as to minimize differences from it's upstream. Black has ways to ignore files/directories, but since python-black is running content through black via standard in, those exclusions don't seem to apply.

I'm not sure if there's something that I need to do in the python-mode universe or here in python-black, or in reformatter.el.

Any suggestions?

THANKS

wbolster commented 4 years ago

i understand the issue, but i'm not sure the emacs package can do something about this.

to use black's exclusion configuration, that would need to live on the python side of the fence.

if black would take a filename that could work, sth like --stdin-filename which apparently exists for other formatters/linters for exactly the same reason; see recent remarks about this over here: https://github.com/purcell/reformatter.el/issues/13#issuecomment-634662838

so i guess black is the right place to request this feature; please cc me if you open one. (maybe it exists? didn't check, using phone now).

alternatively, it could theoretically live inside black-macchiato (i know the maintainer quite well 🙃) but since a) it's not related to partial formatting and b) it likely requires poking into black internals, i'm pretty sure that the maintainer 😜wouldn't be eager to add it there.

wbolster commented 4 years ago

small addition, if black has a flag like that, this package is the right place to add it, reformatter.el lets us pass arbitrary flags, and a filename is easy to add. (see issue i linked above.)

hartzell commented 4 years ago

I'll think about that for a while.

Would it be possible to do something with a file local variable that either python-black or reformatter could notice and return w/out changing anything?

wbolster commented 4 years ago

you could make the enabling of the ...-on-save-mode conditional. how do you currently enable it?

hartzell commented 4 years ago

This is my python setup:

(use-package python
  :mode ("\\.py\\'" . python-mode)
  :interpreter ("python" . python-mode)
  :config
  (use-package lsp-python-ms
    :init (require 'lsp-python-ms))
  (use-package py-isort)
  (use-package python-black)
  ;; (use-package py-yapf)
  :hook (
         (python-mode . lsp-deferred)
         (python-mode . py-isort-enable-on-save)
         ;; (python-mode . py-yapf-enable-on-save)
         (python-mode . python-black-on-save-mode)
         )
  )
wbolster commented 4 years ago

that :hook for the on-save-mode is unconditional in that setup. you could make your own hook which has an if statement in it, and use whatever condition makes sense for your setup.