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

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

Closed
devsnd opened this issue Sep 13, 2018 · 11 comments
Closed

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

devsnd opened this issue Sep 13, 2018 · 11 comments

Comments

@devsnd
Copy link

devsnd commented Sep 13, 2018

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

khvn26 commented Sep 13, 2018

Try git add --patch.

@devsnd
Copy link
Author

devsnd commented Sep 13, 2018

@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.

@zsol
Copy link
Collaborator

zsol commented Sep 13, 2018

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.

@zsol zsol closed this as completed Sep 13, 2018
@wbolster
Copy link
Contributor

wbolster commented Feb 6, 2019

@akaihola
Copy link

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

@vikram-ray
Copy link

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

@mkarrmann
Copy link

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.

@zsol
Copy link
Collaborator

zsol commented Sep 27, 2022

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

@mkarrmann
Copy link

mkarrmann commented Sep 27, 2022

@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.

@wbolster
Copy link
Contributor

@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’)

@mkarrmann
Copy link

@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!

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

7 participants