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#61369: Problem with keeping tree-sitter parse tree up-to-date Date: Fri, 17 Feb 2023 14:32:22 -0800 Message-ID: <7751EE35-F5FF-418B-AF28-F1FF5ECEF3AE@gmail.com> References: <1AC63591-F4EF-411F-B554-7CD38B4B4888@gmail.com> <9c4e551b-42b3-8202-ccff-fb8170b616a6@yandex.ru> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.300.101.1.3\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38018"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Theodor Thornhill , 61369@debbugs.gnu.org To: Dmitry Gutov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Feb 17 23:33:17 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 1pT9I5-0009fS-Ma for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 17 Feb 2023 23:33:17 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pT9Hv-0007Gk-PV; Fri, 17 Feb 2023 17:33:07 -0500 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 1pT9Hr-0007GY-5k for bug-gnu-emacs@gnu.org; Fri, 17 Feb 2023 17:33:03 -0500 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 1pT9Hq-0002Fq-OL for bug-gnu-emacs@gnu.org; Fri, 17 Feb 2023 17:33:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pT9Hq-0004Sw-Io for bug-gnu-emacs@gnu.org; Fri, 17 Feb 2023 17:33:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Yuan Fu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 17 Feb 2023 22:33:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 61369 X-GNU-PR-Package: emacs Original-Received: via spool by 61369-submit@debbugs.gnu.org id=B61369.167667316417142 (code B ref 61369); Fri, 17 Feb 2023 22:33:02 +0000 Original-Received: (at 61369) by debbugs.gnu.org; 17 Feb 2023 22:32:44 +0000 Original-Received: from localhost ([127.0.0.1]:41812 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pT9HY-0004SO-H9 for submit@debbugs.gnu.org; Fri, 17 Feb 2023 17:32:44 -0500 Original-Received: from mail-pf1-f181.google.com ([209.85.210.181]:37409) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pT9HW-0004SA-OV for 61369@debbugs.gnu.org; Fri, 17 Feb 2023 17:32:43 -0500 Original-Received: by mail-pf1-f181.google.com with SMTP id 71so1438434pfy.4 for <61369@debbugs.gnu.org>; Fri, 17 Feb 2023 14:32:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=DzX11qRZn3+gIyZu5pHGhyOqZ3b+HlT7dzXzXFZMsVo=; b=RpvB+QzNo1VaKW6Jj84iMIe5hG6TzWpYMFJGTqM9YMEtn0a//rb4L3DIUaDp20gD7B /hQgXGEDJ/ORph7lgnM8Pwsu5vGKaE/2EaEZPDMKGVn0l8piHld4fBRnoBfohN0Uh9p8 MN3sKiccTUSA/wMpIoDizPxhGv7ek7tQeVxCHi9tWzcE3bA96Gf2u/prK+QAB8JmLsdQ Vm5b1+Hscw9GFlITud0Np1BDrJfbc1Y2hj/nVyhi5G7DI306Q47M/KLkAGKsSKBnimOS KIGrKkLcburEdHJ3wlg6G61aJlw40mDn90X+KDvJZcrHYLuBLiV4a4Y+kIVDszBmkQIC ZuXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DzX11qRZn3+gIyZu5pHGhyOqZ3b+HlT7dzXzXFZMsVo=; b=Ue7I1H/iMcEfGKjseBKOn3kAcOYeIyJJ/GD/YttPSr3+h97Aic1e/iRBBfIeLEIBxF 1Y/mr3Se9J7vd0Q7g/rsqMB/p8Steiv4OzmvcSNv7iz8EnKFqMjsmImm/bfBSJPfTNrk Z41V0N/T8V1RVUGut83iwtcwePzQkuSDHTOfuopP7g5sNWrnd9yC9kD5nzYjPh3p0Yxo pBFEKcYo9v/Hh7MNYRdWtkYYM1OJOVtcZ/sH0k10bV+RUE4m+stDXWsrR33YmEIY/EZz 6QoX+IGntpDK4rV+LG3ZyBwiMBqO/PvL3btmxWxa50OpiPJCo+w+HkSH4m7Z6s2/dSgu bzIg== X-Gm-Message-State: AO0yUKV3uObs1C21trUwW7kDK0XydUkfZQgWkxKHWJkqcbHW7OxwO5aJ cnF9GkBtwPwYL9kSX6o7Hx0= X-Google-Smtp-Source: AK7set+4ypCKfJfebiEXsv4DZHG/oXTX8u4JJQUlvv0je72RFpmDzfZCTb6Aax8ovuCQX8dtqW4IDA== X-Received: by 2002:aa7:95b2:0:b0:5a8:acca:d5ce with SMTP id a18-20020aa795b2000000b005a8accad5cemr2054780pfk.28.1676673156765; Fri, 17 Feb 2023 14:32:36 -0800 (PST) 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 17-20020aa79211000000b005abc0d426c4sm440276pfo.54.2023.02.17.14.32.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Feb 2023 14:32:36 -0800 (PST) In-Reply-To: X-Mailer: Apple Mail (2.3731.300.101.1.3) 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:255908 Archived-At: > On Feb 15, 2023, at 2:44 PM, Dmitry Gutov wrote: >=20 > On 15/02/2023 04:17, Dmitry Gutov wrote: >> Seems like treesit_record_change turns new_end_byte=3D69 into = new_end_byte=3D67 inside treesit_tree_edit_1. >> It seems to fail in this calculation: >> ptrdiff_t new_end_offset =3D (min (visible_end, >> max (visible_end, new_end_byte)) >> - visible_beg); >> because visible_end is still 68 there. It value gets updated later, = closer to the end of this function. >=20 > So FWIW the patch below fixes the problem. But I'm not sure about = change's clipping by the current restriction, or how it should be = handled exactly. >=20 > diff --git a/src/treesit.c b/src/treesit.c > index cab2f0d5354..9f15b88a8bd 100644 > --- a/src/treesit.c > +++ b/src/treesit.c > @@ -794,9 +794,7 @@ treesit_record_change (ptrdiff_t start_byte, = ptrdiff_t old_end_byte, > ptrdiff_t old_end_offset =3D (min (visible_end, > max (visible_beg, old_end_byte)) > - visible_beg); > - ptrdiff_t new_end_offset =3D (min (visible_end, > - max (visible_beg, new_end_byte)) > - - visible_beg); > + ptrdiff_t new_end_offset =3D max (visible_beg, new_end_byte) - = visible_beg; > eassert (start_offset <=3D old_end_offset); > eassert (start_offset <=3D new_end_offset); Thank you very much! I thought that clipping the change into the fixed = visible range, and rely on treesit_sync_visible_region to add back the = clipped =E2=80=9Ctail=E2=80=9D when we extend the visible range would be = equivalent to not clipping, but I guess clipping and re-adding affects = how incremental parsing works inside tree-sitter. I don=E2=80=99t think this change would have any adverse effect, because = if you think of it, inserting text in a narrowed region always extends = the region, rather than pushing text at the end out of the narrowed = region. So the right thing to do here is in fact not clipping = new_end_offset. I pushed this change. Yuan=