Handle fmt blocks with actual AST nodes.
(
ignored_nodes: list[LN],
comment: ProtoComment,
previous_consumed: int,
is_fmt_skip: bool,
lines: Collection[tuple[int, int]],
leaf: Leaf,
)
| 391 | |
| 392 | |
| 393 | def _handle_regular_fmt_block( |
| 394 | ignored_nodes: list[LN], |
| 395 | comment: ProtoComment, |
| 396 | previous_consumed: int, |
| 397 | is_fmt_skip: bool, |
| 398 | lines: Collection[tuple[int, int]], |
| 399 | leaf: Leaf, |
| 400 | ) -> None: |
| 401 | """Handle fmt blocks with actual AST nodes.""" |
| 402 | first = ignored_nodes[0] # Can be a container node with the `leaf`. |
| 403 | parent = first.parent |
| 404 | prefix = first.prefix |
| 405 | |
| 406 | if contains_fmt_directive(comment.value, FMT_OFF): |
| 407 | first.prefix = prefix[comment.consumed :] |
| 408 | if is_fmt_skip: |
| 409 | first.prefix = "" |
| 410 | standalone_comment_prefix = prefix |
| 411 | else: |
| 412 | standalone_comment_prefix = prefix[:previous_consumed] + "\n" * comment.newlines |
| 413 | |
| 414 | # Ensure STANDALONE_COMMENT nodes have trailing newlines when stringified |
| 415 | # This prevents multiple fmt: skip comments from being concatenated on one line |
| 416 | parts = [] |
| 417 | for node in ignored_nodes: |
| 418 | if isinstance(node, Leaf) and node.type == STANDALONE_COMMENT: |
| 419 | # Add newline after STANDALONE_COMMENT Leaf |
| 420 | node_str = str(node) |
| 421 | if not node_str.endswith("\n"): |
| 422 | node_str += "\n" |
| 423 | parts.append(node_str) |
| 424 | elif isinstance(node, Node): |
| 425 | # For nodes that might contain STANDALONE_COMMENT leaves, |
| 426 | # we need custom stringify |
| 427 | has_standalone = any( |
| 428 | leaf.type == STANDALONE_COMMENT for leaf in node.leaves() |
| 429 | ) |
| 430 | if has_standalone: |
| 431 | # Stringify node with STANDALONE_COMMENT leaves having trailing newlines |
| 432 | def stringify_node(n: LN) -> str: |
| 433 | if isinstance(n, Leaf): |
| 434 | if n.type == STANDALONE_COMMENT: |
| 435 | result = n.prefix + n.value |
| 436 | if not result.endswith("\n"): |
| 437 | result += "\n" |
| 438 | return result |
| 439 | return str(n) |
| 440 | else: |
| 441 | # For nested nodes, recursively process children |
| 442 | return "".join(stringify_node(child) for child in n.children) |
| 443 | |
| 444 | parts.append(stringify_node(node)) |
| 445 | else: |
| 446 | parts.append(str(node)) |
| 447 | else: |
| 448 | parts.append(str(node)) |
| 449 | |
| 450 | hidden_value = "".join(parts) |
no test coverage detected