Skip to content

Commit

Permalink
hmmm
Browse files Browse the repository at this point in the history
Signed-off-by: cobalt <[email protected]>
  • Loading branch information
cobaltt7 committed Dec 23, 2024
1 parent a13dcd7 commit 71fed49
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,25 @@ def _prefer_split_rhs_oop_over_rhs(
):
return True

# the split is right after `=`
if not (len(rhs.head.leaves) >= 2 and rhs.head.leaves[-2].type == token.EQUAL):
return True

# the left side of assignment contains brackets
if not any(leaf.type in BRACKETS for leaf in rhs.head.leaves[:-1]):
return True

# the left side of assignment is short enough (the -1 is for the ending optional
# paren)
if not is_line_short_enough(
rhs.head, mode=replace(mode, line_length=mode.line_length - 1)
):
return True

# the left side of assignment won't explode further because of magic trailing comma
if rhs.head.magic_trailing_comma is not None:
return True

# If we have multiple targets, we prefer more `=`s on the head vs pushing them to
# the body
rhs_head_equal_count = [leaf.type for leaf in rhs.head.leaves].count(token.EQUAL)
Expand All @@ -1037,20 +1056,18 @@ def _prefer_split_rhs_oop_over_rhs(
if rhs_head_equal_count > 1 and rhs_head_equal_count > rhs_oop_head_equal_count:
return False

if not is_line_short_enough(
# the -1 is for the ending optional paren
rhs.head,
mode=replace(mode, line_length=mode.line_length - 1),
# the split is actually from inside the optional parens (done by checking the first
# line still contains the `=`)
if (
rhs_head_equal_count > 1
and rhs_oop_head_equal_count > 1
# and the first line is short enough
and is_line_short_enough(rhs_oop.head, mode=mode)
):
return False

# Remove optional parens when the head contains brackets
if any(leaf.type in BRACKETS for leaf in rhs.head.leaves[:-1]):
return True
if rhs.head.magic_trailing_comma is not None:
return False

# contains matching brackets after the `=` (done by checking there is a closing bracket)
# contains matching brackets after the `=` (done by checking there is a closing
# bracket)
has_closing_bracket_after_assign = False
for leaf in reversed(rhs_oop.head.leaves):
if leaf.type == token.EQUAL:
Expand All @@ -1061,14 +1078,6 @@ def _prefer_split_rhs_oop_over_rhs(
if has_closing_bracket_after_assign:
return True

# the split is actually from inside the optional parens (done by checking the first line still contains the `=`)
if (
any(leaf.type == token.EQUAL for leaf in rhs_oop.head.leaves)
# the first line is short enough
and is_line_short_enough(rhs_oop.head, mode=mode)
):
return True

return False


Expand Down

0 comments on commit 71fed49

Please sign in to comment.