psf / black

The uncompromising Python code formatter
https://black.readthedocs.io/en/stable/
MIT License
38.82k stars 2.45k forks source link

Remove trailing whitespace #1114

Closed pylang closed 3 years ago

pylang commented 4 years ago

Is your feature request related to a problem?

Running black (in VSCode) does not remove trailing whitespaces.

s = 'Hello world!"_ _

Describe the solution you'd like

Rather, I'd like black to remove trailing whitespace on all lines.

s = 'Hello world!"

Describe alternatives you've considered

I have to do a followup "Trim Whitespace" command in VSCode. I'd like this to be part of black by default.

There are some prior issues (#567) regarding trailing whitespace inside strings. Removing trailing whitespace by default can clean up code and even save some wasted space.

msgongora commented 4 years ago

this can be fixed using vscode feature instead:

settings.json

"[python]": {
        "files.trimTrailingWhitespace": true,
    },
...
ichard26 commented 4 years ago

Black already does this.

richard-26@ubuntu-laptop:~/programming/tools/ghprcl$ black test.py
reformatted test.py
All done! ✨ 🍰 ✨
1 file reformatted.
richard-26@ubuntu-laptop:~/programming/tools/ghprcl$ diff -u --color orig.py test.py
--- orig.py 2020-07-28 17:45:02.959401063 -0400
+++ test.py 2020-07-28 17:44:46.307273954 -0400
@@ -1 +1 @@
-s = "Hello world!"  
+s = "Hello world!"

BEFORE

Screenshot from 2020-07-28 17-57-26

and AFTER

Screenshot from 2020-07-28 17-58-15

This seems like a problem with how Visual Studio Code handling of diffs. Maybe related to this?


Environment:

JelleZijlstra commented 3 years ago

Closing as this is most likely an issue with VSCode, not Black.

rjurney commented 9 months ago

Try these in <project_root>/.vscode/settings.json:

{
    "python.formatting.provider": "black",
    "[python]": {
        "files.trimTrailingWhitespace": true,
        "editor.formatOnSave": true
    }
}