(
self, line: Line, string_and_rpar_indices: list[int]
)
| 1030 | ) |
| 1031 | |
| 1032 | def _transform_to_new_line( |
| 1033 | self, line: Line, string_and_rpar_indices: list[int] |
| 1034 | ) -> Line: |
| 1035 | LL = line.leaves |
| 1036 | |
| 1037 | new_line = line.clone() |
| 1038 | new_line.comments = line.comments.copy() |
| 1039 | |
| 1040 | previous_idx = -1 |
| 1041 | # We need to sort the indices, since string_idx and its matching |
| 1042 | # rpar_idx may not come in order, e.g. in |
| 1043 | # `("outer" % ("inner".join(items)))`, the "inner" string's |
| 1044 | # string_idx is smaller than "outer" string's rpar_idx. |
| 1045 | for idx in sorted(string_and_rpar_indices): |
| 1046 | leaf = LL[idx] |
| 1047 | lpar_or_rpar_idx = idx - 1 if leaf.type == token.STRING else idx |
| 1048 | append_leaves(new_line, line, LL[previous_idx + 1 : lpar_or_rpar_idx]) |
| 1049 | if leaf.type == token.STRING: |
| 1050 | string_leaf = Leaf(token.STRING, LL[idx].value) |
| 1051 | LL[lpar_or_rpar_idx].remove() # Remove lpar. |
| 1052 | replace_child(LL[idx], string_leaf) |
| 1053 | new_line.append(string_leaf) |
| 1054 | # replace comments |
| 1055 | old_comments = new_line.comments.pop(id(LL[idx]), []) |
| 1056 | new_line.comments.setdefault(id(string_leaf), []).extend(old_comments) |
| 1057 | else: |
| 1058 | LL[lpar_or_rpar_idx].remove() # This is a rpar. |
| 1059 | |
| 1060 | previous_idx = idx |
| 1061 | |
| 1062 | # Append the leaves after the last idx: |
| 1063 | append_leaves(new_line, line, LL[idx + 1 :]) |
| 1064 | |
| 1065 | return new_line |
| 1066 | |
| 1067 | |
| 1068 | class BaseStringSplitter(StringTransformer): |
no test coverage detected