Skip to content

Commit 7a32a3d

Browse files
committed
Fixed formatting of bundle "docstrings"
Signed-off-by: Ole Herman Schumacher Elgesem <ole.elgesem@northern.tech>
1 parent 1f1fcc8 commit 7a32a3d

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

src/cfengine_cli/format.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ def update_previous(self, node):
4949
return tmp
5050

5151

52+
def stringify_children_from_strings(parts):
53+
# Remove trailing comma before closing paren
54+
cleaned = []
55+
for i, part in enumerate(parts):
56+
if part == "," and i + 1 < len(parts) and parts[i + 1] == ")":
57+
continue
58+
cleaned.append(part)
59+
result = ""
60+
previous = None
61+
for part in cleaned:
62+
if previous and previous != "(" and part != "," and part != ")":
63+
result += " "
64+
elif previous == ",":
65+
result += " "
66+
result += part
67+
previous = part
68+
return result
69+
70+
5271
def stringify_children(children):
5372
result = ""
5473
previous = None
@@ -177,12 +196,30 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0):
177196
return
178197
children = node.children
179198
if node.type in ["bundle_block", "promise_block", "body_block"]:
180-
line = " ".join(x.text.decode("utf-8") for x in node.children[0:-1])
199+
header_parts = []
200+
header_comments = []
201+
for x in node.children[0:-1]:
202+
if x.type == "comment":
203+
header_comments.append(text(x))
204+
elif x.type == "parameter_list":
205+
parts = []
206+
for p in x.children:
207+
if p.type == "comment":
208+
header_comments.append(text(p))
209+
else:
210+
parts.append(text(p))
211+
# Append directly to previous part (no space before parens)
212+
header_parts[-1] = header_parts[-1] + stringify_children_from_strings(parts)
213+
else:
214+
header_parts.append(text(x))
215+
line = " ".join(header_parts)
181216
if not fmt.empty:
182217
prev_sib = node.prev_named_sibling
183218
if not (prev_sib and prev_sib.type == "comment"):
184219
fmt.print("", 0)
185220
fmt.print(line, 0)
221+
for comment in header_comments:
222+
fmt.print(comment, 0)
186223
children = node.children[-1].children
187224
if node.type in [
188225
"bundle_section",

0 commit comments

Comments
 (0)