Skip to content

Commit

Permalink
[ref] Make 'normalize_strings' an argument to '__merge_first_string_g…
Browse files Browse the repository at this point in the history
…roup(...)'
  • Loading branch information
bbugyi200 committed Dec 29, 2019
1 parent f480203 commit 2a93aa3
Showing 1 changed file with 65 additions and 59 deletions.
124 changes: 65 additions & 59 deletions black.py
Expand Up @@ -2579,18 +2579,78 @@ 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)
(new_line, line_was_changed) = self.__merge_first_string_group(
new_line, self.normalize_strings
)
while line_was_changed:
(new_line, line_was_changed) = self.__merge_first_string_group(new_line)
(new_line, line_was_changed) = self.__merge_first_string_group(
new_line, self.normalize_strings
)

new_line = self.__remove_bad_trailing_commas(new_line)

yield new_line

def __merge_first_string_group(self, line: Line) -> Tuple[Line, bool]:
@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

@staticmethod
def __remove_bad_trailing_commas(line: Line) -> Line:
line_str = line_to_string(line)
if not re.match(r"^[A-Za-z0-9_]+\(\(?" + STRING_REGEXP + r"\)?,\)", line_str):
return line

already_seen_lpar = False
skip_next_rpar = False

new_line = line.clone()
for old_leaf in line.leaves:
if old_leaf.type == token.COMMA:
continue

if already_seen_lpar and old_leaf.type == token.LPAR:
skip_next_rpar = True
continue

if old_leaf.type == token.LPAR:
already_seen_lpar = True

if skip_next_rpar and old_leaf.type == token.RPAR:
skip_next_rpar = False
continue

new_leaf = Leaf(old_leaf.type, old_leaf.value)
replace_child(old_leaf, new_leaf)
new_line.append(new_leaf)

return new_line

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

if first_str_idx is None:
Expand Down Expand Up @@ -2655,7 +2715,7 @@ def __merge_first_string_group(self, line: Line) -> Tuple[Line, bool]:
return (line, False)

temp_string_leaf = Leaf(token.STRING, string_value)
if self.normalize_strings:
if normalize_strings:
normalize_string_quotes(temp_string_leaf)

naked_string_value = temp_string_leaf.value[len(prefix) + 1 : -1]
Expand Down Expand Up @@ -2728,60 +2788,6 @@ def __get_string_group_index(line: Line) -> Optional[int]:

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)
if not re.match(r"^[A-Za-z0-9_]+\(\(?" + STRING_REGEXP + r"\)?,\)", line_str):
return line

already_seen_lpar = False
skip_next_rpar = False

new_line = line.clone()
for old_leaf in line.leaves:
if old_leaf.type == token.COMMA:
continue

if already_seen_lpar and old_leaf.type == token.LPAR:
skip_next_rpar = True
continue

if old_leaf.type == token.LPAR:
already_seen_lpar = True

if skip_next_rpar and old_leaf.type == token.RPAR:
skip_next_rpar = False
continue

new_leaf = Leaf(old_leaf.type, old_leaf.value)
replace_child(old_leaf, new_leaf)
new_line.append(new_leaf)

return new_line


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

0 comments on commit 2a93aa3

Please sign in to comment.