GNU bug report logs - #65885
29.1.50; wrong behaviour of treesit-beginning-of-defun in c++-ts-mode

Previous Next

Package: emacs;

Reported by: yang.yingchao <at> qq.com

Date: Tue, 12 Sep 2023 04:22:02 UTC

Severity: normal

Found in version 29.1.50

To reply to this bug, email your comments to 65885 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#65885; Package emacs. (Tue, 12 Sep 2023 04:22:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to yang.yingchao <at> qq.com:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Tue, 12 Sep 2023 04:22:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Yang Yingchao <yang.yingchao <at> qq.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 29.1.50; wrong behaviour of treesit-beginning-of-defun in c++-ts-mode
Date: Tue, 12 Sep 2023 12:04:47 +0800

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65885; Package emacs. (Tue, 12 Sep 2023 04:43:02 GMT) Full text and rfc822 format available.

Message #8 received at 65885 <at> debbugs.gnu.org (full text, mbox):

From: Yang Yingchao <yang.yingchao <at> qq.com>
To: 65885 <at> debbugs.gnu.org
Subject: Re: bug#65885: Acknowledgement (29.1.50; wrong behaviour of
 treesit-beginning-of-defun in c++-ts-mode)
Date: Tue, 12 Sep 2023 12:41:32 +0800
[Message part 1 (text/plain, inline)]
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.

Regards.



On Tue, Sep 12 2023, help-debbugs <at> gnu.org (GNU bug Tracking System) wrote:

> Thank you for filing a new bug report with debbugs.gnu.org.
>
> This is an automatically generated reply to let you know your message
> has been received.
>
> Your message is being forwarded to the package maintainers and other
> interested parties for their attention; they will reply in due course.
>
> Your message has been sent to the package maintainer(s):
>  bug-gnu-emacs <at> gnu.org
>
> If you wish to submit further information on this problem, please
> send it to 65885 <at> debbugs.gnu.org.
>
> Please do not send mail to help-debbugs <at> gnu.org unless you wish
> to report a problem with the Bug-tracking system.



Regards,

-- *Yang Yingchao*

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65885; Package emacs. (Tue, 12 Sep 2023 12:29:02 GMT) Full text and rfc822 format available.

Message #11 received at 65885 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: yang.yingchao <at> qq.com
Cc: 65885 <at> debbugs.gnu.org
Subject: Re: bug#65885: Acknowledgement (29.1.50;
 wrong behaviour of treesit-beginning-of-defun in c++-ts-mode)
Date: Tue, 12 Sep 2023 15:27:35 +0300
> 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" <bug-gnu-emacs <at> gnu.org>
> 
> 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.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65885; Package emacs. (Wed, 13 Sep 2023 00:14:01 GMT) Full text and rfc822 format available.

Message #14 received at 65885 <at> debbugs.gnu.org (full text, mbox):

From: Yang Yingchao <yang.yingchao <at> qq.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 65885 <at> debbugs.gnu.org
Subject: Re: bug#65885: Acknowledgement (29.1.50; wrong behaviour of
 treesit-beginning-of-defun in c++-ts-mode)
Date: Wed, 13 Sep 2023 08:03:48 +0800
[Message part 1 (text/plain, inline)]
>> 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" <bug-gnu-emacs <at> gnu.org>
>>
>> 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.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65885; Package emacs. (Wed, 13 Sep 2023 00:49:02 GMT) Full text and rfc822 format available.

Message #17 received at 65885 <at> debbugs.gnu.org (full text, mbox):

From: Yang Yingchao <yang.yingchao <at> qq.com>
To: Eli Zaretskii <eliz <at> gnu.org>, 65885 <at> debbugs.gnu.org
Subject: Re: bug#65885: Acknowledgement (29.1.50; wrong behaviour of
 treesit-beginning-of-defun in c++-ts-mode)
Date: Wed, 13 Sep 2023 08:41:27 +0800
[Message part 1 (text/plain, inline)]
>>> 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" <bug-gnu-emacs <at> gnu.org>
>>>
>>> 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...

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65885; Package emacs. (Wed, 13 Sep 2023 12:14:02 GMT) Full text and rfc822 format available.

Message #20 received at 65885 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: yang.yingchao <at> qq.com, Yuan Fu <casouri <at> gmail.com>
Cc: 65885 <at> debbugs.gnu.org
Subject: Re: bug#65885: Acknowledgement (29.1.50; wrong behaviour of
 treesit-beginning-of-defun in c++-ts-mode)
Date: Wed, 13 Sep 2023 15:13:14 +0300
> From: Yang Yingchao <yang.yingchao <at> qq.com>
> 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" <bug-gnu-emacs <at> gnu.org>
> >>>
> >>> 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...

Yuan, any ideas or suggestions?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#65885; Package emacs. (Thu, 14 Sep 2023 00:23:01 GMT) Full text and rfc822 format available.

Message #23 received at 65885 <at> debbugs.gnu.org (full text, mbox):

From: Yang Yingchao <yang.yingchao <at> qq.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: Yuan Fu <casouri <at> gmail.com>, 65885 <at> debbugs.gnu.org
Subject: Re: bug#65885: Acknowledgement (29.1.50; wrong behaviour of
 treesit-beginning-of-defun in c++-ts-mode)
Date: Thu, 14 Sep 2023 08:17:56 +0800
[Message part 1 (text/plain, inline)]
>> From: Yang Yingchao <yang.yingchao <at> qq.com>
>> 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" <bug-gnu-emacs <at> gnu.org>
>> >>>
>> >>> 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...
>
> Yuan, any ideas or suggestions?

Just FYI, I made some modifications to 'treesit--navigate-thing' in my own branch to fix this specific issue:

,----
| 1 file changed, 8 insertions(+)
| lisp/treesit.el | 8 ++++++++
| 
| modified   lisp/treesit.el
| @@ -2092,6 +2092,14 @@ treesit--navigate-thing
|          (pcase-let
|              ((`(,prev ,next ,parent)
|                (treesit--things-around pos regexp pred)))
| +
| +          ;; YYC: Special handling for static variable inside defun...
| +          (when (and (equal (treesit-node-type prev)
| +                            "storage_class_specifier")
| +                     (equal (treesit-node-type (treesit-node-parent prev))
| +                            "declaration"))
| +            (setq prev nil))
| +
|            ;; When PARENT is nil, nested and top-level are the same, if
|            ;; there is a PARENT, make PARENT to be the top-level parent
|            ;; and pretend there is no nested PREV and NEXT.
| 
`----

This bug report was last modified 233 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.