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", 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!"; +}