MCPcopy
hub / github.com/psf/black / do_splitter_match

Method do_splitter_match

src/black/trans.py:1985–2026  ·  view source on GitHub ↗
(self, line: Line)

Source from the content-addressed store, hash-verified

1983 """
1984
1985 def do_splitter_match(self, line: Line) -> TMatchResult:
1986 LL = line.leaves
1987
1988 if line.leaves[-1].type in OPENING_BRACKETS:
1989 return TErr(
1990 "Cannot wrap parens around a line that ends in an opening bracket."
1991 )
1992
1993 string_idx = (
1994 self._return_match(LL)
1995 or self._else_match(LL)
1996 or self._assert_match(LL)
1997 or self._assign_match(LL)
1998 or self._dict_or_lambda_match(LL)
1999 )
2000
2001 if string_idx is None:
2002 string_idx = self._trailing_comma_tuple_match(line)
2003
2004 if string_idx is None:
2005 string_idx = self._prefer_paren_wrap_match(LL)
2006
2007 if string_idx is not None:
2008 string_value = line.leaves[string_idx].value
2009 # If the string has neither spaces nor East Asian stops...
2010 if not any(
2011 char == " " or char in SPLIT_SAFE_CHARS for char in string_value
2012 ):
2013 # And will still violate the line length limit when split...
2014 max_string_width = self.line_length - ((line.depth + 1) * 4)
2015 if str_width(string_value) > max_string_width:
2016 # And has no associated custom splits...
2017 if not self.has_custom_splits(string_value):
2018 # Then we should NOT put this string on its own line.
2019 return TErr(
2020 "We do not wrap long strings in parentheses when the"
2021 " resultant line would still be over the specified line"
2022 " length and can't be split further by StringSplitter."
2023 )
2024 return Ok([string_idx])
2025
2026 return TErr("This line does not contain any non-atomic strings.")
2027
2028 @staticmethod
2029 def _return_match(LL: list[Leaf]) -> int | None:

Callers

nothing calls this directly

Calls 11

_return_matchMethod · 0.95
_else_matchMethod · 0.95
_assert_matchMethod · 0.95
_assign_matchMethod · 0.95
_dict_or_lambda_matchMethod · 0.95
str_widthFunction · 0.90
OkClass · 0.90
TErrFunction · 0.85
has_custom_splitsMethod · 0.80

Tested by

no test coverage detected