GNU bug report logs -
#66989
30.0.50; tree-sitter: treesit-defun-at-point wrong behavior in python-ts-mode
Previous Next
Reported by: Denis Zubarev <dvzubarev <at> yandex.ru>
Date: Tue, 7 Nov 2023 16:20:02 UTC
Severity: normal
Found in version 30.0.50
Done: Yuan Fu <casouri <at> gmail.com>
Bug is archived. No further changes may be made.
Full log
Message #17 received at 66989 <at> debbugs.gnu.org (full text, mbox):
> On Nov 9, 2023, at 12:13 AM, Yuan Fu <casouri <at> gmail.com> wrote:
>
>
>
>> On Nov 8, 2023, at 12:37 PM, Zubarev Dv <dvzubarev <at> yandex.ru> 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
I’ve pushed to master four new functions: treesit--thing-prev, treesit--thing-next, treesit--thing-at, treesit-node-enclosed-p. And I updated other functions to use the new functions. Now your original example and the later ones should all work right.
Yuan
This bug report was last modified 1 year and 277 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.