Skip to content

Commit

Permalink
[ref] Refactor string_split(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbugyi200 committed Nov 3, 2019
1 parent 086b208 commit 274715a
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions black.py
Expand Up @@ -2479,7 +2479,7 @@ def str_split(line: Line, features: Collection[Feature]) -> Iterator[Line]:


def string_split(
line: Line, line_length: int, features: Collection[Feature] = ()
line: Line, line_length: int, _features: Collection[Feature] = ()
) -> Iterator[Line]:
"""Split long strings."""
leave_types = [leaf.type for leaf in line.leaves]
Expand All @@ -2490,19 +2490,18 @@ def string_split(
):
raise CannotSplit("This split function only works on strings.")

rest = line
while True:
if is_line_short_enough(rest, line_length=line_length):
rest.append(Leaf(token.COMMA, ","))
yield rest
break
if is_line_short_enough(line, line_length=line_length):
raise CannotSplit("Line is already short enough. No reason to split.")

rest_value = rest.leaves[0].value
if rest_value[0] == "f":
raise CannotSplit("This split function cannot handle f-strings yet.")
if line.leaves[0].value[0] == "f":
raise CannotSplit("This split function cannot handle f-strings yet.")

if re.search('^[a-z]?"""', rest_value):
raise CannotSplit("This split function does not work on multiline strings.")
if re.search('^[a-z]?"""', line.leaves[0].value):
raise CannotSplit("This split function does not work on multiline strings.")

rest = line
while not is_line_short_enough(rest, line_length=line_length):
rest_value = rest.leaves[0].value

idx = line_length - 2 - (line.depth * 4)
while 0 <= idx < len(rest_value) and rest_value[idx] != " ":
Expand All @@ -2519,6 +2518,9 @@ def string_split(

yield result

rest.append(Leaf(token.COMMA, ","))
yield rest


def left_hand_split(line: Line, features: Collection[Feature] = ()) -> Iterator[Line]:
"""Split line into many lines, starting with the first matching bracket pair.
Expand Down

0 comments on commit 274715a

Please sign in to comment.