Open jowens opened 1 year ago
GNU Emacs 28.2 (build 1, aarch64-apple-darwin22.2.0, Carbon Version 169 AppKit 2299.3) of 2023-01-12
(define-package "python-black" "1.2.0" "Reformat Python using python-black" '((emacs "25") (dash "2.16.0") (reformatter "0.3")) :commit "e1bbf574a952562ddeadb0caa42c44016136c2c9" :authors '(("wouter bolsterlee" . "wouter@bolsterl.ee")) :maintainer '("wouter bolsterlee" . "wouter@bolsterl.ee") :keywords '("languages") :url "https://github.com/wbolster/emacs-python-black")
python-black-on-save-mode-enable-dwim
uses heuristics to autodetect if a file should be autoformatted:
site-packages/
directory? ➡ do not formatin your case the autodetection that your project uses black likely doesn't work. having a line like this in pyproject.toml
should make that work:
[tool.black]
note that no further configuration options are needed; the header suffices.
alternatively, you could change the hook to unconditionally enable formatting for all python files:
(use-package python-black
:demand t
:after python
:hook (python-mode . python-black-on-save-mode))
however, that can be annoying, e.g. when opening files in the sites-packages/
directory, etc.
I like the dwim aspect. I think auto-detect is great, and the right thing to do in my case. However, I'm just wanting to use it for one standalone .py file. It doesn't have a "pyproject.toml". It's just one file.
In an alternative to an auxiliary file, might there be a comment I can put at the top of my file that would allow dwim to detect I'd like to use black? Right now the first line is #!/usr/bin/env python3
and I would happily manually add a directive that allow dwim to make the right conclusion.
the dwim command exists only for use in hooks, for the reasons outlined above.
i think you're looking for the normal python-black-on-save-mode
toggle. you can run that manually to toggle autoformatting for single file, regardless of what the dwim hook decided
Sure, I'm just asking if I can add a comment in a python file that dwim will recognize (can dwim be augmented to look for such a comment). I like using dwim! I just don't want to manually toggle every single time I visit that file. I'd rather add a comment once for a file I edit often if dwim doesn't, um, do what I mean.
no, such a magic comment honoured by this package does not exist. not too thrilled about the idea either, the point of black is to not have debatable cruft in source code.
i think it's fundamentally impossible to come up with a ‘dwim’ that works in all cases. if you have concrete ideas on how to improve it, suggestions welcome
maybe something with dir-locals? 🤔
(i never use those myself)
I just don't want to manually toggle every single time I visit that file.
@jowens why would you want to use python-black-on-save-mode-enable-dwim
over python-black-on-save-mode
? If you set the hook to python-black-on-save-mode
, then you will have the formatter on by default for python codes. If you want it on only for a specific file though, then I also don't understand why wouldn't you just create a pyproject.toml
as they recommend it in the black's manual too.
I'm using, as the readme says,
Then I see that:
This is all good, but then when I load up a python file, it isn't in black minor mode:
I can manually call
python-black-on-save-mode
in a Python buffer and everything then works as expected. But why is this not automatic? Baffled. Any tips on how to debug?