Skip to content

Commit 478b5eb

Browse files
committed
[ref] Move get_first_unmatched_rpar_idx(...) into StringStripperMixin class
1 parent 1f87d49 commit 478b5eb

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

black.py

+28-18
Original file line numberDiff line numberDiff line change
@@ -2870,26 +2870,36 @@ def __validate_mfsg(line: Line, string_idx: int) -> STResult[None]:
28702870
return None
28712871

28722872

2873-
def get_first_unmatched_rpar_idx(leaves: List[Leaf]) -> Result[int, ValueError]:
2874-
unmatched_parens = 0
2875-
for (i, leaf) in enumerate(leaves):
2876-
if leaf.type == token.LPAR:
2877-
unmatched_parens += 1
2878-
continue
2873+
class StringStripperMixin(StringTransformerMixin):
2874+
@abstractmethod
2875+
def _do_match(self, line: Line) -> STResult[str]:
2876+
pass
2877+
2878+
@abstractmethod
2879+
def _do_transform(self, line: Line, string_idx: int) -> Iterator[STResult[Line]]:
2880+
pass
28792881

2880-
if leaf.type == token.RPAR and unmatched_parens == 0:
2881-
return i
2882+
@staticmethod
2883+
def get_first_unmatched_rpar_idx(leaves: List[Leaf]) -> Result[int, ValueError]:
2884+
unmatched_parens = 0
2885+
for (i, leaf) in enumerate(leaves):
2886+
if leaf.type == token.LPAR:
2887+
unmatched_parens += 1
2888+
continue
28822889

2883-
if leaf.type == token.RPAR:
2884-
if unmatched_parens > 0:
2885-
unmatched_parens -= 1
2886-
else:
2887-
return ValueError("Found RPAR that matches LPAR from the past!")
2890+
if leaf.type == token.RPAR and unmatched_parens == 0:
2891+
return i
2892+
2893+
if leaf.type == token.RPAR:
2894+
if unmatched_parens > 0:
2895+
unmatched_parens -= 1
2896+
else:
2897+
return ValueError("Found RPAR that matches LPAR from the past!")
28882898

2889-
return ValueError("No RPAR found!")
2899+
return ValueError("No RPAR found!")
28902900

28912901

2892-
class StringArgCommaStripper(StringTransformerMixin):
2902+
class StringArgCommaStripper(StringStripperMixin):
28932903
def _do_match(self, line: Line) -> STResult[str]:
28942904
regex_result = self._regex_match(
28952905
line,
@@ -2948,7 +2958,7 @@ def _do_transform(self, line: Line, string_idx: int) -> Iterator[STResult[Line]]
29482958
new_line = line.clone()
29492959
new_line.comments = line.comments.copy()
29502960

2951-
idx_result = get_first_unmatched_rpar_idx(line.leaves[string_idx + 2 :])
2961+
idx_result = self.get_first_unmatched_rpar_idx(line.leaves[string_idx + 2 :])
29522962
if isinstance(idx_result, ValueError):
29532963
raise RuntimeError(
29542964
f"Logic Error. {self.__class__.__name__} was unable to find the ending"
@@ -2975,7 +2985,7 @@ def _do_transform(self, line: Line, string_idx: int) -> Iterator[STResult[Line]]
29752985
yield new_line
29762986

29772987

2978-
class StringParensStripper(StringTransformerMixin):
2988+
class StringParensStripper(StringStripperMixin):
29792989
def _do_match(self, line: Line) -> STResult[str]:
29802990
regex_result = self._regex_match(
29812991
line,
@@ -3028,7 +3038,7 @@ def _do_match(self, line: Line) -> STResult[str]:
30283038
return string_value
30293039

30303040
def _do_transform(self, line: Line, string_idx: int) -> Iterator[STResult[Line]]:
3031-
idx_result = get_first_unmatched_rpar_idx(line.leaves[string_idx + 1 :])
3041+
idx_result = self.get_first_unmatched_rpar_idx(line.leaves[string_idx + 1 :])
30323042
if isinstance(idx_result, ValueError):
30333043
raise RuntimeError(
30343044
f"Logic Error. {self.__class__.__name__} was unable to find the ending"

0 commit comments

Comments
 (0)