chore: Refactor delimiter_split()
(#4257)
Signed-off-by: RedGuy12 <61329810+RedGuy12@users.noreply.github.com>
This commit is contained in:
parent
899002399a
commit
0f18001abf
@ -1135,6 +1135,14 @@ def _get_last_non_comment_leaf(line: Line) -> Optional[int]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _can_add_trailing_comma(leaf: Leaf, features: Collection[Feature]) -> bool:
|
||||||
|
if is_vararg(leaf, within={syms.typedargslist}):
|
||||||
|
return Feature.TRAILING_COMMA_IN_DEF in features
|
||||||
|
if is_vararg(leaf, within={syms.arglist, syms.argument}):
|
||||||
|
return Feature.TRAILING_COMMA_IN_CALL in features
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def _safe_add_trailing_comma(safe: bool, delimiter_priority: int, line: Line) -> Line:
|
def _safe_add_trailing_comma(safe: bool, delimiter_priority: int, line: Line) -> Line:
|
||||||
if (
|
if (
|
||||||
safe
|
safe
|
||||||
@ -1156,10 +1164,9 @@ def delimiter_split(
|
|||||||
If the appropriate Features are given, the split will add trailing commas
|
If the appropriate Features are given, the split will add trailing commas
|
||||||
also in function signatures and calls that contain `*` and `**`.
|
also in function signatures and calls that contain `*` and `**`.
|
||||||
"""
|
"""
|
||||||
try:
|
if len(line.leaves) == 0:
|
||||||
last_leaf = line.leaves[-1]
|
|
||||||
except IndexError:
|
|
||||||
raise CannotSplit("Line empty") from None
|
raise CannotSplit("Line empty") from None
|
||||||
|
last_leaf = line.leaves[-1]
|
||||||
|
|
||||||
bt = line.bracket_tracker
|
bt = line.bracket_tracker
|
||||||
try:
|
try:
|
||||||
@ -1167,9 +1174,11 @@ def delimiter_split(
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
raise CannotSplit("No delimiters found") from None
|
raise CannotSplit("No delimiters found") from None
|
||||||
|
|
||||||
if delimiter_priority == DOT_PRIORITY:
|
if (
|
||||||
if bt.delimiter_count_with_priority(delimiter_priority) == 1:
|
delimiter_priority == DOT_PRIORITY
|
||||||
raise CannotSplit("Splitting a single attribute from its owner looks wrong")
|
and bt.delimiter_count_with_priority(delimiter_priority) == 1
|
||||||
|
):
|
||||||
|
raise CannotSplit("Splitting a single attribute from its owner looks wrong")
|
||||||
|
|
||||||
current_line = Line(
|
current_line = Line(
|
||||||
mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
|
mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
|
||||||
@ -1198,15 +1207,8 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
|
|||||||
yield from append_to_line(comment_after)
|
yield from append_to_line(comment_after)
|
||||||
|
|
||||||
lowest_depth = min(lowest_depth, leaf.bracket_depth)
|
lowest_depth = min(lowest_depth, leaf.bracket_depth)
|
||||||
if leaf.bracket_depth == lowest_depth:
|
if trailing_comma_safe and leaf.bracket_depth == lowest_depth:
|
||||||
if is_vararg(leaf, within={syms.typedargslist}):
|
trailing_comma_safe = _can_add_trailing_comma(leaf, features)
|
||||||
trailing_comma_safe = (
|
|
||||||
trailing_comma_safe and Feature.TRAILING_COMMA_IN_DEF in features
|
|
||||||
)
|
|
||||||
elif is_vararg(leaf, within={syms.arglist, syms.argument}):
|
|
||||||
trailing_comma_safe = (
|
|
||||||
trailing_comma_safe and Feature.TRAILING_COMMA_IN_CALL in features
|
|
||||||
)
|
|
||||||
|
|
||||||
if last_leaf.type == STANDALONE_COMMENT and leaf_idx == last_non_comment_leaf:
|
if last_leaf.type == STANDALONE_COMMENT and leaf_idx == last_non_comment_leaf:
|
||||||
current_line = _safe_add_trailing_comma(
|
current_line = _safe_add_trailing_comma(
|
||||||
@ -1220,6 +1222,7 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
|
|||||||
current_line = Line(
|
current_line = Line(
|
||||||
mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
|
mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
|
||||||
)
|
)
|
||||||
|
|
||||||
if current_line:
|
if current_line:
|
||||||
current_line = _safe_add_trailing_comma(
|
current_line = _safe_add_trailing_comma(
|
||||||
trailing_comma_safe, delimiter_priority, current_line
|
trailing_comma_safe, delimiter_priority, current_line
|
||||||
|
Loading…
Reference in New Issue
Block a user