psf / black

The uncompromising Python code formatter

Home Page:https://black.readthedocs.io/en/stable/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow for partially formatting a file e.g. from line 20 to 25

devsnd opened this issue · comments

Hello,

I just stumbled upon black and I immediately integrated it into my workflow in PyCharm. it's really great tool.

Unfortunately I cannot reformat all of our code at once and make everything consistent, because I would be clobbering our git history and every subsequent git blame would blame me!

So I would like to have a command line switch to only reformat a part of a file, say a range of lines, e.g.

black --from-line 20 --to-line 25

so I can only reformat the code that I'm touching anyway and transform our codebase over time.

@khvn26 But then I have to do pick my hunks manually, and keep in mind which lines I actually changed. It's not the same thing.

This is not going to happen, sorry. It's not generally safe to reformat part of a python source code. See #134 for a full explanation.

Also FYI, I wrote darker to apply Black formatting only to lines which have changed in the Git working tree since the last commit.

one hack:

  1. create a temp.py file
  2. paste your code block
  3. format the temp.py file
  4. cut paste the formatter code in the original file

I understand hesitancy to introduce partial formatting to prevent inconsistent style within files. However, many text editors simply assume partial formatting is a feature of the formatter, so this stance significantly decreases the usability.

My specific use case is a desire is to use Vim to format my code. In many situation, simply calling using the formatter on a paragraph is the simplest way to ident a group of lines, but I'm prevented from doing that when using black. Furthermore, you format an entire file in Vim by selecting the entire file and formatting the selection, so even that isn't possible without support for partial formatting.

Is this still seen as out of the question to consider implementing? I can't help but feel the provided justification isn't satisfactory.

The docs mention a way to use Black from vim without relying on selecting lines. That should get your workflow unblocked immediately

@zsol Thank you very much for the response and the link. However, I'd like to be able to use the Vim keybinding (e.g. =ap) that I'm used to and use in other languages, or even Python projects that don't use black. In my opinion, using black shouldn't block specific (moderately common) dev workflows.

@mkarrmann you may want to try this instead: https://github.com/smbl64/vim-black-macchiato

partial formatting is a dirty hack and should be avoided if possible. black not supporting it seems a done deal, so you're left with hacks to make it work (and accepting the ‘risks’)

@wbolster Thanks! I overlooked your above comment. That's unfortunate to hear (although of course expected, I wasn't expecting my single comment to change a decision made years ago lol). Glad to know I wasn't the only one frustrated by this limitation.

And that's a neat "dirty hack" :) Thanks for sharing!