From: Ɓukasz Langa Date: Thu, 22 Mar 2018 01:23:46 +0000 (-0700) Subject: Don't omit whitespace when the factor is not a math operator X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/92b377556e24616d5980a9010cf558da7fa35d28?ds=sidebyside;hp=c7c8c4f5018844d69edbc961c922b29f0d6ceeaf Don't omit whitespace when the factor is not a math operator Fixes #55 --- diff --git a/README.md b/README.md index 20ab4be..aba30ab 100644 --- a/README.md +++ b/README.md @@ -293,6 +293,11 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md). ## Change Log +### 18.3a4 (unreleased) + +* don't omit whitespace if the previous factor leaf wasn't a math + operator (#55) + ### 18.3a3 * don't remove single empty lines outside of bracketed expressions diff --git a/black.py b/black.py index 1c07de7..0dd7630 100644 --- a/black.py +++ b/black.py @@ -340,6 +340,7 @@ MATH_OPERATORS = { token.AMPER, token.PERCENT, token.CIRCUMFLEX, + token.TILDE, token.LEFTSHIFT, token.RIGHTSHIFT, token.DOUBLESTAR, @@ -888,7 +889,11 @@ def whitespace(leaf: Leaf) -> str: # noqa C901 if prevp.parent and prevp.parent.type in {syms.subscript, syms.sliceop}: return NO - elif prevp.parent and prevp.parent.type in {syms.factor, syms.star_expr}: + elif ( + prevp.parent + and prevp.parent.type in {syms.factor, syms.star_expr} + and prevp.type in MATH_OPERATORS + ): return NO elif prev.type in OPENING_BRACKETS: diff --git a/tests/expression.py b/tests/expression.py index dbb9303..c18942e 100644 --- a/tests/expression.py +++ b/tests/expression.py @@ -63,6 +63,8 @@ str or None if (1 if True else 2) else str or bytes or None [((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)] {i: 0 for i in (1, 2, 3)} {i: j for i, j in ((1, 'a'), (2, 'b'), (3, 'c'))} +{a: b * 2 for a, b in dictionary.items()} +{a: b * -2 for a, b in dictionary.items()} {k: v for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension} Python3 > Python2 > COBOL Life is Life @@ -214,6 +216,8 @@ str or None if (1 if True else 2) else str or bytes or None [((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)] {i: 0 for i in (1, 2, 3)} {i: j for i, j in ((1, 'a'), (2, 'b'), (3, 'c'))} +{a: b * 2 for a, b in dictionary.items()} +{a: b * -2 for a, b in dictionary.items()} { k: v for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension