Skip to content

Commit 3d86604

Browse files
olehermanseclaude
andcommitted
Adjusted formatter according to test expectations
The formatter now: - Removes empty leading / trailing comments. - Inserts empty lines before each class guard except the first one. - Inserts empty lines before each promise guard except the first one. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Ole Herman Schumacher Elgesem <ole.elgesem@northern.tech>
1 parent 3d14ec6 commit 3d86604

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/cfengine_cli/format.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,12 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
244244
if not (prev_sib and prev_sib.type == "comment"):
245245
fmt.print("", 0)
246246
fmt.print(line, 0)
247-
for comment in header_comments:
247+
for i, comment in enumerate(header_comments):
248+
if comment.strip() == "#":
249+
prev_is_comment = i > 0 and header_comments[i - 1].strip() != "#"
250+
next_is_comment = i + 1 < len(header_comments) and header_comments[i + 1].strip() != "#"
251+
if not (prev_is_comment and next_is_comment):
252+
continue
248253
fmt.print(comment, 0)
249254
children = node.children[-1].children
250255
if node.type in [
@@ -263,11 +268,17 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
263268
return
264269
if node.type == "promise":
265270
# Single-line promise: if exactly 1 attribute, no half_promise continuation,
266-
# and the whole line fits in line_length
271+
# not inside a class guard, and the whole line fits in line_length
267272
attr_children = [c for c in children if c.type == "attribute"]
268273
next_sib = node.next_named_sibling
269274
has_continuation = next_sib and next_sib.type == "half_promise"
270-
if len(attr_children) == 1 and not has_continuation:
275+
parent = node.parent
276+
in_class_guard = parent and parent.type in [
277+
"class_guarded_promises",
278+
"class_guarded_body_attributes",
279+
"class_guarded_promise_block_attributes",
280+
]
281+
if len(attr_children) == 1 and not has_continuation and not in_class_guard:
271282
promiser_node = next((c for c in children if c.type == "promiser"), None)
272283
if promiser_node:
273284
line = (
@@ -281,8 +292,13 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
281292
return
282293
if children:
283294
for child in children:
295+
# Blank line between bundle sections
296+
if child.type == "bundle_section":
297+
prev = child.prev_named_sibling
298+
if prev and prev.type == "bundle_section":
299+
fmt.print("", 0)
284300
# Blank line between promises in a section
285-
if child.type == "promise":
301+
elif child.type == "promise":
286302
prev = child.prev_named_sibling
287303
if prev and prev.type in ["promise", "half_promise"]:
288304
fmt.print("", 0)
@@ -295,6 +311,7 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
295311
if prev and prev.type in [
296312
"promise",
297313
"half_promise",
314+
"class_guarded_promises",
298315
]:
299316
fmt.print("", 0)
300317
elif child.type == "comment":
@@ -312,6 +329,11 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
312329
fmt.print_same_line(node)
313330
return
314331
if node.type == "comment":
332+
if text(node).strip() == "#":
333+
prev = node.prev_named_sibling
334+
nxt = node.next_named_sibling
335+
if not (prev and prev.type == "comment" and nxt and nxt.type == "comment"):
336+
return
315337
comment_indent = indent
316338
next_sib = node.next_named_sibling
317339
while next_sib and next_sib.type == "comment":

0 commit comments

Comments
 (0)