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

Function generate_ignored_nodes

src/black/comments.py:501–547  ·  view source on GitHub ↗

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
)

Source from the content-addressed store, hash-verified

499
500
501def 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
550def _find_compound_statement_context(parent: Node) -> Node | None:

Callers 1

convert_one_fmt_off_pairFunction · 0.85

Calls 5

container_ofFunction · 0.90
contains_fmt_directiveFunction · 0.85
is_fmt_onFunction · 0.85
children_contains_fmt_onFunction · 0.85

Tested by

no test coverage detected