Skip to content

Commit e0eec91

Browse files
committed
[ref] Cleanup StringMerger
1 parent f480203 commit e0eec91

File tree

1 file changed

+50
-50
lines changed

1 file changed

+50
-50
lines changed

black.py

+50-50
Original file line numberDiff line numberDiff line change
@@ -2579,7 +2579,6 @@ def _my_regexp(self) -> str:
25792579
return r"^[\s\S]*$"
25802580

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

25852584
(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
25902589

25912590
yield new_line
25922591

2592+
@staticmethod
2593+
def __remove_backslash_line_continuation_chars(line: Line) -> Line:
2594+
"""Merge strings that were split across multiple lines using backslashes."""
2595+
for leaf in line.leaves:
2596+
if (
2597+
leaf.type == token.STRING
2598+
and "\\\n" in leaf.value
2599+
and leaf.value.lstrip(STRING_PREFIX_CHARS)[:3] not in {'"""', "'''"}
2600+
):
2601+
break
2602+
else:
2603+
return line
2604+
2605+
new_line = line.clone()
2606+
new_line.comments = line.comments
2607+
append_leaves(new_line, line, line.leaves)
2608+
for leaf in new_line.leaves:
2609+
if leaf.type == token.STRING and leaf.value.lstrip(STRING_PREFIX_CHARS)[
2610+
:3
2611+
] not in {'"""', "'''"}:
2612+
leaf.value = leaf.value.replace("\\\n", "")
2613+
2614+
return new_line
2615+
25932616
def __merge_first_string_group(self, line: Line) -> Tuple[Line, bool]:
25942617
first_str_idx = self.__get_string_group_index(line)
25952618

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

27032726
return (new_line, True)
27042727

2705-
@staticmethod
2706-
def __get_string_group_index(line: Line) -> Optional[int]:
2707-
num_of_inline_string_comments = 0
2708-
set_of_prefixes = set()
2709-
for leaf in line.leaves:
2710-
if leaf.type == token.STRING:
2711-
prefix = get_string_prefix(leaf.value)
2712-
if prefix:
2713-
set_of_prefixes.add(prefix)
2714-
2715-
if id(leaf) in line.comments:
2716-
num_of_inline_string_comments += 1
2717-
2718-
if num_of_inline_string_comments > 1 or len(set_of_prefixes) > 1:
2719-
return None
2720-
2721-
for i, leaf in enumerate(line.leaves):
2722-
if (
2723-
i + 1 < len(line.leaves)
2724-
and leaf.type == token.STRING
2725-
and line.leaves[i + 1].type == token.STRING
2726-
):
2727-
return i
2728-
2729-
return None
2730-
2731-
@staticmethod
2732-
def __remove_backslash_line_continuation_chars(line: Line) -> Line:
2733-
for leaf in line.leaves:
2734-
if (
2735-
leaf.type == token.STRING
2736-
and "\\\n" in leaf.value
2737-
and leaf.value.lstrip(STRING_PREFIX_CHARS)[:3] not in {'"""', "'''"}
2738-
):
2739-
break
2740-
else:
2741-
return line
2742-
2743-
new_line = line.clone()
2744-
new_line.comments = line.comments
2745-
append_leaves(new_line, line, line.leaves)
2746-
for leaf in new_line.leaves:
2747-
if leaf.type == token.STRING and leaf.value.lstrip(STRING_PREFIX_CHARS)[
2748-
:3
2749-
] not in {'"""', "'''"}:
2750-
leaf.value = leaf.value.replace("\\\n", "")
2751-
2752-
return new_line
2753-
27542728
@staticmethod
27552729
def __remove_bad_trailing_commas(line: Line) -> Line:
27562730
line_str = line_to_string(line)
@@ -2782,6 +2756,32 @@ def __remove_bad_trailing_commas(line: Line) -> Line:
27822756

27832757
return new_line
27842758

2759+
@staticmethod
2760+
def __get_string_group_index(line: Line) -> Optional[int]:
2761+
num_of_inline_string_comments = 0
2762+
set_of_prefixes = set()
2763+
for leaf in line.leaves:
2764+
if leaf.type == token.STRING:
2765+
prefix = get_string_prefix(leaf.value)
2766+
if prefix:
2767+
set_of_prefixes.add(prefix)
2768+
2769+
if id(leaf) in line.comments:
2770+
num_of_inline_string_comments += 1
2771+
2772+
if num_of_inline_string_comments > 1 or len(set_of_prefixes) > 1:
2773+
return None
2774+
2775+
for i, leaf in enumerate(line.leaves):
2776+
if (
2777+
i + 1 < len(line.leaves)
2778+
and leaf.type == token.STRING
2779+
and line.leaves[i + 1].type == token.STRING
2780+
):
2781+
return i
2782+
2783+
return None
2784+
27852785

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

0 commit comments

Comments
 (0)