Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make inlined comments not overlap #17

Merged
merged 1 commit into from
Jun 24, 2023
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
23 changes: 22 additions & 1 deletion ast_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ def _enrich(source: Union[str, bytes], tree: ast.AST) -> None:
attr.append(c_node)
attr.sort(key=lambda x: (x.end_lineno, isinstance(x, Comment)))

# NOTE:
# Due to some issues it's possible for comment nodes to go outside of their initial place
# after the parse-unparse roundtip:
# before parse/unparse:
# ```
# # comment 0
# some_code # comment 1
# ```
# after parse/unparse:
# ```
# # comment 0 # comment 1
# some_code
# ```
# As temporary workaround I decided to correct inline attributes here so they don't
# overlap with each other. This place should be revisited after solving following issues:
# - https://github.com/t3rn0/ast-comments/issues/10
# - https://github.com/t3rn0/ast-comments/issues/13
for left, right in zip(attr[:-1], attr[1:]):
if isinstance(left, Comment) and isinstance(right, Comment):
right.inline = False


def _get_tree_intervals(
node: ast.AST,
Expand All @@ -95,7 +116,7 @@ def _get_tree_intervals(
low = node.lineno if hasattr(node, "lineno") else min(attr_intervals)[0]
high = (
node.end_lineno
if hasattr(node, "endlineno")
if hasattr(node, "end_lineno")
else max(attr_intervals)[1]
)
res[(low, high)] = {"intervals": attr_intervals, "node": node}
Expand Down
Loading