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#74963: Ambiguous treesit named and anonymous nodes in ruby-ts-mode Date: Mon, 23 Dec 2024 19:02:28 -0800 Message-ID: <4DFCFBC8-A353-48F2-8257-D6CFB155A030@gmail.com> References: <87plox4mtp.fsf@masteringemacs.org> <877cay1lqt.fsf@masteringemacs.org> <86frpma06f.fsf@gnu.org> <86ikueiekp.fsf@mail.linkov.net> <86ed4zg1cc.fsf@mail.linkov.net> <87zflac68t.fsf@mail.linkov.net> <87jzcdlxdp.fsf@mail.linkov.net> <87o71jocgs.fsf@mail.linkov.net> <5192B278-66C0-48AE-B881-E57CCBB6B501@gmail.com> <87frmtbc9z.fsf@mail.linkov.net> <86bjxh1h86.fsf@gnu.org> <87y10l8h6k.fsf@mail.linkov.net> <87ldwl8g60.fsf@mail.linkov.net> <87wmg53rdj.fsf@mail.linkov.net> <87a5d0n651.fsf@mail.linkov.net> <87zfktphks.fsf@mail.linkov.net> <87ttb0rwni.fsf_-_@mail.linkov.net> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3776.700.51\)) 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="12666"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Dmitry Gutov , 74963@debbugs.gnu.org To: Juri Linkov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Dec 24 04:04:15 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 1tPvDT-000381-0f for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 24 Dec 2024 04:04:15 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tPvDJ-0000bv-8s; Mon, 23 Dec 2024 22:04:05 -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 1tPvDH-0000b2-87 for bug-gnu-emacs@gnu.org; Mon, 23 Dec 2024 22:04:03 -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 1tPvDG-0003GL-Tu for bug-gnu-emacs@gnu.org; Mon, 23 Dec 2024 22:04:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=References:Date:In-Reply-To:From:Mime-Version:To:Subject; bh=wYDaIglBLzpyky0WSJcJC5BpoMhaHwuwidy490i9sVg=; b=ZyGlWQfn/NVHTHyGzrj5XBNHF09/dVUwMGIm4AZJy0GhC7Nu4/AVNnkQB3nSwUnL3Y/kpi9EUfvv4janblF+TWjDkCqnuvDRCAR7ZCvUV3q3mJZ8JDBz28R9cMZfhspIPbG/9vVf4KSX9l+wlFDGiUF+bhic1CHK8Nk9IWhezmPqBKaO7/W8Mku7fl6Nli6rOkagRzsgLY1qTyfWaSSpbCre15POabZ93BV7FZixcoHa/01Zn6B+LcRp4FBOtXREXms30tiR8fGMbP27KfLj+C2s1XWitBcMV8tQvs1qj1OFnnRQsEQx/pYj5B1/2Q2QO1V2DuhUc6KzNQBiGW0L/Q==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tPvDG-0003RC-OR for bug-gnu-emacs@gnu.org; Mon, 23 Dec 2024 22:04: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: Tue, 24 Dec 2024 03:04:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 74963 X-GNU-PR-Package: emacs Original-Received: via spool by 74963-submit@debbugs.gnu.org id=B74963.173500942513171 (code B ref 74963); Tue, 24 Dec 2024 03:04:02 +0000 Original-Received: (at 74963) by debbugs.gnu.org; 24 Dec 2024 03:03:45 +0000 Original-Received: from localhost ([127.0.0.1]:58816 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tPvCy-0003QM-Kn for submit@debbugs.gnu.org; Mon, 23 Dec 2024 22:03:45 -0500 Original-Received: from mail-pl1-f172.google.com ([209.85.214.172]:44395) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tPvCw-0003QB-Cx for 74963@debbugs.gnu.org; Mon, 23 Dec 2024 22:03:43 -0500 Original-Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2164b662090so41361875ad.1 for <74963@debbugs.gnu.org>; Mon, 23 Dec 2024 19:03:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735009362; x=1735614162; darn=debbugs.gnu.org; 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=wYDaIglBLzpyky0WSJcJC5BpoMhaHwuwidy490i9sVg=; b=N7dgwjO1DkA7nLGEOsLGA3aMoLvJqSd3nNMhDTzQMAvz4wg9GfuCsdiyGC1h+GOkOE kOHrPcb2z162x5JtX0ovlLXLbL2qMAd6U8u7jtunltt6kNdU2xHJYUc3D+DJoVzwnP/k IyfHOPr3ufMQ0B1DQhG1TdTn4kD50wI8GTcjhjF5BbkKFNOpOWTl6tMKaskknjZvQelB ZIQWivvGkKEF7v4dguMWJXc5mpLG3J1WKTHZ9GTW90pqLV58Ad931bYXfU35zOjlKZYf nnENSfNzgWK1p0MQz0qE9GRTyOQ6KEPP2VEqlclE5mLxjTPfpYlRjdGImEbvnvY1TziN bi5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735009362; x=1735614162; 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=wYDaIglBLzpyky0WSJcJC5BpoMhaHwuwidy490i9sVg=; b=boneZFEgYwoUDvSdktRX3N+y9eaJ9EaPF3zEORc0UWVE89N3Gio6TYLJsCJSheyLWi BHM04VndLZYrjXQa+Gy0xd3lyoXRfUV1LKT0VNl4DKgISQNIAeZJ4/y/64Is+/AEQC/S 4+5bhC5UfHzMPtiVwCl+DDeJ9guVO58tn046GQqHvrYv8Kb03Y9t7XecvYG8SwXPNaV4 m0nzkUmFUV2FuASSQbf8f3JIEyfaplGpq84UTJtt9FayUmalBXTd4Wt9xA2l+cHMkgyq LM0Cu8tA3QjoMCkRRJ2FnRaLcOLFuzTuRAgKBzTovF7kcc4WwE1HuhNYTniFbe+HkV6B j81A== X-Gm-Message-State: AOJu0YzHsBSs73rO3aiSZHo+mvP+3woRAJe3oPn4dpXl3Qpo+dnI7cvm L0AnjMq7bKPoUS3gHc20zRopPuzZ3GLowuta1cl3kh50y9oJyAjl X-Gm-Gg: ASbGncu6cJADL4yawyxer3AY2fxDFRB1mzUUcrA9gbUgpXEfQxnaTamqEBnP1Q2CVzw I3UcFCjnJQhUlrfVa+fXijroshMrjYpIO3wZXvLwZV2WBKzVmnrewvuTu9KA3Gxpl3iOLhzi3ME 9RAXUI/KKBXNy3sBMPn4B5NCG8+uNHOinQ7HT06gE8nHiGK21ADlQf3OGB7Qk2FPgKdv2+Dkskx /7rzmfQcm+kDkCmgCKi4HmREmTEUuyCrSU6w4A66OVCIV3n81uDJaedh8s+QhbK0LfrogZzto1B 1ysw X-Google-Smtp-Source: AGHT+IFDZd2wC1jpBMpmnvzTO5jmMoks73Oh2oCO23bJ8BqHKvLzR3XJPyUn1+A3S9wY6YtbnU5vMg== X-Received: by 2002:a17:903:32c2:b0:215:7b06:90ca with SMTP id d9443c01a7336-219e6ea248emr189077125ad.17.1735009360631; Mon, 23 Dec 2024 19:02:40 -0800 (PST) Original-Received: from smtpclient.apple ([2601:646:8f81:6120:1d98:6810:9846:b152]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc9f625csm78441365ad.208.2024.12.23.19.02.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Dec 2024 19:02:40 -0800 (PST) In-Reply-To: <87ttb0rwni.fsf_-_@mail.linkov.net> X-Mailer: Apple Mail (2.3776.700.51) 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:297675 Archived-At: > On Dec 18, 2024, at 11:18=E2=80=AFPM, Juri Linkov = wrote: >=20 > [This is a separate bug report from bug#73404] >=20 >>> While testing treesit-forward-sexp-list, I discovered that >>> thing-navigation functions are not restricted to named nodes. >>>=20 >>> I wonder if there a reason to find anonymous nodes as things? >>=20 >> We should rather ask is there any reason to not find anonymous nodes >> as things? Even ruby-ts-mode defines a bunch of anonymous nodes as >> sexp, no? In any case, excluding anonymous nodes from things = doesn=E2=80=99t >> sound right. >=20 > Indeed, there are many anonymous nodes used in ruby-ts-mode. >=20 >>> The problem was found with the node "unless" in Ruby: >>>=20 >>> unless cond >>> a +=3D 1 >>> else >>> b -=3D 1 >>> end >>>=20 >>> Here the named node 'unless' has exactly the same name >>> as the anonymous node with the text "unless": >>>=20 >>> (unless "unless" condition: (identifier) >>=20 >> I feel like Ruby=E2=80=99s grammar should call the named node = something else, >> like unless_statement. >=20 > Agreed, the problem is that nodes defined in Ruby=E2=80=99s grammar > are too ambiguous. There are more such nodes with the same name > for named and anonymous: "if", "while", "until", etc. >=20 >>> Finding anonymous nodes breaks forward-sexp when point is on = "unless": >>>=20 >>> un-!-less cond >>> a +=3D 1 >>> else >>> b -=3D 1 >>> end >>>=20 >>> because (treesit-thing-at (point) 'sexp t) finds >>>=20 >>> # >>>=20 >>> instead of >>>=20 >>> # >>>=20 >>> Also this breaks backward-sexp and backward-up-list >>> because treesit--thing-sibling finds >>> the anonymous node "unless" as a previous sibling >>> instead of the named node 'unless' as a parent. >>>=20 >>> Would the right solution be to check if the found thing >>> is a named node? With something like: >>>=20 >>> diff --git a/lisp/treesit.el b/lisp/treesit.el >>> index 18200acf53f..9ad879ee40c 100644 >>> --- a/lisp/treesit.el >>> +++ b/lisp/treesit.el >>> @@ -2711,6 +2774,7 @@ treesit--thing-sibling >>> (lambda (n) (>=3D (treesit-node-start n) pos)))) >>> (iter-pred (lambda (node) >>> (and (treesit-node-match-p node thing t) >>> + (treesit-node-check node 'named) >>> (funcall pos-pred node)))) >>> (sibling nil)) >>> (when cursor >>> @@ -2760,6 +2824,7 @@ treesit-thing-at >>> (let* ((cursor (treesit-node-at pos)) >>> (iter-pred (lambda (node) >>> (and (treesit-node-match-p node thing t) >>> + (treesit-node-check node 'named) >>> (if strict >>> (< (treesit-node-start node) pos) >>> (<=3D (treesit-node-start node) pos)) >>=20 >> A better solution IMO is to add some way to distinguish between named = and >> anonymous nodes. I can think of two ways, either add =E2=80=9Cand=E2=80= =9D and >> =E2=80=9Cnamed/anonymous=E2=80=9D predicate, so (and named = =E2=80=9Cunless=E2=80=9D) only matches the named >> =E2=80=9Cunless=E2=80=9D node; or we add a special syntax such that = =E2=80=9C(unless)=E2=80=9D only matches >> named nodes, and =E2=80=9C\=E2=80=9Dunless\=E2=80=9D=E2=80=9D only = matches anonymous nodes. >=20 > Either predicate or a special syntax is welcome. >=20 > This would be more handy than writing a lambda with implicit calls > of treesit-node-check. I=E2=80=99ll go with the (and named =E2=80=9Cunless=E2=80=9D) route = because after thinking about it more, =E2=80=9C(unless)=E2=80=9D will be = hard to work with because the string predicate is actually a regexp. Yuan=