Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/cfengine_cli/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

BLOCK_TYPES = {"bundle_block", "promise_block", "body_block"}

BLOCK_BODY_TYPES = {"bundle_block_body", "promise_block_body", "body_block_body"}

PROMISER_PARTS = {"promiser", "->", "stakeholder"}


Expand Down Expand Up @@ -559,6 +561,9 @@ def _format_block_header(node: Node, fmt: Formatter) -> list[Node]:
line = " ".join(header_parts)
if not fmt.empty:
prev_sib = node.prev_named_sibling
# Skip over preceding empty comments since they will be removed
while prev_sib and prev_sib.type == "comment" and _is_empty_comment(prev_sib):
prev_sib = prev_sib.prev_named_sibling
if not (prev_sib and prev_sib.type == "comment"):
fmt.blank_line()
fmt.print(line, 0)
Expand Down Expand Up @@ -643,6 +648,10 @@ def _comment_indent(node: Node, indent: int) -> int:
nearest = _skip_comments(node.prev_named_sibling, "prev")
if nearest and nearest.type in INDENTED_TYPES:
return indent + 2
# No indented sibling found — if we're directly inside a block body,
# indent so the comment lines up with where promises/attributes would.
if nearest is None and node.parent and node.parent.type in BLOCK_BODY_TYPES:
return indent + 2
return indent


Expand Down
5 changes: 5 additions & 0 deletions tests/format/005_bundle_comments.expected.cf
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ bundle agent f(s, d, o, p)
reports:
"Hello, world!";
}

bundle agent g
{
# comment
}
5 changes: 5 additions & 0 deletions tests/format/005_bundle_comments.input.cf
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ bundle agent f(
reports:
"Hello, world!";
}
#
bundle agent g
{
# comment
}
Loading
Loading