bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#58711: Treesit hangs when calling treesit-search-forward


From: Wilhelm Kirschbaum
Subject: bug#58711: Treesit hangs when calling treesit-search-forward
Date: Mon, 14 Nov 2022 08:32:40 +0200

This works for me for all cases I managed to test:

( I have to keep track of the EOL and BOL to ensure that we don't jump to parents or children. )

(defun heex--treesit-largest-node-at-point (&optional node)
  "Find the largest node at point or from specified NODE."
  (save-excursion
    (forward-comment (point-max))
    (let ((node-list
           (cl-loop for node = (or node (treesit-node-at (point)))
                    then (treesit-node-parent node)
                    while (and node (not (equal (treesit-node-type node) "fragment")))
                    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 ((node
         (save-excursion
           (forward-comment (- (point-max)))
           (let ((bol (pos-bol))
                 (end-node (treesit-search-forward-goto
                            (treesit-node-at (point))
                            (rx (or "end_tag" "end_component" "end_slot")) t t)))
             (if (and end-node (> (treesit-node-end end-node) bol))
                 (treesit-node-start (treesit-node-parent end-node)))))))
    (when node (goto-char node))))

(defun heex--treesit-forward-sexp ()
  "Forward sexp for Heex using treesit."
  (let* ((node (heex--treesit-largest-node-at-point))
         (sibling (treesit-node-next-sibling node)))
    (if sibling
        (progn
          (goto-char (treesit-node-start sibling))
          (forward-comment (- (point-max))))
      (when node
        (pcase (treesit-node-type node)
          ((or "end_tag" "end_component" "end_slot") nil)
          (_ (goto-char (treesit-node-end node))))))))

On Fri, 11 Nov 2022 at 08:30, Wilhelm Kirschbaum <wilhelm@floatpays.co.za> wrote:
Thank you. I will spend some time over the weekend to try and have a look.

On Fri, 11 Nov 2022 at 00:07, Yuan Fu <casouri@gmail.com> wrote:


> On Nov 10, 2022, at 12:43 PM, Wilhelm Kirschbaum <wilhelm@floatpays.co.za> wrote:
>
> Given the following code
>
>  <.foo>  ;; component, start_component
>  <bar>  ;; tag, start_tag
>   </bar>;; [cursor here 1)] end_tag
> </.foo> ;; [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 <bar> and from cursor position 2 to start of <.foo>

The snippet below should take you to the corresponding open tag.

(let ((end-node (treesit-search-forward-goto
                 (treesit-node-at (point))
                 (rx (or "end_tag" "end_component" "end_slot")) t t)))
  ;; Go to the corresponding start tag.
  (goto-char (treesit-node-start (treesit-node-parent end-node))))

>
> 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.

Yeah, if there is a function that gives you the node right before point, treesit-search-forward would work as expected. I’ll see how to add this functionality.

Yuan

reply via email to

[Prev in Thread] Current Thread [Next in Thread]