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

Method do_transform

src/black/trans.py:1514–1744  ·  src/black/trans.py::StringSplitter.do_transform
(
        self, line: Line, string_indices: list[int]
    )

Source from the content-addressed store, hash-verified

1512 return Ok([string_idx])
1513
1514 def do_transform(
1515 self, line: Line, string_indices: list[int]
1516 ) -> Iterator[TResult[Line]]:
1517 LL = line.leaves
1518 assert len(string_indices) == 1, (
1519 fclass="st">"{self.__class__.__name__} should only find one match at a time, found"
1520 fclass="st">" {len(string_indices)}"
1521 )
1522 string_idx = string_indices[0]
1523
1524 QUOTE = LL[string_idx].value[-1]
1525
1526 is_valid_index = is_valid_index_factory(LL)
1527 insert_str_child = insert_str_child_factory(LL[string_idx])
1528
1529 prefix = get_string_prefix(LL[string_idx].value).lower()
1530
1531 class="cm"># We MAY choose to drop the class="st">'f' prefix from substrings that don't
1532 class="cm"># contain any f-expressions, but ONLY if the original f-string
1533 class="cm"># contains at least one f-expression. Otherwise, we will alter the AST
1534 class="cm"># of the program.
1535 drop_pointless_f_prefix = (class="st">"f" in prefix) and fstring_contains_expr(
1536 LL[string_idx].value
1537 )
1538
1539 first_string_line = True
1540
1541 string_op_leaves = self._get_string_operator_leaves(LL)
1542 string_op_leaves_length = (
1543 sum(len(str(prefix_leaf)) for prefix_leaf in string_op_leaves) + 1
1544 if string_op_leaves
1545 else 0
1546 )
1547
1548 def maybe_append_string_operators(new_line: Line) -> None:
1549 class="st">"""
1550 Side Effects:
1551 If @line starts with a string operator and this is the first
1552 line we are constructing, this function appends the string
1553 operator to @new_line and replaces the old string operator leaf
1554 in the node structure. Otherwise this function does nothing.
1555 class="st">"""
1556 maybe_prefix_leaves = string_op_leaves if first_string_line else []
1557 for i, prefix_leaf in enumerate(maybe_prefix_leaves):
1558 replace_child(LL[i], prefix_leaf)
1559 new_line.append(prefix_leaf)
1560
1561 ends_with_comma = (
1562 is_valid_index(string_idx + 1) and LL[string_idx + 1].type == token.COMMA
1563 )
1564
1565 def max_last_string_column() -> int:
1566 class="st">"""
1567 Returns:
1568 The max allowed width of the string value used for the last
1569 line we will construct. Note that this value means the width
1570 rather than the number of characters (e.g., many East Asian
1571 characters expand to two columns).

Callers

nothing calls this directly

Calls 15

_get_break_idxMethod · 0.95
_normalize_f_stringMethod · 0.95
get_string_prefixFunction · 0.90
count_chars_in_widthFunction · 0.90
LeafClass · 0.90
OkClass · 0.90
str_widthFunction · 0.90
append_leavesFunction · 0.90
is_valid_index_factoryFunction · 0.85
insert_str_child_factoryFunction · 0.85

Tested by

no test coverage detected