Skip to content

Commit 367eed6

Browse files
authored
Merge pull request #65 from olehermanse/empty-lines
cfengine format: Fixed indentation of comments inside empty bundles and ensured empty lines between bundles were empty comments were removed
2 parents de75dce + 808784e commit 367eed6

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/cfengine_cli/format.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

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

28+
BLOCK_BODY_TYPES = {"bundle_block_body", "promise_block_body", "body_block_body"}
29+
2830
PROMISER_PARTS = {"promiser", "->", "stakeholder"}
2931

3032

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

648657

tests/format/005_bundle_comments.expected.cf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ bundle agent f(s, d, o, p)
4747
reports:
4848
"Hello, world!";
4949
}
50+
51+
bundle agent g
52+
{
53+
# comment
54+
}

tests/format/005_bundle_comments.input.cf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ bundle agent f(
4545
reports:
4646
"Hello, world!";
4747
}
48+
#
49+
bundle agent g
50+
{
51+
# comment
52+
}

0 commit comments

Comments
 (0)