Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hartzell opened this issue May 31, 2020 · 6 comments
Closed

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

hartzell opened this issue May 31, 2020 · 6 comments

Comments

@hartzell
Copy link

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
Copy link
Owner

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: purcell/emacs-reformatter#13 (comment)

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
Copy link
Owner

wbolster commented May 31, 2020

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
Copy link
Author

hartzell commented Jun 1, 2020

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
Copy link
Owner

wbolster commented Jun 1, 2020

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

@hartzell
Copy link
Author

hartzell commented Jun 1, 2020

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
Copy link
Owner

wbolster commented Jun 1, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants