From 1f1fcc8a37174c5bad8275a7c0b28a2c50c7a87c Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem Date: Tue, 31 Mar 2026 23:43:11 +0200 Subject: [PATCH 1/2] Added formatting test for bundle "docstrings" Signed-off-by: Ole Herman Schumacher Elgesem --- tests/format/005_bundle_comments.expected.cf | 49 ++++++++++++++++++++ tests/format/005_bundle_comments.input.cf | 47 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 tests/format/005_bundle_comments.expected.cf create mode 100644 tests/format/005_bundle_comments.input.cf diff --git a/tests/format/005_bundle_comments.expected.cf b/tests/format/005_bundle_comments.expected.cf new file mode 100644 index 0000000..9d3b8ab --- /dev/null +++ b/tests/format/005_bundle_comments.expected.cf @@ -0,0 +1,49 @@ +bundle agent a +# @brief Description of a +{ + reports: + "Hello, world!"; +} + +bundle agent b +# @brief Description of b +# @note Something important +{ + reports: + "Hello, world!"; +} + +bundle agent c +# @brief Description of c +# @note Something important +{ + reports: + "Hello, world!"; +} + +bundle agent d +# @brief Description of d +# @note Something important +{ + reports: + "Hello, world!"; +} + +bundle agent e +# keyword +# type +# name +{ + reports: + "Hello, world!"; +} + +bundle agent f(s, d, o, p) +# source - where to copy from +# destination - folder or file path +# owner - root, nick +# perms - 0677 +{ + reports: + "Hello, world!"; +} diff --git a/tests/format/005_bundle_comments.input.cf b/tests/format/005_bundle_comments.input.cf new file mode 100644 index 0000000..f2d600d --- /dev/null +++ b/tests/format/005_bundle_comments.input.cf @@ -0,0 +1,47 @@ +bundle agent a # @brief Description of a +{ + reports: + "Hello, world!"; +} + +bundle agent b + # @brief Description of b + # @note Something important +{ + reports: + "Hello, world!"; +} + +bundle agent c # @brief Description of c +# @note Something important +{ + reports: + "Hello, world!"; +} + +bundle agent d +# @brief Description of d +# @note Something important +{ + reports: + "Hello, world!"; +} + +bundle # keyword +agent # type +e # name +{ + reports: + "Hello, world!"; +} + +bundle agent f( + s, # source - where to copy from + d, # destination - folder or file path + o, # owner - root, nick + p, # perms - 0677 +) +{ + reports: + "Hello, world!"; +} From 97f40859944c3a37bd4279500659d581020e70cb Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem Date: Wed, 1 Apr 2026 17:31:13 +0200 Subject: [PATCH 2/2] Fixed formatting of bundle "docstrings" Co-authored-by: Claude Opus 4.6 (1M context) Signed-off-by: Ole Herman Schumacher Elgesem --- src/cfengine_cli/format.py | 41 +++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/cfengine_cli/format.py b/src/cfengine_cli/format.py index 8595fc1..033a2c2 100644 --- a/src/cfengine_cli/format.py +++ b/src/cfengine_cli/format.py @@ -49,6 +49,25 @@ def update_previous(self, node): return tmp +def stringify_children_from_strings(parts): + # Remove trailing comma before closing paren + cleaned = [] + for i, part in enumerate(parts): + if part == "," and i + 1 < len(parts) and parts[i + 1] == ")": + continue + cleaned.append(part) + result = "" + previous = None + for part in cleaned: + if previous and previous != "(" and part != "," and part != ")": + result += " " + elif previous == ",": + result += " " + result += part + previous = part + return result + + def stringify_children(children): result = "" previous = None @@ -177,12 +196,32 @@ def autoformat(node, fmt, line_length, macro_indent, indent=0): return children = node.children if node.type in ["bundle_block", "promise_block", "body_block"]: - line = " ".join(x.text.decode("utf-8") for x in node.children[0:-1]) + header_parts = [] + header_comments = [] + for x in node.children[0:-1]: + if x.type == "comment": + header_comments.append(text(x)) + elif x.type == "parameter_list": + parts = [] + for p in x.children: + if p.type == "comment": + header_comments.append(text(p)) + else: + parts.append(text(p)) + # Append directly to previous part (no space before parens) + header_parts[-1] = header_parts[-1] + stringify_children_from_strings( + parts + ) + else: + header_parts.append(text(x)) + line = " ".join(header_parts) if not fmt.empty: prev_sib = node.prev_named_sibling if not (prev_sib and prev_sib.type == "comment"): fmt.print("", 0) fmt.print(line, 0) + for comment in header_comments: + fmt.print(comment, 0) children = node.children[-1].children if node.type in [ "bundle_section",