Subject: bug#68664: 29.1.50; treesit defun commands broken with nested functions

From: Yuan Fu <[email-masked]>
Date: Tue, 23 Jan 2024 22:29:40 -0800

> On Jan 23, 2024, at 6:30 AM, Troy Brown <[email-masked]> wrote:
> 
> On Mon, Jan 22, 2024 at 7:32 PM Daniel Martín <[email-masked]> wrote:
>> 
>> Troy Brown <[email-masked]> writes:
>> 
>>> I've noticed that "defun" related treesit commands do not appear to work
>>> correctly when nested functions are involved.  I've seen this behavior
>>> in multiple languages and believe the problem is an issue in the
>>> treesit.el library.
>> 
>> Customize the treesit-defun-tactic variable to 'top-level to ignore
>> nested defuns in navigation commands.
> 
> But I don't want it to just go to the top-level, I want it to respect
> the current
> nesting level.  If I insert yet another level in the example, and
> point is within
> the second level of nesting, I want it to move to the beginning and end of that
> nested function (i.e., "secondLevel" in the sample below when point is on the
> call to innerFunction).  As mentioned in my original email, python-mode does
> respect the nesting level correctly, but python-ts-mode, and other "ts" modes
> that support nesting, don't respect it.

The behavior is expected. But I can see that it doesn’t match your expectations. The logic behind the current behavior is to first move between siblings in the same level; if there’s no sibling to move across anymore, move to the beginning/end of the immediate parent, and so on.

To get the behavior you want, we would need to add a fourth defun navigation tactic, in addition to the existing three: nested, top-level, and restricted.

If you are interested and able, maybe you can look into adding it to treesit--navigate-thing or treesit-beginning/end-of-defun?

Yuan