From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Wilhelm Kirschbaum Newsgroups: gmane.emacs.bugs Subject: bug#58711: Treesit hangs when calling treesit-search-forward Date: Thu, 10 Nov 2022 22:43:31 +0200 Message-ID: References: <895A3C87-507A-4FC5-8BAE-B138B35CD40C@gmail.com> <46B629C0-1413-41CE-BDA8-CE14C12B4449@gmail.com> <3054006.Zh9iicr411@melissa.local> <106B8822-FE37-4590-BC77-5FB24FB0758C@gmail.com> <82A013FB-9A94-4E57-9FD2-AF8E892241A9@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="0000000000002e024d05ed23d4a6" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="37116"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 58711@debbugs.gnu.org To: Yuan Fu Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Nov 10 21:44:15 2022 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 1otEPG-0009OT-8B for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 10 Nov 2022 21:44:14 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1otEP6-0006Lh-Bw; Thu, 10 Nov 2022 15:44:04 -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 1otEP5-0006LX-B5 for bug-gnu-emacs@gnu.org; Thu, 10 Nov 2022 15:44: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 1otEP4-0008TU-VN for bug-gnu-emacs@gnu.org; Thu, 10 Nov 2022 15:44:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1otEP4-0003JQ-GD for bug-gnu-emacs@gnu.org; Thu, 10 Nov 2022 15:44:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Wilhelm Kirschbaum Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 10 Nov 2022 20:44:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 58711 X-GNU-PR-Package: emacs Original-Received: via spool by 58711-submit@debbugs.gnu.org id=B58711.166811303612712 (code B ref 58711); Thu, 10 Nov 2022 20:44:02 +0000 Original-Received: (at 58711) by debbugs.gnu.org; 10 Nov 2022 20:43:56 +0000 Original-Received: from localhost ([127.0.0.1]:44497 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1otEOx-0003Ix-2c for submit@debbugs.gnu.org; Thu, 10 Nov 2022 15:43:55 -0500 Original-Received: from mail-lf1-f42.google.com ([209.85.167.42]:46990) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1otEOr-0003Ig-32 for 58711@debbugs.gnu.org; Thu, 10 Nov 2022 15:43:53 -0500 Original-Received: by mail-lf1-f42.google.com with SMTP id bp15so5303377lfb.13 for <58711@debbugs.gnu.org>; Thu, 10 Nov 2022 12:43:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=floatpays.co.za; s=google; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Zs0ol0n/8TuaJtyJTctFMFkSp5mqoYeN9t4GJv8BXMU=; b=bDUq6LpN77NqGxYxAWKLOB256vSg2eZnumwY2mtKe/vUbS42H168RLwPe11oC1hIRw iDqnIqn1RLDgu22gavgxFysouSoVc8LWJEEwxBkvDSLSY4YwiZ2URGbAtJfwZQ2P6MkL X33TCUX9d3ayzMZ1WKj1zF+Eaz5XlP1YK8gUw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Zs0ol0n/8TuaJtyJTctFMFkSp5mqoYeN9t4GJv8BXMU=; b=4d1J3+bLrNKVg68hY8a5BYLxzvVF9TccNgS2SlnocwzSBfd1xaxclLzUNAAA2Jzwwc s2HSmD+thW6vw8CYSSWDAiP+ig+1cGClwZgK2cnmfym69sK4pGD/O3/QK8FFTHCkIGOO j63FTsTydhoj6Kn5bXI3tM4hodSVapvOQ1gaB32yS8K9D2CHDAJjwhQi21g4OQT7IeRx cpEx/JEQcaKs3XjgIk/SY+6mZ04+AlGJezi7RkSe2Y2Vd7ymj9GbgVBLpKx2LZRlZltx j/kjMivlQlGtanSeNQeaBfyuHjdRX3X5wKcVxC6sAOljS46GwJrKB8rU/tzVm+5zHDK1 I+hQ== X-Gm-Message-State: ACrzQf1F1xzjYuC49RWJUqYt/UPOgI7VO9QbRCBFT+jevupvnJZtnL9F I0R0L8kylP2dRvDowAZ/gCk4UDHeGJErv3PgmJ2WzA== X-Google-Smtp-Source: AMsMyM4b2Vj39igZgTXUtsncmivIVHh3i7HtcKOts5c+joxCeebwNkLaWYGY94vJwWtPGUK7vbae4Eh3jrASaTplehg= X-Received: by 2002:ac2:4ecc:0:b0:4a2:bbdc:1430 with SMTP id p12-20020ac24ecc000000b004a2bbdc1430mr1796750lfr.404.1668113022969; Thu, 10 Nov 2022 12:43:42 -0800 (PST) In-Reply-To: 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:247546 Archived-At: --0000000000002e024d05ed23d4a6 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Given the following code <.foo> ;; component, start_component ;; tag, start_tag ;; [cursor here 1)] end_tag ;; [cursor here 2] end_component and ast (fragment [0, 0] - [5, 0] (component [0, 0] - [3, 7] (start_component [0, 0] - [0, 6] (component_name [0, 1] - [0, 5] (function [0, 2] - [0, 5]))) (tag [1, 2] - [2, 8] (start_tag [1, 2] - [1, 7] (tag_name [1, 3] - [1, 6])) (end_tag [2, 2] - [2, 8] (tag_name [2, 4] - [2, 7]))) (end_component [3, 0] - [3, 7] (component_name [3, 2] - [3, 6] (function [3, 3] - [3, 6]))))) I do not know how to reliably move from cursor position 1 to start of and from cursor position 2 to start of <.foo> as (treesit-search-forward (treesit-node-at (point)) (rx (or "end_tag" "end_component" "end_slot")) t) on position 1 will not look backwards, but find the <./foo> end_component. if i am at the point after the node, how do I find the node before the point? once we have the node before the point we can find the correct parent and then find the prev sibling to goto. Hope this makes sense. On Thu, 10 Nov 2022 at 21:32, Wilhelm Kirschbaum wrote: > Sorry, I see something like this makes sense and is not the prev-sibling > issue. > > (defun heex--treesit-backward-sexp () > "Forward sexp for Heex using treesit." > (let* ((node (treesit-search-forward > (treesit-node-at (point)) > (rx (or "end_tag" "end_component" "end_slot")) > t)) > (sibling (treesit-node-prev-sibling node))) > (when sibling > (goto-char (treesit-node-start sibling))))) > > Just need to handle the outermost (fragment peace and should work then. > > Thanks for the help. > > On Thu, 10 Nov 2022 at 21:05, Wilhelm Kirschbaum > wrote: > >> Full implementation here: >> https://github.com/wkirschbaum/elixir-mode/blob/main/heex-mode.el >> >> On Thu, 10 Nov 2022 at 21:03, Wilhelm Kirschbaum >> wrote: >> >>> forward-sexp works well with the below code, but when calling >>> treesit-node-prev-sibling it will traverse up the list, which is then >>> breaking backward-up-list when defining forward-sexp in the major mode. >>> >>> (defun heex--treesit-largest-node-at-point () >>> "Find the largest node at point." >>> (save-excursion >>> (forward-comment (point-max)) >>> (let ((node-list >>> (cl-loop for node =3D (treesit-node-at (point)) >>> then (treesit-node-parent node) >>> while node >>> if (eq (treesit-node-start node) >>> (point)) >>> collect node))) >>> (car (last node-list))))) >>> >>> (defun heex--treesit-backward-sexp () >>> "Forward sexp for Heex using treesit." >>> (let* ((largest-node (heex--treesit-largest-node-at-point)) >>> (sibling (treesit-node-prev-sibling largest-node))) >>> (when sibling >>> (goto-char (treesit-node-start sibling))))) >>> >>> (defun heex--treesit-forward-sexp () >>> "Forward sexp for Heex using treesit." >>> (let* ((largest-node (heex--treesit-largest-node-at-point)) >>> (sibling (treesit-node-next-sibling largest-node))) >>> (when sibling >>> (goto-char (treesit-node-start sibling)) >>> (forward-comment (- (point-max)))))) >>> >>> On Thu, 10 Nov 2022 at 10:14, Yuan Fu wrote: >>> >>>> >>>> >>>> > On Nov 9, 2022, at 10:44 PM, Wilhelm Kirschbaum < >>>> wilhelm@floatpays.co.za> wrote: >>>> > >>>> > I finally had some time to have a look. I don't see any more issues, >>>> thank you for the fantastic work on this. The defun-type-regexp is not >>>> enough to identify a defun in elixir this is the query I am using curr= ently: >>>> > >>>> > (defvar elixir--treesit-query-defun >>>> > (let ((query `((call >>>> > target: (identifier) @type >>>> > (arguments >>>> > [ >>>> > (alias) @name >>>> > (identifier) @name >>>> > (call target: (identifier)) @name >>>> > (binary_operator >>>> > left: (call target: (identifier)) @name >>>> > operator: "when") >>>> > ]) >>>> > (:match ,elixir--definition-keywords-re @type) >>>> > )))) >>>> > (treesit-query-compile 'elixir query))) >>>> > >>>> > Regex will work in most cases I guess, but does not really deal with >>>> more complex queries for more complex cases like in elixir as there is= not >>>> one type which is always the defun. elixir relies heavily on macros an= d >>>> different defun macros can be defined on the fly. >>>> >>>> You can try the following procedure: use a regex to find the >>>> next/previous call, then perform some check on whether it=E2=80=99s in= deed a defun, >>>> if not, keep searching for the next/previous call. >>>> >>>> >>>> > Maybe if there is an option for using either a regex or a function? >>>> >>>> Yes, instead of a regex you can pass a predicate function. >>>> >>>> > >>>> > I am also not sure how forward-sexp can work with the current >>>> treesit-search-forward-goto function as it does not take into consider= ation >>>> the level. Is there perhaps a way to move forward/backward, but do not= jump >>>> to parents or children? >>>> >>>> If you want to move in the same level, perhaps you can use >>>> treesit-next/prev/sibling? >>>> >>>> Yuan >>> >>> --0000000000002e024d05ed23d4a6 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Given the following code

=C2= =A0<.foo>=C2=A0 ;; component, start_component
=C2=A0<bar>=C2= =A0 ;; tag, start_tag
=C2=A0 </bar>;; [cursor here 1)] end_tag </.foo> ;; [cursor here 2] end_component

and ast

(fragment [0, 0] - [5, 0]
=C2=A0 (component [0, = 0] - [3, 7]
=C2=A0 =C2=A0 (start_component [0, 0] - [0, 6]
=C2=A0 =C2= =A0 =C2=A0 (component_name [0, 1] - [0, 5]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (= function [0, 2] - [0, 5])))
=C2=A0 =C2=A0 (tag [1, 2] - [2, 8]
=C2=A0= =C2=A0 =C2=A0 (start_tag [1, 2] - [1, 7]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (t= ag_name [1, 3] - [1, 6]))
=C2=A0 =C2=A0 =C2=A0 (end_tag [2, 2] - [2, 8]<= br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 (tag_name [2, 4] - [2, 7])))
=C2=A0 =C2= =A0 (end_component [3, 0] - [3, 7]
=C2=A0 =C2=A0 =C2=A0 (component_name = [3, 2] - [3, 6]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (function [3, 3] - [3, 6])))= ))

I do not know how to reliably move from cursor positio= n 1 to start of <bar> and from cursor position 2 to start of <.foo= >

as (treesit-search-forward (treesit-node-at (= point)) (rx (or "end_tag" "end_component" "end_slo= t")) t) on position 1 will not look backwards, but find the <./foo&= gt; end_component.

if i am at the point after the node, = how do I find the node before the point? once we have the node before the p= oint we can find the correct parent and then find the prev sibling to goto.=

Hope this makes sense.


On Thu, 10 Nov 2022 at 21:32, Wilhelm Kirschbaum <wilhelm@floatpays.co.za> wrote:
Sorry= , I see something like this makes sense and is not the prev-sibling issue. =

(defun heex--treesit-backward-sexp ()
=C2= =A0 "Forward sexp for Heex using treesit."
=C2=A0 (let* ((node= (treesit-search-forward
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 (treesit-node-at (point))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 (rx (or "end_tag" "end_component" = "end_slot"))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 t))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(sibling (treesit-node-prev= -sibling node)))
=C2=A0 =C2=A0 (when sibling
=C2=A0 =C2=A0 =C2=A0 (go= to-char (treesit-node-start sibling)))))

Just need= to handle the outermost (fragment peace and should work then.

Thanks for the help.

On Thu, 10 Nov 2022 at 21:= 05, Wilhelm Kirschbaum <wilhelm@floatpays.co.za> wrote:

On Thu, 10 Nov 2022 at 21:03, Wilhelm Kirschbaum <wilhelm@floatpays= .co.za> wrote:
forward-sexp works well with the below code, bu= t when calling treesit-node-prev-sibling it will traverse up the list, whic= h is then breaking backward-up-list when defining forward-sexp in the major= mode.

(defun heex--treesit-largest-node-at-p= oint ()
=C2=A0 "Find the largest node at point."
=C2=A0 (sa= ve-excursion
=C2=A0 =C2=A0 (forward-comment (point-max))
=C2=A0 =C2= =A0 (let ((node-list
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(cl-loop f= or node =3D (treesit-node-at (point))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 then (treesit-node-parent node)
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 while no= de
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= if (eq (treesit-node-start node)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(point))
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 collect = node)))
=C2=A0 =C2=A0 =C2=A0 (car (last node-list)))))

(defun hee= x--treesit-backward-sexp ()
=C2=A0 "Forward sexp for Heex using tre= esit."
=C2=A0 (let* ((largest-node (heex--treesit-largest-node-at-p= oint))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(sibling (treesit-node-prev-sib= ling largest-node)))
=C2=A0 =C2=A0 (when sibling
=C2=A0 =C2=A0 =C2=A0= (goto-char (treesit-node-start sibling)))))

(defun heex--treesit-fo= rward-sexp ()
=C2=A0 "Forward sexp for Heex using treesit."=C2=A0 (let* ((largest-node (heex--treesit-largest-node-at-point))
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(sibling (treesit-node-next-sibling largest-= node)))
=C2=A0 =C2=A0 (when sibling
=C2=A0 =C2=A0 =C2=A0 (goto-char (= treesit-node-start sibling))
=C2=A0 =C2=A0 =C2=A0 (forward-comment (- (p= oint-max))))))

On Thu, 10 Nov 2022 at 10:14, Yuan Fu <casouri@gmail.com> wrote:<= br>


> On Nov 9, 2022, at 10:44 PM, Wilhelm Kirschbaum <wilhelm@floatpays.co.za> = wrote:
>
> I finally had some time to have a look. I don't see any more issue= s, thank you for the fantastic work on this. The defun-type-regexp is not e= nough to identify a defun in elixir this is the query I am using currently:=
>
> (defvar elixir--treesit-query-defun
>=C2=A0 =C2=A0(let ((query `((call
>=C2=A0 =C2=A0 =C2=A0 target: (identifier) @type
>=C2=A0 =C2=A0 =C2=A0 (arguments
>=C2=A0 =C2=A0 =C2=A0 =C2=A0[
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 (alias) @name
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 (identifier) @name
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 (call target: (identifier)) @name
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 (binary_operator
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0left: (call target: (identifier)) @na= me
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0operator: "when")
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 ])
>=C2=A0 =C2=A0 =C2=A0 (:match ,elixir--definition-keywords-re @type)
>=C2=A0 =C2=A0 =C2=A0 ))))
>=C2=A0 =C2=A0 =C2=A0(treesit-query-compile 'elixir query)))
>
> Regex will work in most cases I guess, but does not really deal with m= ore complex queries for more complex cases like in elixir as there is not o= ne type which is always the defun. elixir relies heavily on macros and diff= erent defun macros can be defined on the fly.

You can try the following procedure: use a regex to find the next/previous = call, then perform some check on whether it=E2=80=99s indeed a defun, if no= t, keep searching for the next/previous call.


> Maybe if there is an option for using either a regex or a function?
Yes, instead of a regex you can pass a predicate function.

>
> I am also not sure how forward-sexp can work with the current treesit-= search-forward-goto function as it does not take into consideration the lev= el. Is there perhaps a way to move forward/backward, but do not jump to par= ents or children?

If you want to move in the same level, perhaps you can use treesit-next/pre= v/sibling?

Yuan
--0000000000002e024d05ed23d4a6--