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

feature request: collapse multiple consecutive comments into single 'multiline' comment via some predicate #24

Closed wahuneke closed 1 year ago

wahuneke commented 1 year ago

Maybe this is a niche feature. I can just put it into my own app, if you don't want it in ast-comments.

I think it would be great to have an addition to the parse function or a pre-packaged helper that walks the tree and merges consecutive Comment nodes in cases where they fit some rule.

In my case (and I think this is not uncommon), I use this convention for multiline comments:

def my_func():
   """
   Standard function documentation .... not part of my example
   """
   function_doing_some_stuff = 1 + 2
   # TODO: Come back to this later this is really not a great
   #     solution. I think there's a better one here: https://.....
   my_dumb_solution()

In the above example, it is a multiline comment that is a long TODO item. The above convention happens to be the one followed in PyCharm, ie it will identify the above as a multiline TODO and keep track of it for you in its list of todos.

I'm proposing you add some merger predicate option which allows people to provide their own convention for merging one comment with another. Probably something like: Callable[[str, str], bool] meaning, we'll give you the body of the current comment and the body of the one that follows, you return True if these two should be considered part of the same comment. ie the second will get absorbed into the node above it.

As with my other issue ticket, I'm happy to impl this in a PR if you like.

Thanks!

wahuneke commented 1 year ago

I've done some googling to try to find support for this supposed coding convention. I'm not seeing much of anything anywhere. Maybe I've made the whole thing up. Pycharm definitely does it (https://www.jetbrains.com/help/pycharm/using-todo.html#add-multiline-todo)

I think this feature probably belongs just within my own app. I'll keep my implementation there unless others speak up to indicate this has broader usefulness.