Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/cfengine_cli/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
49 changes: 49 additions & 0 deletions tests/format/005_bundle_comments.expected.cf
Original file line number Diff line number Diff line change
@@ -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!";
}
47 changes: 47 additions & 0 deletions tests/format/005_bundle_comments.input.cf
Original file line number Diff line number Diff line change
@@ -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!";
}
Loading