Starting from the container of `leaf`, generate all leaves until `# fmt: on`. If comment is skip, returns leaf only. Stops at the end of the block.
(
leaf: Leaf, comment: ProtoComment, mode: Mode
)
| 499 | |
| 500 | |
| 501 | def generate_ignored_nodes( |
| 502 | leaf: Leaf, comment: ProtoComment, mode: Mode |
| 503 | ) -> Iterator[LN]: |
| 504 | """Starting from the container of `leaf`, generate all leaves until `# fmt: on`. |
| 505 | |
| 506 | If comment is skip, returns leaf only. |
| 507 | Stops at the end of the block. |
| 508 | """ |
| 509 | if contains_fmt_directive(comment.value, FMT_SKIP): |
| 510 | yield from _generate_ignored_nodes_from_fmt_skip(leaf, comment, mode) |
| 511 | return |
| 512 | container: LN | None = container_of(leaf) |
| 513 | while container is not None and container.type != token.ENDMARKER: |
| 514 | if is_fmt_on(container, mode=mode): |
| 515 | return |
| 516 | |
| 517 | # fix for fmt: on in children |
| 518 | if children_contains_fmt_on(container, mode=mode): |
| 519 | for index, child in enumerate(container.children): |
| 520 | if isinstance(child, Leaf) and is_fmt_on(child, mode=mode): |
| 521 | if child.type in CLOSING_BRACKETS: |
| 522 | # This means `# fmt: on` is placed at a different bracket level |
| 523 | # than `# fmt: off`. This is an invalid use, but as a courtesy, |
| 524 | # we include this closing bracket in the ignored nodes. |
| 525 | # The alternative is to fail the formatting. |
| 526 | yield child |
| 527 | return |
| 528 | if ( |
| 529 | child.type == token.INDENT |
| 530 | and index < len(container.children) - 1 |
| 531 | and children_contains_fmt_on( |
| 532 | container.children[index + 1], mode=mode |
| 533 | ) |
| 534 | ): |
| 535 | # This means `# fmt: on` is placed right after an indentation |
| 536 | # level, and we shouldn't swallow the previous INDENT token. |
| 537 | return |
| 538 | if children_contains_fmt_on(child, mode=mode): |
| 539 | return |
| 540 | yield child |
| 541 | else: |
| 542 | if container.type == token.DEDENT and container.next_sibling is None: |
| 543 | # This can happen when there is no matching `# fmt: on` comment at the |
| 544 | # same level as `# fmt: on`. We need to keep this DEDENT. |
| 545 | return |
| 546 | yield container |
| 547 | container = container.next_sibling |
| 548 | |
| 549 | |
| 550 | def _find_compound_statement_context(parent: Node) -> Node | None: |
no test coverage detected