Skip to content

Commit 1b57797

Browse files
olehermanseclaude
andcommitted
Fixed formatter to indent trailing comments in bodies and add empty lines according to test expectations
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
1 parent d2e3778 commit 1b57797

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

src/cfengine_cli/format.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,16 +733,31 @@ def _needs_blank_line_before(child: Node, indent: int, line_length: int) -> bool
733733
)
734734
return not both_single
735735

736+
if child.type == "class_guarded_promise_block_attributes":
737+
return prev.type in {"attribute", "class_guarded_promise_block_attributes"}
738+
736739
if child.type in CLASS_GUARD_TYPES:
737740
return prev.type in {"promise", "half_promise", "class_guarded_promises"}
738741

739742
if child.type == "comment":
740743
if prev.type not in {"promise", "half_promise"} | CLASS_GUARD_TYPES:
741744
return False
742745
parent = child.parent
743-
return bool(
744-
parent and parent.type in {"bundle_section", "class_guarded_promises"}
745-
)
746+
if parent and parent.type in {"bundle_section", "class_guarded_promises"}:
747+
return True
748+
# Inside a body/promise block, a comment between two class guards
749+
# only gets a blank-line separator when the preceding class guard
750+
# already has interior comments (i.e. the visual block is rich
751+
# enough that running it together with the next would look dense).
752+
if parent and parent.type in {"body_block_body", "promise_block_body"}:
753+
if _skip_comments(child.next_named_sibling, "next") is None:
754+
return False
755+
if prev.type in CLASS_GUARD_TYPES and any(
756+
c.type == "comment" for c in prev.children
757+
):
758+
return True
759+
return False
760+
return False
746761

747762
return False
748763

@@ -775,8 +790,13 @@ def _skip_comments(sibling: Node | None, direction: str = "next") -> Node | None
775790
def _comment_indent(node: Node, indent: int) -> int:
776791
"""Compute indentation for a leaf comment based on its nearest non-comment neighbor."""
777792
nearest = _skip_comments(node.next_named_sibling, "next")
793+
# A trailing comment whose previous sibling is a class-guarded group
794+
# lines up with that group's contents (one extra indent level), as if
795+
# it were the last comment inside the class guard.
778796
if nearest is None:
779797
nearest = _skip_comments(node.prev_named_sibling, "prev")
798+
if nearest and nearest.type in CLASS_GUARD_TYPES:
799+
return indent + 4
780800
if nearest and nearest.type in INDENTED_TYPES:
781801
return indent + 2
782802
# No indented sibling found — if we're directly inside a block body,

0 commit comments

Comments
 (0)