The cause of that bug is that in a particular parse tree, the node
treesit_cursor_helper tries to go to is a missing node, not only is it
a missing node, it is the first node of a subtree. So when
treesit_cursor_helper follows the algorithm and goes down the tree, it
goes down the previous subtree (because that subtree's end = end_pos,
because the target node has zero width).
o
|
o--+-o
| |
+-+ +-+-+
| | | | |
o x t o o
(We ended up in x when the target is t, because t has zero width.)
One way to solve it is to go back up the tree if we are at a leaf node
and still haven't matched the target node. That's too ugly and
finicky so I resorted to recursion. Now one more functions will
return give up (treesit_node_parent) if we are in a werid parse tree
that is super deep. But since we already kind of give up on this kind
of parse trees (bug#59426), it doesn't really hurt.
* src/treesit.c (treesit_cursor_helper_1): New function.
(treesit_cursor_helper): Use the new function. Change return type to
bool, and accept a cursor pointer.
(Ftreesit_node_parent)
(Ftreesit_search_subtree)
(Ftreesit_search_forward)
(Ftreesit_induce_sparse_tree): Use the new signature.