Subject: bug#59853: 30.0.50; tree-sitter modes have unexpected beginning-of-defun behavior

From: Yuan Fu <[email-masked]>
Date: Tue, 20 Dec 2022 21:24:32 -0800

Theodor Thornhill <[email-masked]> writes:

> Brian Leung <[email-masked]> writes:
>
>> Theodor Thornhill <[email-masked]> writes:
>>
>>>> 2. When point is anywhere in the first line of the class 
>>>> declaration, mark-defun highlights "void otherMethod()", 
>>>> instead 
>>>> of the entire class declaration.
>>>
>>> Yeah, I think I've fixed this in a patch I just submitted.
>>
>> Which commit are you referring to?
>>
>
> I believe it was the one I included as a patch here.
>
>>>> 3a. When point is at the [*] in between someMethod and 
>>>> otherMethod, narrow-to-defun captures "void otherMethod()". I 
>>>> feel 
>>>> that since the methods inside the interface declaration have no 
>>>> bodies, it makes more sense to capture the entire interface 
>>>> definition if point is at [*].
>>>
>>> Maybe, but I don't believe this is wrong either.
>>
>> Let me rephrase my request. Consider the following example:
>>
>>> class Cow implements Animal {
>>>   public void animalSound() {
>>>     // The body of animalSound() is provided here
>>>     System.out.println("The cow says: moo");
>>>   }
>>>
>>> [*]
>>>
>>>   public void sleep() {
>>>     // The body of sleep() is provided here
>>>     System.out.println("Zzz");
>>>   }
>>> }
>>
>> Both the methods have bodies. If point is at the [*], I would like 
>> for narrow-to-defun to capture the entire class declaration, since 
>> point is not really contained in either method. (For this 
>> particular example, java-mode presently agrees with java-ts-mode.)
>>
>> Is there a clean way of ensuring that, when point lies between 
>> (and is not contained in) those two methods, point is not treated 
>> as if it were in one of those methods' tree-sitter nodes?
>>
>
> I understand.  I think that either we need to tweak the
> treesit-defun-type-regexp or make use of something like:
>
> (treesit-node-on (point) (point)) in the code that searches for
> beginning/end-of-defun.
>
> That code should return what you want, right?
>
>>>> 3b. Arguably, even if point were on the method declarations, we 
>>>> might still want to (as plain java-mode does) capture the 
>>>> entire 
>>>> interface definition, since body-less method declarations don't 
>>>> feel especially defun-like.
>>>
>>> Maybe.  Can you try applying the below patch and see if this 
>>> changes
>>> anything for you?
>>
>> It captures the entire interface definition only when I remove 
>> "method_declaration" (which we probably want to keep) from the 
>> regexp.
>
> Yeah.  But I believe Yuan is cooking on some code wrt
> beginning/end-of-defun, so maybe we should just wait and see what he
> comes up with.

I’ve cooked and served said code. Now beginning/end-of-defun should work
reliably, and you can configure its behavior with treesit-defun-tactic.

I also fixed c-ts-mode-indent-defun with the new defun functions.

Yuan