diff --git a/src/cfengine_cli/format.py b/src/cfengine_cli/format.py index 9ea2236..bce9009 100644 --- a/src/cfengine_cli/format.py +++ b/src/cfengine_cli/format.py @@ -743,6 +743,12 @@ def _needs_blank_line_before(child: Node, indent: int, line_length: int) -> bool if child.type == "comment": if _is_empty_comment(child): return False + # Empty comments preceding this one will be dropped — look past them + # so we treat the comment as following the real prior content. + while prev and prev.type == "comment" and _is_empty_comment(prev): + prev = prev.prev_named_sibling + if prev is None: + return False # Top-level comment after a complete block — visually separates them if prev.type in BLOCK_TYPES: return True diff --git a/tests/format/004_comments.expected.cf b/tests/format/004_comments.expected.cf index 421abe4..00507c0 100644 --- a/tests/format/004_comments.expected.cf +++ b/tests/format/004_comments.expected.cf @@ -144,3 +144,10 @@ body package_method cgcacgcac # bar package_changes => "bulk"; } + +# Example here: +bundle agent stigs +{ + vars: + "foo" string => "bar"; +} diff --git a/tests/format/004_comments.input.cf b/tests/format/004_comments.input.cf index 0d1f90a..1f2c7b9 100644 --- a/tests/format/004_comments.input.cf +++ b/tests/format/004_comments.input.cf @@ -133,3 +133,13 @@ package_changes => "bulk"; # bar package_changes => "bulk"; } + +# +# Example here: +# + +bundle agent stigs +{ + vars: + "foo" string => "bar"; +}