t3rn0 / ast-comments

Extension to the built-in ast module. Finds comments in source code and adds them to the parsed tree.
MIT License
31 stars 10 forks source link

Make inline comments stay inline #9

Closed jvllmr closed 1 year ago

jvllmr commented 1 year ago

First of all, thanks for this project. I use it in my project for detecting ignore comments :D

This is my approach for making inline comments stay inline. I overwrote the traverse to inspect the first item of the body (maybe a comment) and save the lineno to a list if the lines of body parent and comment match. When visiting a comment I immediately write the comment with two leading spaces instead of writing a newline by using fill.

When adding a setup.py with your package name you can see which packages depend on yours on github. Just a litte bonus :)

t3rn0 commented 1 year ago

That's a great work you've done here! Here are some thoughts:

setup.py

As I understand setup.py adds no value here: poetry.lock is well enough for github and its dependency graph (https://docs.github.com/en/code-security/supply-chain-security/understanding-your-software-supply-chain/about-the-dependency-graph#supported-package-ecosystems).

inlined comments work in very specific cases

Your solution works with def, class, if, ... With everything that has body attribute.

However, if we start identifying inlined comments for if-parts we'd better do this for elif/else parts also (https://github.com/t3rn0/ast-comments/issues/10). Moreover, having this inlined-regular logic inside traverse method will make the problem impossible to solve for simple expressions like a = 1 # inlined.

I'd compare line numbers in _enhance function and store inline info into Comment.inline (bool) attribute. I mean, I don't have any concrete solution yet, it's just a thought.