unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Yang Yingchao via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Yuan Fu <casouri@gmail.com>, 65885@debbugs.gnu.org
Subject: 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	[thread overview]
Message-ID: <tencent_653BAFD0E8734E6AF2C888BF820C50C06105@qq.com> (raw)
In-Reply-To: <83h6nynued.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 3510 bytes --]

>> From: Yang Yingchao <yang.yingchao@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@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.
| 
`----

      reply	other threads:[~2023-09-14  0:17 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <871qf4doit.fsf@qq.com>
     [not found] ` <handler.65885.B.169449251427125.ack@debbugs.gnu.org>
2023-09-12  4:41   ` bug#65885: Acknowledgement (29.1.50; wrong behaviour of treesit-beginning-of-defun in c++-ts-mode) Yang Yingchao via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-12 12:27     ` Eli Zaretskii
2023-09-13  0:03       ` Yang Yingchao via Bug reports for GNU Emacs, the Swiss army knife of text editors
     [not found]       ` <87wmwv9bhu.fsf@qq.com>
2023-09-13  0:41         ` Yang Yingchao via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-13 12:13           ` Eli Zaretskii
2023-09-14  0:17             ` Yang Yingchao via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tencent_653BAFD0E8734E6AF2C888BF820C50C06105@qq.com \
    --to=bug-gnu-emacs@gnu.org \
    --cc=65885@debbugs.gnu.org \
    --cc=casouri@gmail.com \
    --cc=eliz@gnu.org \
    --cc=yang.yingchao@qq.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).