Fix spurious space after star-based unary expression

This happened when the operand was a complex expression.

Fixes #31
This commit is contained in:
Łukasz Langa 2018-03-15 23:56:30 -07:00
parent 9fea478022
commit 22ce1e1fc3
2 changed files with 6 additions and 15 deletions

View File

@ -804,6 +804,7 @@ def __attrs_post_init__(self) -> None:
OPENING_BRACKETS = set(BRACKET.keys()) OPENING_BRACKETS = set(BRACKET.keys())
CLOSING_BRACKETS = set(BRACKET.values()) CLOSING_BRACKETS = set(BRACKET.values())
BRACKETS = OPENING_BRACKETS | CLOSING_BRACKETS BRACKETS = OPENING_BRACKETS | CLOSING_BRACKETS
ALWAYS_NO_SPACE = CLOSING_BRACKETS | {token.COMMA, token.COLON, STANDALONE_COMMENT}
def whitespace(leaf: Leaf) -> str: def whitespace(leaf: Leaf) -> str:
@ -814,24 +815,12 @@ def whitespace(leaf: Leaf) -> str:
t = leaf.type t = leaf.type
p = leaf.parent p = leaf.parent
v = leaf.value v = leaf.value
if t == token.COLON: if t in ALWAYS_NO_SPACE:
return NO
if t == token.COMMA:
return NO
if t == token.RPAR:
return NO return NO
if t == token.COMMENT: if t == token.COMMENT:
return DOUBLESPACE return DOUBLESPACE
if t == STANDALONE_COMMENT:
return NO
if t in CLOSING_BRACKETS:
return NO
assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}" assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
prev = leaf.prev_sibling prev = leaf.prev_sibling
if not prev: if not prev:
@ -863,7 +852,7 @@ def whitespace(leaf: Leaf) -> str:
if prevp.parent and prevp.parent.type == syms.subscript: if prevp.parent and prevp.parent.type == syms.subscript:
return NO return NO
elif prevp.parent and prevp.parent.type == syms.factor: elif prevp.parent and prevp.parent.type in {syms.factor, syms.star_expr}:
return NO return NO
elif prev.type in OPENING_BRACKETS: elif prev.type in OPENING_BRACKETS:
@ -992,7 +981,7 @@ def whitespace(leaf: Leaf) -> str:
if prev.type == token.DOUBLESTAR: if prev.type == token.DOUBLESTAR:
return NO return NO
elif p.type == syms.factor or p.type == syms.star_expr: elif p.type in {syms.factor, syms.star_expr}:
# unary ops # unary ops
if not prev: if not prev:
prevp = preceding_leaf(p) prevp = preceding_leaf(p)

View File

@ -72,6 +72,7 @@
call(arg, kwarg='hey') call(arg, kwarg='hey')
call(arg, another, kwarg='hey', **kwargs) call(arg, another, kwarg='hey', **kwargs)
call(this_is_a_very_long_variable_which_will_force_a_delimiter_split, arg, another, kwarg='hey', **kwargs) # note: no trailing comma pre-3.6 call(this_is_a_very_long_variable_which_will_force_a_delimiter_split, arg, another, kwarg='hey', **kwargs) # note: no trailing comma pre-3.6
call(*gidgets[:2])
lukasz.langa.pl lukasz.langa.pl
call.me(maybe) call.me(maybe)
1 .real 1 .real
@ -204,6 +205,7 @@ async def f():
kwarg='hey', kwarg='hey',
**kwargs **kwargs
) # note: no trailing comma pre-3.6 ) # note: no trailing comma pre-3.6
call(*gidgets[:2])
lukasz.langa.pl lukasz.langa.pl
call.me(maybe) call.me(maybe)
1 .real 1 .real