Skip to content

Commit 1741a12

Browse files
Add all kinds of comments (#632)
* Add all kinds of comments * Format * Fix get_comment * Improve test * Lint
1 parent 1a23f09 commit 1741a12

File tree

3 files changed

+78
-24
lines changed

3 files changed

+78
-24
lines changed

src/betterproto/plugin/models.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,33 @@ def get_comment(
153153
) -> str:
154154
pad = " " * indent
155155
for sci_loc in proto_file.source_code_info.location:
156-
if list(sci_loc.path) == path and sci_loc.leading_comments:
157-
lines = sci_loc.leading_comments.strip().split("\n")
156+
if list(sci_loc.path) == path:
157+
all_comments = list(sci_loc.leading_detached_comments)
158+
if sci_loc.leading_comments:
159+
all_comments.append(sci_loc.leading_comments)
160+
if sci_loc.trailing_comments:
161+
all_comments.append(sci_loc.trailing_comments)
162+
163+
lines = []
164+
165+
for comment in all_comments:
166+
lines += comment.split("\n")
167+
lines.append("")
168+
169+
# Remove consecutive empty lines
170+
lines = [
171+
line for i, line in enumerate(lines) if line or (i == 0 or lines[i - 1])
172+
]
173+
174+
if lines and not lines[-1]:
175+
lines.pop() # Remove the last empty line
176+
177+
# It is common for one line comments to start with a space, for example: // comment
178+
# We don't add this space to the generated file.
179+
lines = [line[1:] if line and line[0] == " " else line for line in lines]
180+
158181
# This is a field, message, enum, service, or method
159182
if len(lines) == 1 and len(lines[0]) < 79 - indent - 6:
160-
lines[0] = lines[0].strip('"')
161183
return f'{pad}"""{lines[0]}"""'
162184
else:
163185
joined = f"\n{pad}".join(lines)
Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
11
syntax = "proto3";
22
package documentation;
33

4-
// Documentation of message
5-
message Test {
6-
// Documentation of field
7-
uint32 x = 1;
4+
// Documentation of message 1
5+
// other line 1
6+
7+
// Documentation of message 2
8+
// other line 2
9+
message Test { // Documentation of message 3
10+
// Documentation of field 1
11+
// other line 1
12+
13+
// Documentation of field 2
14+
// other line 2
15+
uint32 x = 1; // Documentation of field 3
816
}
917

10-
// Documentation of enum
11-
enum Enum {
12-
// Documentation of variant
13-
Enum_Variant = 0;
18+
// Documentation of enum 1
19+
// other line 1
20+
21+
// Documentation of enum 2
22+
// other line 2
23+
enum Enum { // Documentation of enum 3
24+
// Documentation of variant 1
25+
// other line 1
26+
27+
// Documentation of variant 2
28+
// other line 2
29+
Enum_Variant = 0; // Documentation of variant 3
1430
}
1531

16-
// Documentation of service
17-
service Service {
18-
// Documentation of method
19-
rpc get(Test) returns (Test);
32+
// Documentation of service 1
33+
// other line 1
34+
35+
// Documentation of service 2
36+
// other line 2
37+
service Service { // Documentation of service 3
38+
// Documentation of method 1
39+
// other line 1
40+
41+
// Documentation of method 2
42+
// other line 2
43+
rpc get(Test) returns (Test); // Documentation of method 3
2044
}

tests/test_documentation.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,36 @@
22
import inspect
33

44

5-
def test_documentation():
5+
def check(generated_doc: str, type: str) -> None:
6+
assert f"Documentation of {type} 1" in generated_doc
7+
assert "other line 1" in generated_doc
8+
assert f"Documentation of {type} 2" in generated_doc
9+
assert "other line 2" in generated_doc
10+
assert f"Documentation of {type} 3" in generated_doc
11+
12+
13+
def test_documentation() -> None:
614
from .output_betterproto.documentation import (
715
Enum,
816
ServiceBase,
917
ServiceStub,
1018
Test,
1119
)
1220

13-
assert Test.__doc__ == "Documentation of message"
21+
check(Test.__doc__, "message")
1422

1523
source = inspect.getsource(Test)
1624
tree = ast.parse(source)
17-
assert tree.body[0].body[2].value.value == "Documentation of field"
25+
check(tree.body[0].body[2].value.value, "field")
1826

19-
assert Enum.__doc__ == "Documentation of enum"
27+
check(Enum.__doc__, "enum")
2028

2129
source = inspect.getsource(Enum)
2230
tree = ast.parse(source)
23-
assert tree.body[0].body[2].value.value == "Documentation of variant"
31+
check(tree.body[0].body[2].value.value, "variant")
2432

25-
assert ServiceBase.__doc__ == "Documentation of service"
26-
assert ServiceBase.get.__doc__ == "Documentation of method"
33+
check(ServiceBase.__doc__, "service")
34+
check(ServiceBase.get.__doc__, "method")
2735

28-
assert ServiceStub.__doc__ == "Documentation of service"
29-
assert ServiceStub.get.__doc__ == "Documentation of method"
36+
check(ServiceStub.__doc__, "service")
37+
check(ServiceStub.get.__doc__, "method")

0 commit comments

Comments
 (0)