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

Run black in specific project directory #13

Open ayys opened 1 year ago

ayys commented 1 year ago

I want to run black in the root dir of projectile project. I am trying to do this because right now, black will not pick up the pyproject.toml configurations.

wbolster commented 1 year ago

i think you're jumping to conclusions too quickly 🙃

i think your problem is not caused by this emacs package because black already takes care of what your describe:

https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#where-black-looks-for-the-file

ayys commented 1 year ago

@wbolster thanks for responding. I don't think this is an invalid issue. From reformatter docs,

When called, the reformatter will use PROGRAM and any ARGS to reformat the current buffer. The contents of the buffer will be passed as standard input to the reformatter, which should output them to standard output.

This means black is parsing stdin. So, from black docs,

If you’re formatting standard input, Black will look for configuration starting from the current working directory.

But since the configuration is not in the cwd, black doesn't properly use pyproject.toml.

wbolster commented 1 year ago

black also has a --stdin-filename flag:

  --stdin-filename TEXT           The name of the file when passing it through
                                  stdin. Useful to make sure Black will
                                  respect --force-exclude option on some
                                  editors that rely on using stdin.

it seems this file is also used to find the project root, as this call to find_project_root() shows: https://github.com/psf/black/blob/d4a85643a465f5fae2113d07d22d021d4af4795a/src/black/__init__.py#L469

wbolster commented 1 year ago

@ayys can you try out #14 please?

wbolster commented 1 year ago

i'm not actually sure it's needed though. i read this:

If you’re formatting standard input, Black will look for configuration starting from the current working directory.

… as ‘will start looking from the current working directory upwards’, which is the desired behaviour