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 53b9269 commit 8592d46
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions black.py
Expand Up @@ -2479,33 +2479,32 @@ 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 = [t for t in [leaf.type for leaf in line.leaves]]
leave_types = [leaf.type for leaf in line.leaves]
if (
len(leave_types) != 2
or leave_types[0] != token.STRING
or leave_types[1] != token.COMMA
):
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 rest_value[idx] != " ":
while 0 <= idx < len(rest_value) and rest_value[idx] != " ":
idx -= 1

result = Line(depth=line.depth)
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 8592d46

Please sign in to comment.