From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#74963: Ambiguous treesit named and anonymous nodes in ruby-ts-mode Date: Thu, 19 Dec 2024 09:18:37 +0200 Organization: LINKOV.NET Message-ID: <87ttb0rwni.fsf_-_@mail.linkov.net> 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> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="20457"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/31.0.50 (x86_64-pc-linux-gnu) Cc: Yuan Fu , Dmitry Gutov To: 74963@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Dec 19 08:21: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 1tOAqG-0005Af-Bl for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 19 Dec 2024 08:21:04 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tOApK-0004YE-7Y; Thu, 19 Dec 2024 02:20:06 -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 1tOApH-0004Vy-VE for bug-gnu-emacs@gnu.org; Thu, 19 Dec 2024 02:20:04 -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 1tOApH-0001uK-KN for bug-gnu-emacs@gnu.org; Thu, 19 Dec 2024 02:20:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:References:From:To:Subject; bh=pmJqTTAPztCb7VU9mUhDsbCfuMKxO6DDSLa7rO0zwNw=; b=gr3s8Ovx3fUIbtdhBV7QsgcCnyLZUullBLly9yVQPKW/dcqmA6otr7qs6qdvQQvPazcbllPfdbYj9Bvmu4ZOzUDLfGTLcfhPMla7ZwQjLng3df+KzExmnr4ZKEAlmzekLG9Dm6evONIUpzZsDWz4mL3UFflNJUJpj2GVA4tsY5PoRYlHDDrklhux6E1NErqZlUZghTf3G7X9C5MzH/HHYaaKpIRTa544pFqWZK9hRu5xvGvV0kEOJHLPPtyQepmAkds5kvbatutaMI0iTJzMF5JlI+J8YMgptJCcvN0RKkNFUgreRic/ZN6SjUvuRYgUoIaEBekcyDPViWzQ1yDzSQ==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tOApG-0006tM-Tg; Thu, 19 Dec 2024 02:20:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: casouri@gmail.com, dmitry@gutov.dev, bug-gnu-emacs@gnu.org Resent-Date: Thu, 19 Dec 2024 07:20:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 74963 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org X-Debbugs-Original-Xcc: Yuan Fu , Dmitry Gutov Original-Received: via spool by submit@debbugs.gnu.org id=B.173459280126473 (code B ref -1); Thu, 19 Dec 2024 07:20:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 19 Dec 2024 07:20:01 +0000 Original-Received: from localhost ([127.0.0.1]:37229 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tOApF-0006su-0Y for submit@debbugs.gnu.org; Thu, 19 Dec 2024 02:20:01 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:53546) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tOAp7-0006sW-0J for submit@debbugs.gnu.org; Thu, 19 Dec 2024 02:19:53 -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 1tOAp6-0004T5-Nf for bug-gnu-emacs@gnu.org; Thu, 19 Dec 2024 02:19:52 -0500 Original-Received: from relay4-d.mail.gandi.net ([2001:4b98:dc4:8::224]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tOAp4-0001nE-2j for bug-gnu-emacs@gnu.org; Thu, 19 Dec 2024 02:19:51 -0500 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id AB75AE0003 for ; Thu, 19 Dec 2024 07:19:46 +0000 (UTC) X-GND-Sasl: juri@linkov.net Received-SPF: pass client-ip=2001:4b98:dc4:8::224; envelope-from=juri@linkov.net; helo=relay4-d.mail.gandi.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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:297363 Archived-At: [This is a separate bug report from bug#73404] >> While testing treesit-forward-sexp-list, I discovered that >> thing-navigation functions are not restricted to named nodes. >> >> I wonder if there a reason to find anonymous nodes as things? > > 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’t > sound right. Indeed, there are many anonymous nodes used in ruby-ts-mode. >> The problem was found with the node "unless" in Ruby: >> >> unless cond >> a += 1 >> else >> b -= 1 >> end >> >> Here the named node 'unless' has exactly the same name >> as the anonymous node with the text "unless": >> >> (unless "unless" condition: (identifier) > > I feel like Ruby’s grammar should call the named node something else, > like unless_statement. Agreed, the problem is that nodes defined in Ruby’s grammar are too ambiguous. There are more such nodes with the same name for named and anonymous: "if", "while", "until", etc. >> Finding anonymous nodes breaks forward-sexp when point is on "unless": >> >> un-!-less cond >> a += 1 >> else >> b -= 1 >> end >> >> because (treesit-thing-at (point) 'sexp t) finds >> >> # >> >> instead of >> >> # >> >> 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. >> >> Would the right solution be to check if the found thing >> is a named node? With something like: >> >> 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) (>= (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) >> (<= (treesit-node-start node) pos)) > > A better solution IMO is to add some way to distinguish between named and > anonymous nodes. I can think of two ways, either add “and” and > “named/anonymous” predicate, so (and named “unless”) only matches the named > “unless” node; or we add a special syntax such that “(unless)” only matches > named nodes, and “\”unless\”” only matches anonymous nodes. Either predicate or a special syntax is welcome. This would be more handy than writing a lambda with implicit calls of treesit-node-check.