Skip to content

Commit

Permalink
[ref] Cleanup StringMerger
Browse files Browse the repository at this point in the history
  • Loading branch information
bbugyi200 committed Dec 29, 2019
1 parent f480203 commit e0eec91
Showing 1 changed file with 50 additions and 50 deletions.
100 changes: 50 additions & 50 deletions black.py
Expand Up @@ -2579,7 +2579,6 @@ def _my_regexp(self) -> str:
return r"^[\s\S]*$"

def _do_transform(self, line: Line, _string_idx: Optional[int]) -> Iterator[Line]:
# Merge strings that were split across multiple lines using backslashes.
new_line = self.__remove_backslash_line_continuation_chars(line)

(new_line, line_was_changed) = self.__merge_first_string_group(new_line)
Expand All @@ -2590,6 +2589,30 @@ def _do_transform(self, line: Line, _string_idx: Optional[int]) -> Iterator[Line

yield new_line

@staticmethod
def __remove_backslash_line_continuation_chars(line: Line) -> Line:
"""Merge strings that were split across multiple lines using backslashes."""
for leaf in line.leaves:
if (
leaf.type == token.STRING
and "\\\n" in leaf.value
and leaf.value.lstrip(STRING_PREFIX_CHARS)[:3] not in {'"""', "'''"}
):
break
else:
return line

new_line = line.clone()
new_line.comments = line.comments
append_leaves(new_line, line, line.leaves)
for leaf in new_line.leaves:
if leaf.type == token.STRING and leaf.value.lstrip(STRING_PREFIX_CHARS)[
:3
] not in {'"""', "'''"}:
leaf.value = leaf.value.replace("\\\n", "")

return new_line

def __merge_first_string_group(self, line: Line) -> Tuple[Line, bool]:
first_str_idx = self.__get_string_group_index(line)

Expand Down Expand Up @@ -2702,55 +2725,6 @@ def __merge_first_string_group(self, line: Line) -> Tuple[Line, bool]:

return (new_line, True)

@staticmethod
def __get_string_group_index(line: Line) -> Optional[int]:
num_of_inline_string_comments = 0
set_of_prefixes = set()
for leaf in line.leaves:
if leaf.type == token.STRING:
prefix = get_string_prefix(leaf.value)
if prefix:
set_of_prefixes.add(prefix)

if id(leaf) in line.comments:
num_of_inline_string_comments += 1

if num_of_inline_string_comments > 1 or len(set_of_prefixes) > 1:
return None

for i, leaf in enumerate(line.leaves):
if (
i + 1 < len(line.leaves)
and leaf.type == token.STRING
and line.leaves[i + 1].type == token.STRING
):
return i

return None

@staticmethod
def __remove_backslash_line_continuation_chars(line: Line) -> Line:
for leaf in line.leaves:
if (
leaf.type == token.STRING
and "\\\n" in leaf.value
and leaf.value.lstrip(STRING_PREFIX_CHARS)[:3] not in {'"""', "'''"}
):
break
else:
return line

new_line = line.clone()
new_line.comments = line.comments
append_leaves(new_line, line, line.leaves)
for leaf in new_line.leaves:
if leaf.type == token.STRING and leaf.value.lstrip(STRING_PREFIX_CHARS)[
:3
] not in {'"""', "'''"}:
leaf.value = leaf.value.replace("\\\n", "")

return new_line

@staticmethod
def __remove_bad_trailing_commas(line: Line) -> Line:
line_str = line_to_string(line)
Expand Down Expand Up @@ -2782,6 +2756,32 @@ def __remove_bad_trailing_commas(line: Line) -> Line:

return new_line

@staticmethod
def __get_string_group_index(line: Line) -> Optional[int]:
num_of_inline_string_comments = 0
set_of_prefixes = set()
for leaf in line.leaves:
if leaf.type == token.STRING:
prefix = get_string_prefix(leaf.value)
if prefix:
set_of_prefixes.add(prefix)

if id(leaf) in line.comments:
num_of_inline_string_comments += 1

if num_of_inline_string_comments > 1 or len(set_of_prefixes) > 1:
return None

for i, leaf in enumerate(line.leaves):
if (
i + 1 < len(line.leaves)
and leaf.type == token.STRING
and line.leaves[i + 1].type == token.STRING
):
return i

return None


class StringSplitterMixin(StringTransformerMixin):
STRING_CHILD_IDX_MAP: ClassVar[Dict[int, Optional[int]]] = {}
Expand Down

0 comments on commit e0eec91

Please sign in to comment.