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#68899: Treesitter's forward-sexp-function Date: Sat, 3 Feb 2024 21:35:00 -0800 Message-ID: <981BC2F8-9B7F-40AC-9C1A-C995A71F5C97@gmail.com> References: Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.700.6\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_930FBEFD-7B64-4765-B372-875E9588D84D" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9478"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 68899@debbugs.gnu.org To: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Feb 04 06:36:04 2024 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 1rWVAh-0002Lg-7J for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 04 Feb 2024 06:36:03 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rWVAV-0001MG-SL; Sun, 04 Feb 2024 00:35:51 -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 1rWVAV-0001M1-2W for bug-gnu-emacs@gnu.org; Sun, 04 Feb 2024 00:35:51 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rWVAU-0006jq-Qm for bug-gnu-emacs@gnu.org; Sun, 04 Feb 2024 00:35:50 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rWVAg-0003W1-Hj for bug-gnu-emacs@gnu.org; Sun, 04 Feb 2024 00:36: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: Sun, 04 Feb 2024 05:36:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68899 X-GNU-PR-Package: emacs Original-Received: via spool by 68899-submit@debbugs.gnu.org id=B68899.170702493213473 (code B ref 68899); Sun, 04 Feb 2024 05:36:02 +0000 Original-Received: (at 68899) by debbugs.gnu.org; 4 Feb 2024 05:35:32 +0000 Original-Received: from localhost ([127.0.0.1]:48051 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rWVAB-0003VE-OA for submit@debbugs.gnu.org; Sun, 04 Feb 2024 00:35:32 -0500 Original-Received: from mail-oi1-x22e.google.com ([2607:f8b0:4864:20::22e]:60425) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rWVAA-0003V1-0V for 68899@debbugs.gnu.org; Sun, 04 Feb 2024 00:35:30 -0500 Original-Received: by mail-oi1-x22e.google.com with SMTP id 5614622812f47-3bba50cd318so2884280b6e.0 for <68899@debbugs.gnu.org>; Sat, 03 Feb 2024 21:35:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707024912; x=1707629712; darn=debbugs.gnu.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=BADZerMJY7xRXhef2T/iE6A72mg2U5q+P9GgBSVw4Eo=; b=k00m35SmYmOoJ4533cd/KNQmeOYpdJVjCmPtTfQFgPqQ39ePI3oo4QR1H4VV+GZrpF a8KutTBtI9XxKOsDjSfW0RjLJDmE1A7rgwRhm+oaro/iSpCHAAerUw1+1WrlF2Idf2Ae XGTaldLbkbd8/SFYCEbab5LVc3Z/h6wfS/yPHm2wLDX/gUbEqEKFm3dl9mdSGKK06g4Z FW4fMl2DfGW4a8JvvkNuqjRZPOlgTUQx2p1vrYowGqAsnDPvSwrOqgAqKe3pVQ8sB/0x JJ4CBlG3SS0BbG0VhMVZNmobiWUJe80Qg3P5CFXRGRX23kSAstQdgFICuPOgkMWiiV0M NyRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707024912; x=1707629712; 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=BADZerMJY7xRXhef2T/iE6A72mg2U5q+P9GgBSVw4Eo=; b=Frp6ErjmOOVb8Uk2Vfub8ytr4Qzp1X9j/lQ/wgjWb4zAqMvEfgaTfqZaMADTDlQN8o Nu4vT+21+YrBgOt4UDupuVG2lxrgyST6b2MvlX+FGQz/VHMQiMpYct4ahxWUlLnBcjes 4rK3qAPieFF67jB5c9v+/3CxXg3ZNO/Z7VVCVMg+9wdVMAOrA65TeVsN2qWHCSIH7byd RpkYNaTsD6nZ5ralRfWlglYnt2nhuRwA01lM7IKwFVOt9EQZJDI5XF2ChDLC67rU2kho i3bOk8+SfifCnIpFGokmcHKMapate3dRsU9/wcQWMzO8E1znahOKqR/jg2O9NBtAcaDm WOGA== X-Gm-Message-State: AOJu0YzQyUuolbwH9WvIxarPd55ynRFNkRk8zMg5A4pSx191D1zoPUH+ YfFFVmFq3+XJoHKUMVV2rgT5xRKq1SYfCPY8IB0XjIzMMGich3oE X-Google-Smtp-Source: AGHT+IEz45gjUj7IxRvdtfTcf2adv53j0R3GyQwzIl2GMIXwN5Xfog1F6Ra1g4nLF9z9u1Pu7chalg== X-Received: by 2002:a05:6808:1a8c:b0:3be:5d77:cfa7 with SMTP id bm12-20020a0568081a8c00b003be5d77cfa7mr12894132oib.1.1707024912208; Sat, 03 Feb 2024 21:35:12 -0800 (PST) Original-Received: from smtpclient.apple (172-117-161-177.res.spectrum.com. [172.117.161.177]) by smtp.gmail.com with ESMTPSA id j17-20020a170902c3d100b001d92f2129dasm3962663plj.233.2024.02.03.21.35.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 03 Feb 2024 21:35:11 -0800 (PST) In-Reply-To: X-Mailer: Apple Mail (2.3731.700.6) 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:279392 Archived-At: --Apple-Mail=_930FBEFD-7B64-4765-B372-875E9588D84D Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Feb 2, 2024, at 4:57 PM, Jo=C3=A3o T=C3=A1vora = wrote: >=20 > On Sat, Feb 3, 2024 at 12:42=E2=80=AFAM Jo=C3=A3o T=C3=A1vora = wrote: >=20 > > > This doesn't seem to break tests, assuming it's not in these 3 = there > > > were skipped because I don't have the grammar installed. > > > > Despite that, I think it's still wrong :-/ Now it moves too much, > > i.e. it never stops moving. >=20 > This looks more promising. Works well in my tests. >=20 > diff --git a/lisp/treesit.el b/lisp/treesit.el > index c6b9d8ff4bc..cad7497fb74 100644 > --- a/lisp/treesit.el > +++ b/lisp/treesit.el > @@ -2579,9 +2579,12 @@ treesit--navigate-thing > (setq parent (treesit-node-top-level parent thing t) > prev nil > next nil)) > - ;; If TACTIC is `restricted', the implementation is very = simple. > + ;; If TACTIC is `restricted', the implementation is = reasonably simple. > (if (eq tactic 'restricted) > - (setq pos (funcall advance (if (> arg 0) next prev))) > + (setq pos (funcall advance (cond ((and (null next) = (null prev)) > + parent) > + ((> arg 0) next) > + (t prev)))) > ;; For `nested', it's a bit more work: > ;; Move... > (if (> arg 0) Thanks for looking into this, Joao. IME a very useful characteristic of = forward-sexp is that it stays in the same =E2=80=9Clevel=E2=80=9D and = doesn=E2=80=99t go up automatically when there=E2=80=99s no siblings = (when there=E2=80=99s a closing delimiter). Eg, in an Elisp buffer, = forward-sexp stops at the closing parenthesis, in C, it should stop at = the closing bracket. Also you don=E2=80=99t want to check for prev when moving forward, and = vice versa, ie, we don=E2=80=99t want to check (null next) and (null = prev) together. So, how do you like this patch: Yuan --Apple-Mail=_930FBEFD-7B64-4765-B372-875E9588D84D Content-Disposition: attachment; filename=forward-sexp.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="forward-sexp.patch" Content-Transfer-Encoding: quoted-printable =46rom=200afe667244caba04f061270cd2fc052ff6021130=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20Yuan=20Fu=20=0ADate:=20Sat,=20= 3=20Feb=202024=2021:24:29=20-0800=0ASubject:=20[PATCH]=20Improve=20= treesit-forward-sexp=20behavior=20for=20leaf=20nodes=0A=20(bug#68899)=0A=0A= treesit-forward-sexp=20uses=20treesit--navigate-thing=20with=20= 'restricted'=0Atactic.=20=20In=20this=20tactic=20we=20don't=20move=20= over=20the=20parent=20thing.=20=20However,=0Athis=20makes=20forward-sexp=20= useless=20for=20symbols=20when=20point=20is=20in=20the=0Asymbol=20rather=20= than=20at=20the=20beginning=20of=20it:=20in=20that=20case,=20the=20= symbol=20is=0Aconsidered=20parent=20and=20treesit-forward-sexp=20won't=20= move=20to=20the=20end=20of=0Ait.=0A=0ATo=20solve=20that,=20we=20allow=20= to=20move=20across=20the=20parent=20even=20in=20'restricted'=0Amode=20if=20= the=20parent=20is=20a=20leaf=20node.=0A=0AHere,=20"leaf=20node"=20is=20= defined=20as=20"doesn't=20have=20named=20child=20node",=20i.e.,=0Aif=20= the=20node=20has=20only=20anonymous=20child=20nodes,=20it's=20still=20= considered=20a=0Aleaf=20node.=20=20I=20don't=20know=20how=20useful=20= this=20heuristic=20is,=20but=20it=20feels=0Alike=20a=20good=20idea.=0A=0A= *=20lisp/treesit.el=20(treesit--navigate-thing):=20Move=20over=20parent=20= in=0A'restricted'=20tactic=20if=20the=20parent=20is=20a=20leaf=20node.=0A= ---=0A=20lisp/treesit.el=20|=2016=20++++++++++++++--=0A=201=20file=20= changed,=2014=20insertions(+),=202=20deletions(-)=0A=0Adiff=20--git=20= a/lisp/treesit.el=20b/lisp/treesit.el=0Aindex=20fab2ddd88e6..bf6b8dffde4=20= 100644=0A---=20a/lisp/treesit.el=0A+++=20b/lisp/treesit.el=0A@@=20= -2662,9=20+2662,21=20@@=20treesit--navigate-thing=0A=20=20=20=20=20=20=20= =20=20=20=20=20=20(setq=20parent=20(treesit-node-top-level=20parent=20= thing=20t)=0A=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= prev=20nil=0A=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= next=20nil))=0A-=20=20=20=20=20=20=20=20=20=20;;=20If=20TACTIC=20is=20= `restricted',=20the=20implementation=20is=20very=20simple.=0A+=20=20=20=20= =20=20=20=20=20=20;;=20If=20TACTIC=20is=20`restricted',=20the=20= implementation=20is=20simple.=0A+=20=20=20=20=20=20=20=20=20=20;;=20In=20= principle=20we=20don't=20go=20to=20parent's=20beg/end=20for=0A+=20=20=20=20= =20=20=20=20=20=20;;=20`restricted'=20tactic,=20but=20if=20the=20parent=20= is=20a=20leaf=20node=0A+=20=20=20=20=20=20=20=20=20=20;;=20(e.g.,=20a=20= symbol),=20we=20do,=20lest=20you=20can't=20go=20to=20the=20end=20of=0A+=20= =20=20=20=20=20=20=20=20=20;;=20a=20symbol=20with=20`forward-sexp'=20= when=20point=20is=20in=20the=20symbol=0A+=20=20=20=20=20=20=20=20=20=20= ;;=20rather=20than=20at=20the=20beg=20of=20it.=0A=20=20=20=20=20=20=20=20= =20=20=20(if=20(eq=20tactic=20'restricted)=0A-=20=20=20=20=20=20=20=20=20= =20=20=20=20=20(setq=20pos=20(funcall=20advance=20(if=20(>=20arg=200)=20= next=20prev)))=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20(let=20= ((restricted-parent=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20(and=20(not=20(treesit-node-child=20parent=200=20t))=0A+=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= parent)))=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20(setq=20pos=20= (funcall=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20advance=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20(if=20(>=20arg=200)=0A+=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20(or=20next=20restricted-parent)=0A+=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20(or=20prev=20= restricted-parent)))))=0A=20=20=20=20=20=20=20=20=20=20=20=20=20;;=20For=20= `nested',=20it's=20a=20bit=20more=20work:=0A=20=20=20=20=20=20=20=20=20=20= =20=20=20;;=20Move...=0A=20=20=20=20=20=20=20=20=20=20=20=20=20(if=20(>=20= arg=200)=0A--=20=0A2.41.0=0A=0A= --Apple-Mail=_930FBEFD-7B64-4765-B372-875E9588D84D Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii --Apple-Mail=_930FBEFD-7B64-4765-B372-875E9588D84D--