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

From: Troy Brown <[email-masked]>
Date: Tue, 23 Jan 2024 09:30:49 -0500

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.

--8<---------------cut here---------------start------------->8---
# -*- mode: python-ts -*-

def outerFunction(text):
    text = text

    def secondLevel(text):
        print(text)

        def innerFunction(text):
            print(text)

        innerFunction()

        def innerFunction2(text):
            print(text)

        innerFunction2()

    secondLevel()

--8<---------------cut here---------------end--------------->8---