Skip to content

Commit 4c73e8a

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 cc6bc94 commit 4c73e8a

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

src/cfengine_cli/format.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,7 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
233233
else:
234234
parts.append(text(p))
235235
# Append directly to previous part (no space before parens)
236-
header_parts[-1] = header_parts[-1] + stringify_parameter_list(
237-
parts
238-
)
236+
header_parts[-1] = header_parts[-1] + stringify_parameter_list(parts)
239237
else:
240238
header_parts.append(text(x))
241239
line = " ".join(header_parts)
@@ -244,7 +242,15 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
244242
if not (prev_sib and prev_sib.type == "comment"):
245243
fmt.print("", 0)
246244
fmt.print(line, 0)
247-
for comment in header_comments:
245+
for i, comment in enumerate(header_comments):
246+
if comment.strip() == "#":
247+
prev_is_comment = i > 0 and header_comments[i - 1].strip() != "#"
248+
next_is_comment = (
249+
i + 1 < len(header_comments)
250+
and header_comments[i + 1].strip() != "#"
251+
)
252+
if not (prev_is_comment and next_is_comment):
253+
continue
248254
fmt.print(comment, 0)
249255
children = node.children[-1].children
250256
if node.type in [
@@ -263,11 +269,17 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
263269
return
264270
if node.type == "promise":
265271
# Single-line promise: if exactly 1 attribute, no half_promise continuation,
266-
# and the whole line fits in line_length
272+
# not inside a class guard, and the whole line fits in line_length
267273
attr_children = [c for c in children if c.type == "attribute"]
268274
next_sib = node.next_named_sibling
269275
has_continuation = next_sib and next_sib.type == "half_promise"
270-
if len(attr_children) == 1 and not has_continuation:
276+
parent = node.parent
277+
in_class_guard = parent and parent.type in [
278+
"class_guarded_promises",
279+
"class_guarded_body_attributes",
280+
"class_guarded_promise_block_attributes",
281+
]
282+
if len(attr_children) == 1 and not has_continuation and not in_class_guard:
271283
promiser_node = next((c for c in children if c.type == "promiser"), None)
272284
if promiser_node:
273285
line = (
@@ -281,8 +293,13 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
281293
return
282294
if children:
283295
for child in children:
296+
# Blank line between bundle sections
297+
if child.type == "bundle_section":
298+
prev = child.prev_named_sibling
299+
if prev and prev.type == "bundle_section":
300+
fmt.print("", 0)
284301
# Blank line between promises in a section
285-
if child.type == "promise":
302+
elif child.type == "promise":
286303
prev = child.prev_named_sibling
287304
if prev and prev.type in ["promise", "half_promise"]:
288305
fmt.print("", 0)
@@ -295,6 +312,7 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
295312
if prev and prev.type in [
296313
"promise",
297314
"half_promise",
315+
"class_guarded_promises",
298316
]:
299317
fmt.print("", 0)
300318
elif child.type == "comment":
@@ -312,6 +330,11 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
312330
fmt.print_same_line(node)
313331
return
314332
if node.type == "comment":
333+
if text(node).strip() == "#":
334+
prev = node.prev_named_sibling
335+
nxt = node.next_named_sibling
336+
if not (prev and prev.type == "comment" and nxt and nxt.type == "comment"):
337+
return
315338
comment_indent = indent
316339
next_sib = node.next_named_sibling
317340
while next_sib and next_sib.type == "comment":

0 commit comments

Comments
 (0)