Add all kinds of comments (#632)
* Add all kinds of comments * Format * Fix get_comment * Improve test * Lint
This commit is contained in:
@@ -153,11 +153,33 @@ def get_comment(
|
||||
) -> str:
|
||||
pad = " " * indent
|
||||
for sci_loc in proto_file.source_code_info.location:
|
||||
if list(sci_loc.path) == path and sci_loc.leading_comments:
|
||||
lines = sci_loc.leading_comments.strip().split("\n")
|
||||
if list(sci_loc.path) == path:
|
||||
all_comments = list(sci_loc.leading_detached_comments)
|
||||
if sci_loc.leading_comments:
|
||||
all_comments.append(sci_loc.leading_comments)
|
||||
if sci_loc.trailing_comments:
|
||||
all_comments.append(sci_loc.trailing_comments)
|
||||
|
||||
lines = []
|
||||
|
||||
for comment in all_comments:
|
||||
lines += comment.split("\n")
|
||||
lines.append("")
|
||||
|
||||
# Remove consecutive empty lines
|
||||
lines = [
|
||||
line for i, line in enumerate(lines) if line or (i == 0 or lines[i - 1])
|
||||
]
|
||||
|
||||
if lines and not lines[-1]:
|
||||
lines.pop() # Remove the last empty line
|
||||
|
||||
# It is common for one line comments to start with a space, for example: // comment
|
||||
# We don't add this space to the generated file.
|
||||
lines = [line[1:] if line and line[0] == " " else line for line in lines]
|
||||
|
||||
# This is a field, message, enum, service, or method
|
||||
if len(lines) == 1 and len(lines[0]) < 79 - indent - 6:
|
||||
lines[0] = lines[0].strip('"')
|
||||
return f'{pad}"""{lines[0]}"""'
|
||||
else:
|
||||
joined = f"\n{pad}".join(lines)
|
||||
|
||||
Reference in New Issue
Block a user