Skip to content

Commit 64e5b8e

Browse files
olehermanseclaude
andcommitted
cfengine format: Single line promises in more cases
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 840c4a6 commit 64e5b8e

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

src/cfengine_cli/format.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,25 @@ def stringify(node, indent, line_length):
207207
return [single_line]
208208

209209

210+
def can_single_line_promise(node, indent, line_length):
211+
"""Check if a promise node can be formatted on a single line."""
212+
if node.type != "promise":
213+
return False
214+
children = node.children
215+
attr_children = [c for c in children if c.type == "attribute"]
216+
next_sib = node.next_named_sibling
217+
has_continuation = next_sib and next_sib.type == "half_promise"
218+
if len(attr_children) != 1 or has_continuation:
219+
return False
220+
promiser_node = next((c for c in children if c.type == "promiser"), None)
221+
if not promiser_node:
222+
return False
223+
line = (
224+
text(promiser_node) + " " + stringify_single_line_node(attr_children[0]) + ";"
225+
)
226+
return indent + len(line) <= line_length
227+
228+
210229
def autoformat(node, fmt, line_length, macro_indent, indent=0):
211230
previous = fmt.update_previous(node)
212231
if previous and previous.type == "macro" and text(previous).startswith("@else"):
@@ -273,13 +292,7 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
273292
attr_children = [c for c in children if c.type == "attribute"]
274293
next_sib = node.next_named_sibling
275294
has_continuation = next_sib and next_sib.type == "half_promise"
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:
295+
if len(attr_children) == 1 and not has_continuation:
283296
promiser_node = next((c for c in children if c.type == "promiser"), None)
284297
if promiser_node:
285298
line = (
@@ -302,7 +315,15 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
302315
elif child.type == "promise":
303316
prev = child.prev_named_sibling
304317
if prev and prev.type in ["promise", "half_promise"]:
305-
fmt.print("", 0)
318+
# Skip blank line between consecutive single-line promises
319+
promise_indent = indent + 2
320+
both_single = (
321+
prev.type == "promise"
322+
and can_single_line_promise(prev, promise_indent, line_length)
323+
and can_single_line_promise(child, promise_indent, line_length)
324+
)
325+
if not both_single:
326+
fmt.print("", 0)
306327
elif child.type in [
307328
"class_guarded_promises",
308329
"class_guarded_body_attributes",

0 commit comments

Comments
 (0)