astsearch
astsearch copied to clipboard
print the whole expression matching the ast query
Currently, astsearch only prints the line at which the ast query match starts. It would be nice (and offer a real plus compared to just grepping, which is inaccurate but often good enough) if it instead printed the whole matching expression (or offers a flag to do so).
Yup, that would be nice. It's a bit fiddly, because Python only records the line where a node starts, not where it ends. I can think of two approaches to work out where it ends:
- Get the line number of the next sibling node, and then work out somehow whether the node of interest finishes on that line or the previous one. Tricky, because we get the node without an easy way to get information on its siblings.
- Go through all children of the node, and get the last (highest) line number from them. Easier, but I suspect it won't be right in all situations, e.g.:
a = [1,
2,
]
There's no node for the closing bracket, so it would only show the first two