GNU bug report logs - #64321
29.0.92; Tree-Sitter/which-function Narrow/Widen causes modified buffer

Previous Next

Package: emacs;

Reported by: Troy Brown <brownts <at> troybrown.dev>

Date: Wed, 28 Jun 2023 02:25:01 UTC

Severity: normal

Found in version 29.0.92

Done: Yuan Fu <casouri <at> gmail.com>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 64321 in the body.
You can then email your comments to 64321 AT debbugs.gnu.org in the normal way.

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#64321; Package emacs. (Wed, 28 Jun 2023 02:25:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Troy Brown <brownts <at> troybrown.dev>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Wed, 28 Jun 2023 02:25:02 GMT) Full text and rfc822 format available.

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

From: Troy Brown <brownts <at> troybrown.dev>
To: bug-gnu-emacs <at> gnu.org
Subject: 29.0.92;
 Tree-Sitter/which-function Narrow/Widen causes modified buffer
Date: Tue, 27 Jun 2023 22:23:52 -0400
This problem seems to manifest with multiple tree-sitter modes.  It
appears there is an interaction with which-function-mode and tree-sitter
which causes the buffer to be considered modified either after a narrow
or widen of the buffer.

To reproduce this, use a "hello_world.c" as follows:

--8<---------------cut here---------------start------------->8---
#include <stdio.h>

int main (void)
{
  printf("Hello, world!\n");
  return 0;
}
--8<---------------cut here---------------end--------------->8---

Running "emacs -Q" with the corresponding tree-sitter grammar library
installed.  Move into the main function and narrow-to-defun.  In some
modes, I've seen this cause the buffer to show as modified, however with
this example, it seems to occur after widening.

The following sequence of commands can be used to reproduce the issue:

C-x C-f ~/hello_world.c
M-x c-ts-mode
M-x which-function-mode
C-x n d
C-x n w

At this point, the modeline shows that the buffer is modified, and
running "M-: (buffer-modified-p)" indicates "t".  As previously
mentioned, this appears to only happen with tree-sitter modes when
which-function-mode is enabled in the buffer.

I then repeated the same but also triggered a backtrace on the first
change as follows:

M-: (add-to-list 'first-change-hook #'backtrace)

With that in place, the following is the generated traceback:

  backtrace()
  treesit--font-lock-notifier(((1 . 21)) #<treesit-parser for c>)
  treesit-buffer-root-node(c)
  treesit-node-at(68)
  treesit--things-around(68
"\\(?:class_specifier\\|enum_specifier\\|function_defi..."
c-ts-mode--defun-valid-p)
  treesit-thing-at-point(("\\(?:class_specifier\\|enum_specifier\\|function_defi..."
. c-ts-mode--defun-valid-p) top-level)
  treesit-defun-at-point()
  treesit-add-log-current-defun()
  c-ts-mode--emacs-current-defun-name()
  add-log-current-defun()
  which-function()
  which-func-update-1(#<window 3 on hello_world.c>)
  which-func-update()
  apply(which-func-update nil)
  timer-event-handler([t 0 0 500000 t which-func-update nil idle 0 nil])




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#64321; Package emacs. (Wed, 28 Jun 2023 04:05:01 GMT) Full text and rfc822 format available.

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

From: Yuan Fu <casouri <at> gmail.com>
To: Troy Brown <brownts <at> troybrown.dev>
Cc: Eli Zaretskii <eliz <at> gnu.org>, 64321 <at> debbugs.gnu.org
Subject: Re: bug#64321: 29.0.92; Tree-Sitter/which-function Narrow/Widen
 causes modified buffer
Date: Tue, 27 Jun 2023 21:04:22 -0700
[Message part 1 (text/plain, inline)]

> On Jun 27, 2023, at 7:23 PM, Troy Brown <brownts <at> troybrown.dev> wrote:
> 
> This problem seems to manifest with multiple tree-sitter modes.  It
> appears there is an interaction with which-function-mode and tree-sitter
> which causes the buffer to be considered modified either after a narrow
> or widen of the buffer.
> 
> To reproduce this, use a "hello_world.c" as follows:
> 
> --8<---------------cut here---------------start------------->8---
> #include <stdio.h>
> 
> int main (void)
> {
>  printf("Hello, world!\n");
>  return 0;
> }
> --8<---------------cut here---------------end--------------->8---
> 
> Running "emacs -Q" with the corresponding tree-sitter grammar library
> installed.  Move into the main function and narrow-to-defun.  In some
> modes, I've seen this cause the buffer to show as modified, however with
> this example, it seems to occur after widening.
> 
> The following sequence of commands can be used to reproduce the issue:
> 
> C-x C-f ~/hello_world.c
> M-x c-ts-mode
> M-x which-function-mode
> C-x n d
> C-x n w
> 
> At this point, the modeline shows that the buffer is modified, and
> running "M-: (buffer-modified-p)" indicates "t".  As previously
> mentioned, this appears to only happen with tree-sitter modes when
> which-function-mode is enabled in the buffer.
> 
> I then repeated the same but also triggered a backtrace on the first
> change as follows:
> 
> M-: (add-to-list 'first-change-hook #'backtrace)
> 
> With that in place, the following is the generated traceback:
> 
>  backtrace()
>  treesit--font-lock-notifier(((1 . 21)) #<treesit-parser for c>)
>  treesit-buffer-root-node(c)
>  treesit-node-at(68)
>  treesit--things-around(68
> "\\(?:class_specifier\\|enum_specifier\\|function_defi..."
> c-ts-mode--defun-valid-p)
>  treesit-thing-at-point(("\\(?:class_specifier\\|enum_specifier\\|function_defi..."
> . c-ts-mode--defun-valid-p) top-level)
>  treesit-defun-at-point()
>  treesit-add-log-current-defun()
>  c-ts-mode--emacs-current-defun-name()
>  add-log-current-defun()
>  which-function()
>  which-func-update-1(#<window 3 on hello_world.c>)
>  which-func-update()
>  apply(which-func-update nil)
>  timer-event-handler([t 0 0 500000 t which-func-update nil idle 0 nil])

Thanks for the backtrace, it’s very helpful.

Narrowing and widening are transparent to tree-sitter, so when the buffer widens, from tree-sitter’s POV, text are added to the buffer. Which-func calling the tree-sitter current-defund function ultimately leads to tree-sitter calling treesit—font-lock-notifier which puts text properties on the “added” portion of the buffer, which changed the modified status.

treesit—font-lock-notifier should put text properties without changing modified status, like what jit-lock does. This patch should fix it.

Yuan

[silent.patch (application/octet-stream, attachment)]
[Message part 3 (text/plain, inline)]


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

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

From: Eli Zaretskii <eliz <at> gnu.org>
To: Yuan Fu <casouri <at> gmail.com>
Cc: brownts <at> troybrown.dev, 64321 <at> debbugs.gnu.org
Subject: Re: bug#64321: 29.0.92; Tree-Sitter/which-function Narrow/Widen
 causes modified buffer
Date: Wed, 28 Jun 2023 15:01:07 +0300
> From: Yuan Fu <casouri <at> gmail.com>
> Date: Tue, 27 Jun 2023 21:04:22 -0700
> Cc: 64321 <at> debbugs.gnu.org,
>  Eli Zaretskii <eliz <at> gnu.org>
> 
> Thanks for the backtrace, it’s very helpful.
> 
> Narrowing and widening are transparent to tree-sitter, so when the buffer widens, from tree-sitter’s POV, text are added to the buffer. Which-func calling the tree-sitter current-defund function ultimately leads to tree-sitter calling treesit—font-lock-notifier which puts text properties on the “added” portion of the buffer, which changed the modified status.
> 
> treesit—font-lock-notifier should put text properties without changing modified status, like what jit-lock does. This patch should fix it.

Thanks.  If this solves the problem, please install in the emacs-29
release branch.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#64321; Package emacs. (Wed, 28 Jun 2023 16:26:02 GMT) Full text and rfc822 format available.

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

From: Troy Brown <brownts <at> troybrown.dev>
To: Yuan Fu <casouri <at> gmail.com>
Cc: Eli Zaretskii <eliz <at> gnu.org>, 64321 <at> debbugs.gnu.org
Subject: Re: bug#64321: 29.0.92; Tree-Sitter/which-function Narrow/Widen
 causes modified buffer
Date: Wed, 28 Jun 2023 12:25:02 -0400
On Wed, Jun 28, 2023 at 12:04 AM Yuan Fu <casouri <at> gmail.com> wrote:
>
> Narrowing and widening are transparent to tree-sitter, so when the buffer widens, from tree-sitter’s POV, text are added to the buffer. Which-func calling the tree-sitter current-defund function ultimately leads to tree-sitter calling treesit—font-lock-notifier which puts text properties on the “added” portion of the buffer, which changed the modified status.
>
> treesit—font-lock-notifier should put text properties without changing modified status, like what jit-lock does. This patch should fix it.
>

Thanks for the explanation and the supplied patch.  I can confirm that
this patch fixes the issue.




Reply sent to Yuan Fu <casouri <at> gmail.com>:
You have taken responsibility. (Wed, 28 Jun 2023 20:11:01 GMT) Full text and rfc822 format available.

Notification sent to Troy Brown <brownts <at> troybrown.dev>:
bug acknowledged by developer. (Wed, 28 Jun 2023 20:11:01 GMT) Full text and rfc822 format available.

Message #19 received at 64321-done <at> debbugs.gnu.org (full text, mbox):

From: Yuan Fu <casouri <at> gmail.com>
To: Troy Brown <brownts <at> troybrown.dev>
Cc: Eli Zaretskii <eliz <at> gnu.org>, 64321-done <at> debbugs.gnu.org
Subject: Re: bug#64321: 29.0.92; Tree-Sitter/which-function Narrow/Widen
 causes modified buffer
Date: Wed, 28 Jun 2023 13:09:41 -0700

> On Jun 28, 2023, at 9:25 AM, Troy Brown <brownts <at> troybrown.dev> wrote:
> 
> On Wed, Jun 28, 2023 at 12:04 AM Yuan Fu <casouri <at> gmail.com> wrote:
>> 
>> Narrowing and widening are transparent to tree-sitter, so when the buffer widens, from tree-sitter’s POV, text are added to the buffer. Which-func calling the tree-sitter current-defund function ultimately leads to tree-sitter calling treesit—font-lock-notifier which puts text properties on the “added” portion of the buffer, which changed the modified status.
>> 
>> treesit—font-lock-notifier should put text properties without changing modified status, like what jit-lock does. This patch should fix it.
>> 
> 
> Thanks for the explanation and the supplied patch.  I can confirm that
> this patch fixes the issue.

Cool, thanks, pushed.

Yuan



bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Thu, 27 Jul 2023 11:24:06 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 333 days ago.

Previous Next


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