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

From: Zubarev Dv <[email-masked]>
Date: Wed, 08 Nov 2023 23:37:45 +0300

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.

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.