Skip to content

Commit

Permalink
Address pre-existing trailing commas when not in the rightmost bracke…
Browse files Browse the repository at this point in the history
…t pair

This required some hackery.  Long story short, we need to reuse the ability to
omit rightmost bracket pairs (which glues them together and splits on something
else instead), for use with pre-existing trailing commas.

This form of user-controlled formatting is brittle so we have to be careful not
to cause a scenario where Black first formats code without trailing commas in
one way, and then looks at the same file with pre-existing trailing commas
(that it itself put on the previous run) and decides to format the code again.

One particular ugly edge case here is handling of optional parentheses.  In
particular, the long-standing `line_length=1` hack got in the way of
pre-existing trailing commas and had to be removed.  Instead, a more
intelligent but costly solution was put in place: a "second opinion" if the
formatting that omits optional parentheses ended up causing lines to be too
long.  Again, for efficiency purposes, Black reuses Leaf objects from blib2to3
and modifies them in place, which was invalid for having two separate
formattings.  Line cloning was used to mitigate this.

Fixes #1619
  • Loading branch information
ambv committed Aug 25, 2020
1 parent d46268c commit 586d242
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 75 deletions.
Loading

0 comments on commit 586d242

Please sign in to comment.