Subject: bug#66989: 30.0.50; tree-sitter: treesit-defun-at-point wrong behavior in python-ts-mode

From: Yuan Fu <[email-masked]>
Date: Thu, 9 Nov 2023 00:13:24 -0800

> On Nov 8, 2023, at 12:37 PM, Zubarev Dv <[email-masked]> wrote:
> 
> Initially I thought that it is the second definition.  It seems to be more intuitive. But after reading code, I was sure its the first case.
> 
> But I actually don't sure that all three "things" are needed at once.
> For example,  I think for `thing-at-point` only enclosing parent is needed.
> If parent is nil, it may optionally fallback to the next sibling thing.
> `treesit--navigate-thing` uses only `parent` and `next` or `prev`.
> I was playing with creating evil text objects, based on your awesome
> work with "things".
> I ended up extracting the second part (;; 2. Find the parent) from `treesit--things-around`.
> Since I only need to find enclosing parent and I don't want to pay for
> what I don't use.
> So it seems no function uses all results from `treesit--things-around`.
> 
> Maybe it makes sense to decompose `treesit--things-around` into two
> functions:
> 1. (treesit--enclosing-thing (pos thing)) - returns node or nil if no
> enclosing parent
> 2. (treesit--find-sibling (pos thing dir)) - return sibling node in
> specified direction.

Makes sense. I think separating into two functions is ok. For treesit--enclosing-thing, we might as well call it treesit--thing-at-point ;-) Tentative plan: retire treesit—things-around, add treesit--enclosing-thing (as --thing-at-point), add treesit--find-sibling (as --sibling-thing), add treesit-node-enclose-p which tests whether a node encloses another node.

> 
> BTW, I've found another unexpected behavior on the same code:
> 
> 
> 1. python-ts-mode
> 
> def premain():
>    pa|ss
> def main():
>    pass
> 2. after
> (goto-char (treesit--navigate-thing (point) 1 'end treesit-defun-type-regexp))
> 
> def premain():
>    pass
> def main():
>    pass|
> 
> cursor in the end of the second function
> 
> 3. but if you move cursor to the beginning of pass
> 
> def premain():
>    |pass
> def main():
>    pass
> 
> 4.  after
> (goto-char (treesit--navigate-thing (point) 1 'end treesit-defun-type-regexp))
> 
> def premain():
>    pass|
> def main():
>    pass
> 
> This behavior does not affected by the fix proposed in the first message.

In the first case, treesit--navigate-thing returns a non-nil NEXT node. And treesit—navigate will prioritize NEXT node over PARENT node (because it assumes PARENT node always encloses NEXT node). Fixing the original problem should fix this too.

Yuan