diff --git a/black.py b/black.py index b3bb4440aab..1823cf8eee5 100644 --- a/black.py +++ b/black.py @@ -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) @@ -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) @@ -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) @@ -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]]] = {}