From 33f74f6a4540de61a4389dd49facf3ef253bdc2a Mon Sep 17 00:00:00 2001 From: "Daniel G. Taylor" Date: Tue, 29 Oct 2019 21:59:23 -0700 Subject: [PATCH] Fix comment indent bug; bump version --- CHANGELOG.md | 7 ++++++- betterproto/plugin.py | 17 +++++++++-------- betterproto/templates/template.py | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 135429a..7998a6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.2.1] - 2019-10-29 + +- Fix comment indentation bug in rendered gRPC methods. + ## [1.2.0] - 2019-10-28 - Generated code output auto-formatting via [Black](https://github.com/psf/black) @@ -29,7 +33,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Initial release -[unreleased]: https://github.com/danielgtaylor/python-betterproto/compare/v1.2.0...HEAD +[unreleased]: https://github.com/danielgtaylor/python-betterproto/compare/v1.2.1...HEAD +[1.2.1]: https://github.com/danielgtaylor/python-betterproto/compare/v1.2.0...v1.2.1 [1.2.0]: https://github.com/danielgtaylor/python-betterproto/compare/v1.1.0...v1.2.0 [1.1.0]: https://github.com/danielgtaylor/python-betterproto/compare/v1.0.1...v1.1.0 [1.0.1]: https://github.com/danielgtaylor/python-betterproto/compare/v1.0.0...v1.0.1 diff --git a/betterproto/plugin.py b/betterproto/plugin.py index 02a660d..fcb3927 100755 --- a/betterproto/plugin.py +++ b/betterproto/plugin.py @@ -142,25 +142,26 @@ def traverse(proto_file): ) -def get_comment(proto_file, path: List[int]) -> str: +def get_comment(proto_file, path: List[int], indent: int = 4) -> str: + pad = " " * indent for sci in proto_file.source_code_info.location: # print(list(sci.path), path, file=sys.stderr) if list(sci.path) == path and sci.leading_comments: lines = textwrap.wrap( - sci.leading_comments.strip().replace("\n", ""), width=75 + sci.leading_comments.strip().replace("\n", ""), width=79 - indent ) if path[-2] == 2 and path[-4] != 6: # This is a field - return " # " + "\n # ".join(lines) + return f"{pad}# " + f"\n{pad}# ".join(lines) else: # This is a message, enum, service, or method - if len(lines) == 1 and len(lines[0]) < 70: + if len(lines) == 1 and len(lines[0]) < 79 - indent - 6: lines[0] = lines[0].strip('"') - return f' """{lines[0]}"""' + return f'{pad}"""{lines[0]}"""' else: - joined = "\n ".join(lines) - return f' """\n {joined}\n """' + joined = f"\n{pad}".join(lines) + return f'{pad}"""\n{pad}{joined}\n{pad}"""' return "" @@ -371,7 +372,7 @@ def generate_code(request, response): { "name": method.name, "py_name": stringcase.snakecase(method.name), - "comment": get_comment(proto_file, [6, i, 2, j]), + "comment": get_comment(proto_file, [6, i, 2, j], indent=8), "route": f"/{package}.{service.name}/{method.name}", "input": get_ref_type( package, output["imports"], method.input_type diff --git a/betterproto/templates/template.py b/betterproto/templates/template.py index d0f14e8..4c18ccc 100644 --- a/betterproto/templates/template.py +++ b/betterproto/templates/template.py @@ -15,8 +15,8 @@ import betterproto {% if description.services %} import grpclib {% endif %} -{% for i in description.imports %} +{% for i in description.imports %} {{ i }} {% endfor %}