Subject: Re: bug#65885: Acknowledgement (29.1.50; wrong behaviour of treesit-beginning-of-defun in c++-ts-mode)

From: Yang Yingchao <[email-masked]>
Date: Wed, 13 Sep 2023 08:41:27 +0800

>>> Date: Tue, 12 Sep 2023 12:41:32 +0800
>>> From:  Yang Yingchao via "Bug reports for GNU Emacs,
>>>  the Swiss army knife of text editors" <[email-masked]>
>>>
>>> It seams that there is an issue with the behavior of `treesit-beginning-of-defun` function in
>>> `c++-ts-mode` when there are static variables present in the source code. For instance, in the
>>> provided code snippet, if the cursor is initially placed on the line containing `int b`, and the
>>> "C-M-a" is pressed, the cursor is positioned on line 4 instead of line 1 where the beginning of
>>> the function is located.
>>>
>>> ,----
>>> | void func		// c-ts-mode: treesit-beginning-of-defun stops in this line,   RIGHT
>>> | {
>>> |   char * msg;
>>> |   static int a; // c++-ts-mode: treesit-beginning-of-defun stops in this line, WRONG
>>> |   int b;        // PUT CUSOR HERE
>>> | }
>>> `----
>>>
>>> it works in c-ts-mode works normally.
>>
>> If you want c++-ts-mode to work like c-ts-mode, set
>> treesit-defun-tactic to 'top-level'.  By default, we try to support
>> nested defuns in C++, but not in C.
>
>
> I apologize, but I realized that I have already asked this question before.
>
> However, setting 'treesit-defun-tactic' to 'top-level' could cause other
> issues, as seen in the following snippets:
>
>
> ,----
> | class Name {
> | public:
> |   Name()
> |   {
> |     // cursor here
> |   }
> |
> |   void func()
> |   {
> |     static int a;
> |   }
> |
> | };
> `----
>
> Suppose the cursor is in line 5, and "C-M-a" is pressed:
>
> - In 'c++-mode', the cursor will be moved to the start of the function 'Name()', which is the expected behavior.
> - In 'c++-ts-mode':
>   - With 'treesit-defun-tactic' set to 'nested', it behaves the same as in 'c++-mode'.
>   - With 'treesit-defun-tactic' set to 'top-level', the cursor is moved to the beginning of the class, i.e., line 1.
>
> Is it possible to make 'c++-ts-mode' behave the same as in 'c++-mode'?
>
> Regards.

Maybe I'm wrong, but I would like to suggest reconsidering the solution to the 'begging-of-defun' issue related to the static *variable*. It feels strange to me to fix this issue by changing the 'treesit-defun-tactic', which is meant to apply to a *function* literally...