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

Method do_transform

src/black/trans.py:2227–2348  ·  view source on GitHub ↗
(
        self, line: Line, string_indices: list[int]
    )

Source from the content-addressed store, hash-verified

2225 return None
2226
2227 def do_transform(
2228 self, line: Line, string_indices: list[int]
2229 ) -> Iterator[TResult[Line]]:
2230 LL = line.leaves
2231 assert len(string_indices) == 1, (
2232 f"{self.__class__.__name__} should only find one match at a time, found"
2233 f" {len(string_indices)}"
2234 )
2235 string_idx = string_indices[0]
2236
2237 is_valid_index = is_valid_index_factory(LL)
2238 insert_str_child = insert_str_child_factory(LL[string_idx])
2239
2240 comma_idx = -1
2241 ends_with_comma = False
2242 if LL[comma_idx].type == token.COMMA:
2243 ends_with_comma = True
2244
2245 leaves_to_steal_comments_from = [LL[string_idx]]
2246 if ends_with_comma:
2247 leaves_to_steal_comments_from.append(LL[comma_idx])
2248
2249 # --- First Line
2250 first_line = line.clone()
2251 left_leaves = LL[:string_idx]
2252
2253 # We have to remember to account for (possibly invisible) LPAR and RPAR
2254 # leaves that already wrapped the target string. If these leaves do
2255 # exist, we will replace them with our own LPAR and RPAR leaves.
2256 old_parens_exist = False
2257 if left_leaves and left_leaves[-1].type == token.LPAR:
2258 old_parens_exist = True
2259 leaves_to_steal_comments_from.append(left_leaves[-1])
2260 left_leaves.pop()
2261
2262 append_leaves(first_line, line, left_leaves)
2263
2264 lpar_leaf = Leaf(token.LPAR, "(")
2265 if old_parens_exist:
2266 replace_child(LL[string_idx - 1], lpar_leaf)
2267 else:
2268 insert_str_child(lpar_leaf)
2269 first_line.append(lpar_leaf)
2270
2271 # We throw inline comments that were originally to the right of the
2272 # target string to the top line. They will now be shown to the right of
2273 # the LPAR.
2274 for leaf in leaves_to_steal_comments_from:
2275 for comment_leaf in line.comments_after(leaf):
2276 first_line.append(comment_leaf, preformatted=True)
2277
2278 yield Ok(first_line)
2279
2280 # --- Middle (String) Line
2281 # We only need to yield one (possibly too long) string line, since the
2282 # `StringSplitter` will break it down further if necessary.
2283 string_value = LL[string_idx].value
2284 string_line = Line(

Callers

nothing calls this directly

Calls 13

appendMethod · 0.95
append_leavesFunction · 0.90
LeafClass · 0.90
replace_childFunction · 0.90
OkClass · 0.90
LineClass · 0.90
is_valid_index_factoryFunction · 0.85
insert_str_child_factoryFunction · 0.85
insert_str_childFunction · 0.85
is_valid_indexFunction · 0.85
popMethod · 0.80
comments_afterMethod · 0.80

Tested by

no test coverage detected