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:03:06 +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="0000000000000c813b05ed226dfb" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="17385"; 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:04: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 1otCqS-0004DT-Rl for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 10 Nov 2022 20:04:13 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1otCqL-0006Tj-Oa; Thu, 10 Nov 2022 14: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 1otCqI-0006Tb-7v for bug-gnu-emacs@gnu.org; Thu, 10 Nov 2022 14:04: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 1otCqH-0005jY-Vr for bug-gnu-emacs@gnu.org; Thu, 10 Nov 2022 14:04:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1otCqH-000104-RF for bug-gnu-emacs@gnu.org; Thu, 10 Nov 2022 14:04: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:04: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.16681070093806 (code B ref 58711); Thu, 10 Nov 2022 19:04:01 +0000 Original-Received: (at 58711) by debbugs.gnu.org; 10 Nov 2022 19:03:29 +0000 Original-Received: from localhost ([127.0.0.1]:44398 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1otCpk-0000zJ-Bx for submit@debbugs.gnu.org; Thu, 10 Nov 2022 14:03:28 -0500 Original-Received: from mail-lf1-f42.google.com ([209.85.167.42]:41737) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1otCpf-0000z4-TY for 58711@debbugs.gnu.org; Thu, 10 Nov 2022 14:03:26 -0500 Original-Received: by mail-lf1-f42.google.com with SMTP id f37so4945977lfv.8 for <58711@debbugs.gnu.org>; Thu, 10 Nov 2022 11:03:23 -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=WukTo/F8ThmY6SVGCg/AhwnXhgpJY65zqkFnHYfimJ8=; b=ViV3iPthz/IgTZtJtYpbbxoyJmDZUtP+6TH6/Kz/rdJ3BoAJ2rnuL2eAp9OqrS1AMA fkr1ZHtVnGJmPwMnnscJKiY0UWSWXgROwtUXWPtNAWhVrV7F+j8ZNmd70xLJNLxRaJjg 3GrYlkdef6RcmYzWeuhbqCQT44MfgdegI8hCs= 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=WukTo/F8ThmY6SVGCg/AhwnXhgpJY65zqkFnHYfimJ8=; b=1NGd148Ax058kVXULgtsKES2dWOBBgAT2x7OtNaAod7Spw5Pgy9MJhF74MUfgB2FbV vjUIbcwVUmEAaq7dNz8hBJoWbUXb7MjX4AfRVuqiFspMeDf51hWzdkbpxGCf658pYpzc tKLbCRvQ4G27fH46Phhn3FUeNzt3frM/xFyrbtrmfOixnrUvOhj+i6pvfGrDFQnzEg+x jzuHcL1q4lJoSSWsFQrLowoCbV3Xq8EQKlnYmkc/aQyvA9wU3DzG0/jgUEQUHbQmI5VV tW4Izt2FXg1GA+4OT0ushSLwecH3IywKNrWF23FYGOju9niOpbXQFOl14l4gdj5WVigv 423w== X-Gm-Message-State: ACrzQf16LowoA/X8uJUg/peu4I+A9i76A7z1DuHTP0yX2dx7jxKxmXDJ pj46fZduyETlw/gx+dP+PV0TLJm5rvpJ1816urQAsw== X-Google-Smtp-Source: AMsMyM6xdv1sNh0W+cctWqnAPZGHfmL6dp54K/mUGbHAwuSP5g8V90E05ppvWewM95ak4iyOPCm4mrFvuLht9m+D6j0= X-Received: by 2002:ac2:4ecc:0:b0:4a2:bbdc:1430 with SMTP id p12-20020ac24ecc000000b004a2bbdc1430mr1699552lfr.404.1668106997752; Thu, 10 Nov 2022 11:03:17 -0800 (PST) In-Reply-To: <82A013FB-9A94-4E57-9FD2-AF8E892241A9@gmail.com> 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:247538 Archived-At: --0000000000000c813b05ed226dfb Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 > 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 current= ly: > > > > (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 no= t > 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/previou= s > call, then perform some check on whether it=E2=80=99s indeed 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 considerati= on > the level. Is there perhaps a way to move forward/backward, but do not ju= mp > to parents or children? > > If you want to move in the same level, perhaps you can use > treesit-next/prev/sibling? > > Yuan --0000000000000c813b05ed226dfb Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
forward-sexp works well with the below code, but when= calling treesit-node-prev-sibling it will traverse up the list, which is t= hen 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-exc= ursion
=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)
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 while node
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (e= q (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 heex--t= reesit-backward-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-prev-sibling= largest-node)))
=C2=A0 =C2=A0 (when sibling
=C2=A0 =C2=A0 =C2=A0 (go= to-char (treesit-node-start sibling)))))

(defun heex--treesit-forwar= d-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 (tree= sit-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> 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 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
--0000000000000c813b05ed226dfb--