Skip to content

Commit 9aed902

Browse files
authored
Merge pull request #42 from olehermanse/main
Fixed formatting of bundle "docstrings"
2 parents 6b51e49 + 97f4085 commit 9aed902

3 files changed

Lines changed: 136 additions & 1 deletion

File tree

src/cfengine_cli/format.py

Lines changed: 40 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,32 @@ 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(
213+
parts
214+
)
215+
else:
216+
header_parts.append(text(x))
217+
line = " ".join(header_parts)
181218
if not fmt.empty:
182219
prev_sib = node.prev_named_sibling
183220
if not (prev_sib and prev_sib.type == "comment"):
184221
fmt.print("", 0)
185222
fmt.print(line, 0)
223+
for comment in header_comments:
224+
fmt.print(comment, 0)
186225
children = node.children[-1].children
187226
if node.type in [
188227
"bundle_section",
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
bundle agent a
2+
# @brief Description of a
3+
{
4+
reports:
5+
"Hello, world!";
6+
}
7+
8+
bundle agent b
9+
# @brief Description of b
10+
# @note Something important
11+
{
12+
reports:
13+
"Hello, world!";
14+
}
15+
16+
bundle agent c
17+
# @brief Description of c
18+
# @note Something important
19+
{
20+
reports:
21+
"Hello, world!";
22+
}
23+
24+
bundle agent d
25+
# @brief Description of d
26+
# @note Something important
27+
{
28+
reports:
29+
"Hello, world!";
30+
}
31+
32+
bundle agent e
33+
# keyword
34+
# type
35+
# name
36+
{
37+
reports:
38+
"Hello, world!";
39+
}
40+
41+
bundle agent f(s, d, o, p)
42+
# source - where to copy from
43+
# destination - folder or file path
44+
# owner - root, nick
45+
# perms - 0677
46+
{
47+
reports:
48+
"Hello, world!";
49+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
bundle agent a # @brief Description of a
2+
{
3+
reports:
4+
"Hello, world!";
5+
}
6+
7+
bundle agent b
8+
# @brief Description of b
9+
# @note Something important
10+
{
11+
reports:
12+
"Hello, world!";
13+
}
14+
15+
bundle agent c # @brief Description of c
16+
# @note Something important
17+
{
18+
reports:
19+
"Hello, world!";
20+
}
21+
22+
bundle agent d
23+
# @brief Description of d
24+
# @note Something important
25+
{
26+
reports:
27+
"Hello, world!";
28+
}
29+
30+
bundle # keyword
31+
agent # type
32+
e # name
33+
{
34+
reports:
35+
"Hello, world!";
36+
}
37+
38+
bundle agent f(
39+
s, # source - where to copy from
40+
d, # destination - folder or file path
41+
o, # owner - root, nick
42+
p, # perms - 0677
43+
)
44+
{
45+
reports:
46+
"Hello, world!";
47+
}

0 commit comments

Comments
 (0)