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

From: Yuan Fu <[email-masked]>
Date: Tue, 7 Nov 2023 23:08:53 -0800

> On Nov 7, 2023, at 8:16 AM, Denis Zubarev <[email-masked]> wrote:
> 
> 
> Steps to reproduce:
> 1. emacs -Q
> 2. M-x find-file /tmp/t.py
> 3. paste to the buffer
> 
> def premain():
>    pa|ss
> def main():
>    pass
> 
> where | is cursor position for further steps
> 
> 4. M-x python-ts-mode
> 5. M-x eval-expression (treesit-defun-at-point)
> treesit-defun-at-point returns nil, but I expect that it will return
> a node for `premain`.
> If you move the cursor to the beginning of the word `pass` and try again
> it will return node for `premain` function.
> 
> If you eval `(treesit--things-around (point) treesit-defun-type-regexp)`,
> it returns  `(nil #<treesit-node function_definition ...> nil)`
> It seems that when cursor is not on the beginning of the node, treesit
> erroneously finds next sibling. 
> It prevents finding parent node correctly (premain function in this case).
> I managed to fix the problem by replacing 
> `(cursor (or (nth 0 result) (nth 1 result) node)` with ~(cursor node)~ - here 
> https://github.com/emacs-mirror/emacs/blob/12d1f33ceb37a2d0399af7e4054024c60a465ab8/lisp/treesit.el#L2383
> But I'm sure it is not the proper fix.

Thank you very much, Denis, your fix is actually the proper fix! But the problem is more complicated. I didn’t clearly define the (prev next parent) returned by treesit--things-around—maybe parent must enclose the prev and next node, and if there are prev and next node beyond the smallest parent node at point, they are not returned; maybe parent doesn’t have to enclose prev and next node, and we will include prev and next nodes in the return value as long as they exist.

It’s not clear which definition is more correct. I’m leaning towards the first. Maybe both are correct and useful and we should support both, hmm...

Yuan