Return a new line with given `leaves` and respective comments from `original`. If it's the head component, brackets will be tracked so trailing commas are respected. If it's the body component, the result line is one-indented inside brackets and as such has its first leaf's prefix
(
leaves: list[Leaf],
original: Line,
opening_bracket: Leaf,
*,
component: _BracketSplitComponent,
)
| 1267 | |
| 1268 | |
| 1269 | def bracket_split_build_line( |
| 1270 | leaves: list[Leaf], |
| 1271 | original: Line, |
| 1272 | opening_bracket: Leaf, |
| 1273 | *, |
| 1274 | component: _BracketSplitComponent, |
| 1275 | ) -> Line: |
| 1276 | """Return a new line with given `leaves` and respective comments from `original`. |
| 1277 | |
| 1278 | If it's the head component, brackets will be tracked so trailing commas are |
| 1279 | respected. |
| 1280 | |
| 1281 | If it's the body component, the result line is one-indented inside brackets and as |
| 1282 | such has its first leaf's prefix normalized and a trailing comma added when |
| 1283 | expected. |
| 1284 | """ |
| 1285 | result = Line(mode=original.mode, depth=original.depth) |
| 1286 | if component is _BracketSplitComponent.body: |
| 1287 | result.inside_brackets = True |
| 1288 | result.depth += 1 |
| 1289 | if _ensure_trailing_comma(leaves, original, opening_bracket): |
| 1290 | for i in range(len(leaves) - 1, -1, -1): |
| 1291 | if leaves[i].type == STANDALONE_COMMENT: |
| 1292 | continue |
| 1293 | |
| 1294 | if leaves[i].type != token.COMMA: |
| 1295 | new_comma = Leaf(token.COMMA, ",") |
| 1296 | leaves.insert(i + 1, new_comma) |
| 1297 | break |
| 1298 | |
| 1299 | leaves_to_track: set[LeafID] = set() |
| 1300 | if component is _BracketSplitComponent.head: |
| 1301 | leaves_to_track = get_leaves_inside_matching_brackets(leaves) |
| 1302 | # Populate the line |
| 1303 | for leaf in leaves: |
| 1304 | result.append( |
| 1305 | leaf, |
| 1306 | preformatted=True, |
| 1307 | track_bracket=id(leaf) in leaves_to_track, |
| 1308 | ) |
| 1309 | for comment_after in original.comments_after(leaf): |
| 1310 | result.append(comment_after, preformatted=True) |
| 1311 | if component is _BracketSplitComponent.body and should_split_line( |
| 1312 | result, opening_bracket |
| 1313 | ): |
| 1314 | result.should_split_rhs = True |
| 1315 | return result |
| 1316 | |
| 1317 | |
| 1318 | def dont_increase_indentation(split_func: Transformer) -> Transformer: |
no test coverage detected