black icon indicating copy to clipboard operation
black copied to clipboard

Show trailing space in diff in explicit way

Open abcdenis opened this issue 2 years ago • 5 comments

Hi, I have a project with precommit hook that calls [black --check --diff] and fails commit on non-empty diff. Egor (collegue of mine) suddenly added space after last character in line but diff looks confusing:

-) 
+)

I could see the source by checkout his branch and running black over it but this is a long way. Could black show trailing spaces in some explicit way? It will be appreciated even by specifying some CLI option. Thank you.

abcdenis avatar May 25 '22 13:05 abcdenis

Hmm, this seems like a reasonable request, and we could append some character when we see trailing spaces in the diff. Something like

felix-hilden avatar May 25 '22 16:05 felix-hilden

and when I have 5 trailing spaces in original text and 10 trailing spaces after changes then diff will look in the same way as it is now.

I think it is better do create some magic but add Unicode representation of all characters.

abcdenis avatar May 30 '22 08:05 abcdenis

I mean, the diff would look something like this:

-) ↲
+)↲

felix-hilden avatar May 30 '22 08:05 felix-hilden

I'll note that if we accept this it must be disabled by default so tools that call black in a subprocess with --diff can use the diff without needing to manually strip . We'd have to add another flag (or maybe hack it into --color?)

ichard26 avatar Aug 27 '22 19:08 ichard26

True and good catch. But this doesn't seem worthy of a dedicated flag like --highlight-trailing-spaces. Perhaps something like --rich for color + whatever else magic we want to add? 🤷‍♂️

felix-hilden avatar Aug 28 '22 15:08 felix-hilden