From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.bugs Subject: bug#64321: 29.0.92; Tree-Sitter/which-function Narrow/Widen causes modified buffer Date: Tue, 27 Jun 2023 21:04:22 -0700 Message-ID: <8D52F265-1596-4D95-BFCD-75C897412AC2@gmail.com> References: Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.600.7\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_8481864E-C3BF-4916-9327-ED4D15B40C60" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="10427"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , 64321@debbugs.gnu.org To: Troy Brown Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Jun 28 06:05:32 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qEMQu-0002VW-GF for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 28 Jun 2023 06:05:32 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qEMQY-0004NT-01; Wed, 28 Jun 2023 00:05:10 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qEMQW-0004N3-2j for bug-gnu-emacs@gnu.org; Wed, 28 Jun 2023 00:05:08 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qEMQQ-0007nj-W0 for bug-gnu-emacs@gnu.org; Wed, 28 Jun 2023 00:05:07 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qEMQP-0006DO-Rg for bug-gnu-emacs@gnu.org; Wed, 28 Jun 2023 00:05:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Yuan Fu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 28 Jun 2023 04:05:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 64321 X-GNU-PR-Package: emacs Original-Received: via spool by 64321-submit@debbugs.gnu.org id=B64321.168792508323863 (code B ref 64321); Wed, 28 Jun 2023 04:05:01 +0000 Original-Received: (at 64321) by debbugs.gnu.org; 28 Jun 2023 04:04:43 +0000 Original-Received: from localhost ([127.0.0.1]:49854 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qEMQ6-0006Co-QZ for submit@debbugs.gnu.org; Wed, 28 Jun 2023 00:04:43 -0400 Original-Received: from mail-pj1-f48.google.com ([209.85.216.48]:56762) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qEMQ4-0006Cb-Ps for 64321@debbugs.gnu.org; Wed, 28 Jun 2023 00:04:41 -0400 Original-Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-262ef07be72so1798955a91.1 for <64321@debbugs.gnu.org>; Tue, 27 Jun 2023 21:04:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687925075; x=1690517075; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=RV3rQQikb/+EdG1rC1wHfF4Sj9ERQQFMJnGcGpVOC0o=; b=eI7J7DroePxjXcVR79SunhJ9uEr6yll/a8q2/Q5wzhJsNHRBEqOL42n4AW54HXUlS6 2IS5qtliR6DKN0rCKptDOaG7tgiOrOOtS7UjnQY+3OcuBqtdGnmYY728sOVBZQ/X/X9E rYvO9I6vyerRpXCxN8ZFrYwWcrMWsFs/QdNowkgBeE1XCnIGfGkPnnOsBgCDCcciBOUr XN+Z5W3y2CfxvUfNko91D7LkGKsFBtRujLTQ+FfORwt/27AyrTyLkXzjh/lRm2VJkaWT 8J54t5TMtK+Gv5eWmRSCCE44wPDLJqD2KOrRBPbS8/TbdKWFLJ8R3TcH2eV/vQVR42Ne t+Eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687925075; x=1690517075; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RV3rQQikb/+EdG1rC1wHfF4Sj9ERQQFMJnGcGpVOC0o=; b=SdwX8AdDDF5p0WBNuuYYzeB0ctQU58XdPa0dMM2UP5weDqksrmsJNM1NKOouSeLegH kQPdKSvBlxHXhSZwMM4doMFJirad2qCBgFj93lGhePoMIZuM6G7Krw9kdX6xv0VVBcq0 EAgVXxU15oF08UNIKRNZjNoJxsElvOhp9rifeWqZGevq9z1S47VDoLtOHnh94yMfluRR 1X25SZNGxDXweAdptLOho/j8+bNrCxwFHeJ2JKbHXTGfDmgPT/EBO3ik/RONmI80ZyeN 1LraBM1gDAHNN/sCnv3Awz6No7rZ3mHUGYhd/jo99dq193SYVew8c4Be5Dq0+Vn3Txeb vgug== X-Gm-Message-State: AC+VfDxWCdkikRJSadFuS9bCM31xoa893abBJdQPKpzeVAE3DoyiRkkG oHW6kuZAAQAH/tAMRXQZ27e7gJDPa8U= X-Google-Smtp-Source: ACHHUZ4VZqbP8zUTBegQZ3qfpSqDp18OVs5iCaQdFp4NbXCNN8xEXUAvMDjQIIIgBpcsXSUSTLnTOA== X-Received: by 2002:a17:90a:f689:b0:262:df1d:4535 with SMTP id cl9-20020a17090af68900b00262df1d4535mr6249780pjb.31.1687925074473; Tue, 27 Jun 2023 21:04:34 -0700 (PDT) Original-Received: from smtpclient.apple (cpe-172-117-161-177.socal.res.rr.com. [172.117.161.177]) by smtp.gmail.com with ESMTPSA id q34-20020a17090a1b2500b00262e485156esm5566697pjq.57.2023.06.27.21.04.33 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Jun 2023 21:04:34 -0700 (PDT) In-Reply-To: X-Mailer: Apple Mail (2.3731.600.7) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:264194 Archived-At: --Apple-Mail=_8481864E-C3BF-4916-9327-ED4D15B40C60 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Jun 27, 2023, at 7:23 PM, Troy Brown wrote: >=20 > 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. >=20 > To reproduce this, use a "hello_world.c" as follows: >=20 > --8<---------------cut here---------------start------------->8--- > #include >=20 > int main (void) > { > printf("Hello, world!\n"); > return 0; > } > --8<---------------cut here---------------end--------------->8--- >=20 > 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. >=20 > The following sequence of commands can be used to reproduce the issue: >=20 > 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 >=20 > 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. >=20 > I then repeated the same but also triggered a backtrace on the first > change as follows: >=20 > M-: (add-to-list 'first-change-hook #'backtrace) >=20 > With that in place, the following is the generated traceback: >=20 > backtrace() > treesit--font-lock-notifier(((1 . 21)) #) > 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(#) > 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=E2=80=99s very helpful. Narrowing and widening are transparent to tree-sitter, so when the = buffer widens, from tree-sitter=E2=80=99s POV, text are added to the = buffer. Which-func calling the tree-sitter current-defund function = ultimately leads to tree-sitter calling treesit=E2=80=94font-lock-notifier= which puts text properties on the =E2=80=9Cadded=E2=80=9D portion of = the buffer, which changed the modified status. treesit=E2=80=94font-lock-notifier should put text properties without = changing modified status, like what jit-lock does. This patch should fix = it. Yuan --Apple-Mail=_8481864E-C3BF-4916-9327-ED4D15B40C60 Content-Disposition: attachment; filename=silent.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="silent.patch" Content-Transfer-Encoding: quoted-printable =46rom=202c90ade09a4d52a583158cb9cacf665ac11e8387=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20Yuan=20Fu=20=0ADate:=20Tue,=20= 27=20Jun=202023=2020:58:34=20-0700=0ASubject:=20[PATCH]=20Tree-sitter=20= use=20with-silent-modifications=20like=20jit-lock=0A=20(bug#64321)=0A=0A= *=20lisp/treesit.el=20(treesit--font-lock-notifier):=20Use=0A= with-silent-modifications=20when=20marking=20modified=20text=20to=20be=20= fontified=0Aby=20jit-lock.=20=20This=20is=20what=20jit-lock=20itself=20= does.=0A---=0A=20lisp/treesit.el=20|=203=20++-=0A=201=20file=20changed,=20= 2=20insertions(+),=201=20deletion(-)=0A=0Adiff=20--git=20= a/lisp/treesit.el=20b/lisp/treesit.el=0Aindex=200e1d7931d49..04d460fdea4=20= 100644=0A---=20a/lisp/treesit.el=0A+++=20b/lisp/treesit.el=0A@@=20= -1072,7=20+1072,8=20@@=20treesit--font-lock-notifier=0A=20=20=20=20=20=20= =20(when=20treesit--font-lock-verbose=0A=20=20=20=20=20=20=20=20=20= (message=20"Notifier=20received=20range:=20%s-%s"=0A=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20(car=20range)=20(cdr=20range)))=0A-=20=20= =20=20=20=20(put-text-property=20(car=20range)=20(cdr=20range)=20= 'fontified=20nil))))=0A+=20=20=20=20=20=20(with-silent-modifications=0A+=20= =20=20=20=20=20=20=20(put-text-property=20(car=20range)=20(cdr=20range)=20= 'fontified=20nil)))))=0A=20=0A=20;;;=20Indent=0A=20=0A--=20=0A2.33.1=0A=0A= --Apple-Mail=_8481864E-C3BF-4916-9327-ED4D15B40C60 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii --Apple-Mail=_8481864E-C3BF-4916-9327-ED4D15B40C60--