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:05:07 +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="00000000000038755f05ed2274b6" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29126"; 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:06:14 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 1otCsO-0007Li-R7 for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 10 Nov 2022 20:06:13 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1otCsG-0007PA-9T; Thu, 10 Nov 2022 14:06: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 1otCsE-0007On-OB for bug-gnu-emacs@gnu.org; Thu, 10 Nov 2022 14:06:02 -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 1otCsE-0007td-FQ for bug-gnu-emacs@gnu.org; Thu, 10 Nov 2022 14:06:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1otCsE-00012z-41 for bug-gnu-emacs@gnu.org; Thu, 10 Nov 2022 14:06: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 19:06: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.16681071263984 (code B ref 58711); Thu, 10 Nov 2022 19:06:02 +0000 Original-Received: (at 58711) by debbugs.gnu.org; 10 Nov 2022 19:05:26 +0000 Original-Received: from localhost ([127.0.0.1]:44402 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1otCre-00012C-37 for submit@debbugs.gnu.org; Thu, 10 Nov 2022 14:05:26 -0500 Original-Received: from mail-lj1-f180.google.com ([209.85.208.180]:46867) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1otCrc-00011y-2v for 58711@debbugs.gnu.org; Thu, 10 Nov 2022 14:05:24 -0500 Original-Received: by mail-lj1-f180.google.com with SMTP id l8so1995533ljh.13 for <58711@debbugs.gnu.org>; Thu, 10 Nov 2022 11:05:24 -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=84Ra6BECLrVwom9uGsI4aBwCLeVgl+1BWgbJ13R8taM=; b=brno2d5gW3sgPKiByJqxfkdAgmaMc1EGt8lImH5MnPY0fRlGSEs2ucnVbDoWX/VCiC o2C1YAi7z5YBSMLd4/9e3AHwjK34sRvfcvZAzFjbQWwnDoisfXPChvgceEC9qLShzG1H hud/MnccuNWhLTZkUwlCIJqYUb0n+Puwqc2F0= 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=84Ra6BECLrVwom9uGsI4aBwCLeVgl+1BWgbJ13R8taM=; b=Z5W4PTkay1s8YhkurEgVuQen57kWgPxxsCW7BVhUsnCRIQDSp3//dSAM8LCC7IguiA hiRxmPu3TbWDJCkKS9KUvoLuooFIwOZkXnOQ8Xts7OOtfQlJrUA+XRoMNvWAPIKDoo/T +IjWMXAiagBqYvjcjoHERwny0ZnV9OoVxSbzlOebjnFWADWyDYudwNzdfSn717L2xu8Y ANYBoI7j6HoYjfD5VfDuiReCm/otc6mbcUbjdEeQ6xBCPraHp/cNil/04E3pu7bv6TQr fS2h5AfQdQmSmVWcKSgeFKOyhgR1W0RqJROr4shDcHnxE77cADvbE+qD/LMt8iL7Vl8o 94/A== X-Gm-Message-State: ACrzQf1vYeqsQi0lIO94SmiQAvePHdOmbmG1vS8WPLGdUppAf4CZU9Zj 0Z+J2Y+aKqIZ9OoWwiHs52XDQQSjRt344IuqhFqbaTTJALuj470v X-Google-Smtp-Source: AMsMyM4rtSDkps2o1O9KItbQ3HlYEpLwPM1km9YFAZc4va89KalEWyWsg4bwAc6As5E8cUqR9Ylf5oz3eu52BMiXTFU= X-Received: by 2002:a05:651c:b1e:b0:26e:33e:4513 with SMTP id b30-20020a05651c0b1e00b0026e033e4513mr9156253ljr.253.1668107118073; Thu, 10 Nov 2022 11:05:18 -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:247539 Archived-At: --00000000000038755f05ed2274b6 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 curren= tly: >> > >> > (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 n= ot >> 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 inde= ed 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 considerat= ion >> the level. Is there perhaps a way to move forward/backward, but do not j= ump >> to parents or children? >> >> If you want to move in the same level, perhaps you can use >> treesit-next/prev/sibling? >> >> Yuan > > --00000000000038755f05ed2274b6 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Thu, 10 Nov 2022 at 21:03, Wilhe= lm Kirschbaum <wilhelm@floatp= ays.co.za> wrote:
forward-sexp works well with the below cod= e, 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 ()
=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
--00000000000038755f05ed2274b6--