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 21:32:56 +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="000000000000b4833805ed22d7f9" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9676"; 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 20:34:27 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 1otDJi-0002IZ-W5 for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 10 Nov 2022 20:34:27 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1otDJW-00077P-EN; Thu, 10 Nov 2022 14:34:14 -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 1otDJK-00073T-Mk for bug-gnu-emacs@gnu.org; Thu, 10 Nov 2022 14:34:08 -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 1otDJJ-00080L-Sq for bug-gnu-emacs@gnu.org; Thu, 10 Nov 2022 14:34:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1otDJJ-0001g5-Oc for bug-gnu-emacs@gnu.org; Thu, 10 Nov 2022 14:34:01 -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 19:34:01 +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.16681087976399 (code B ref 58711); Thu, 10 Nov 2022 19:34:01 +0000 Original-Received: (at 58711) by debbugs.gnu.org; 10 Nov 2022 19:33:17 +0000 Original-Received: from localhost ([127.0.0.1]:44419 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1otDIb-0001f9-42 for submit@debbugs.gnu.org; Thu, 10 Nov 2022 14:33:17 -0500 Original-Received: from mail-lf1-f50.google.com ([209.85.167.50]:38618) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1otDIX-0001et-57 for 58711@debbugs.gnu.org; Thu, 10 Nov 2022 14:33:16 -0500 Original-Received: by mail-lf1-f50.google.com with SMTP id g7so5098487lfv.5 for <58711@debbugs.gnu.org>; Thu, 10 Nov 2022 11:33:13 -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=YZw301uNZkCNjbr85xf8rKkNBuxQAuvc7TqiKgx5LZo=; b=fuDhXhVXJ0ENa3LWns/ET1tRFnhiNU1uEc0+4lJFDNM6C6bI40dyu0BIbXwor+P8xF /nygdLF1NxGuqkyjfdXGNiANym0SkqGCCsFjIPXs6HYEXarJ0OUPvMP//9qTMU8jL3e4 ilMx1XPbU+cTfu+Qi0A+eeIhK6kCqWjuxkfag= 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=YZw301uNZkCNjbr85xf8rKkNBuxQAuvc7TqiKgx5LZo=; b=aWYbBXtvZcEaffXv+jjAFXurX7wKgQ69Dv8IgLspzh58XZOocB/OLEPGAXmr8ZdC6w gC1kcLCJvAP344OKUGmoHmuUuyEo+izWQIEdfCmllcanprjiWhT+q6jUjt2CEpfXLH3+ kUHEgw4EeKQRyeMGDK7AoD//fhPc4VwCHPJrTqt9uFX0lRor4QcHX2JwYL5SPBcMwnS8 XnSbiptNJ92tMTlBxzu3ghFdRPIutHMknl+o9BOOfcN8NMXFXZzESMEnjrHbZvBq1Nl1 udSrww/l1zsStV7s3PlVkW5Iaqo7I04s163jkBfLSZrHKGAMbR/R9P1Z2oc+Zu5xAkRK GVQg== X-Gm-Message-State: ANoB5plJpPM8wLTs4PqTI0dsY36tOON3Pwqdvk7PUnlj1+dpCHeudr+d gAeHvqADrctVAk/U+VimbfILF2YkbTyVSvQR9LpIBg== X-Google-Smtp-Source: AA0mqf6Lmr227gFWFETKzLQ9MW1U6zuijbo0LSvppEqRLJ6tQ7bdln+oihntHArWilCoTjUnW0zCac0B36wmIO52tTY= X-Received: by 2002:ac2:560b:0:b0:4b3:abaf:b7c1 with SMTP id v11-20020ac2560b000000b004b3abafb7c1mr1722283lfd.39.1668108787149; Thu, 10 Nov 2022 11:33:07 -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:247541 Archived-At: --000000000000b4833805ed22d7f9 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 curre= ntly: >>> > >>> > (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 and >>> 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 ind= eed 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 considera= tion >>> 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 >> >> --000000000000b4833805ed22d7f9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Sorry, I see something like this makes sense and is n= ot the prev-sibling issue.

(defun heex--trees= it-backward-sexp ()
=C2=A0 "Forward sexp for Heex using treesit.&qu= ot;
=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" &= quot;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(s= ibling (treesit-node-prev-sibling node)))
=C2=A0 =C2=A0 (when sibling=C2=A0 =C2=A0 =C2=A0 (goto-char (treesit-node-start sibling)))))

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

Thanks for the help.
<= /div>
O= n 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@fl= oatpays.co.za> wrote:
forward-sexp works well with the below c= ode, but when calling treesit-node-prev-sibling it will traverse up the lis= t, which is then breaking backward-up-list when defining forward-sexp in th= e major mode.

(defun heex--treesit-largest-no= de-at-point ()
=C2=A0 "Find the largest node at point."
=C2= =A0 (save-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 for 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)<= br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 wh= ile node
=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))<= br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 co= llect node)))
=C2=A0 =C2=A0 =C2=A0 (car (last node-list)))))

(def= un heex--treesit-backward-sexp ()
=C2=A0 "Forward sexp for Heex usi= ng treesit."
=C2=A0 (let* ((largest-node (heex--treesit-largest-nod= e-at-point))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(sibling (treesit-node-pr= ev-sibling largest-node)))
=C2=A0 =C2=A0 (when sibling
=C2=A0 =C2=A0 = =C2=A0 (goto-char (treesit-node-start sibling)))))

(defun heex--tree= sit-forward-sexp ()
=C2=A0 "Forward sexp for Heex using treesit.&qu= ot;
=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 lar= gest-node)))
=C2=A0 =C2=A0 (when sibling
=C2=A0 =C2=A0 =C2=A0 (goto-c= har (treesit-node-start sibling))
=C2=A0 =C2=A0 =C2=A0 (forward-comment = (- (point-max))))))

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


> 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
--000000000000b4833805ed22d7f9--