Subject: bug#78458: 31.0.50; treesit.el: thing navigation functions work incorrectly with some Clojure nodes

From: Roman Rudakov <[email-masked]>
Date: Mon, 19 May 2025 20:20:51 +0200

Juri Linkov <[email-masked]> writes:

>>> +     (sexp-default
>>> +      ;; For `C-M-f' in "#|(a)"
>>> +      ("(" . ,(lambda (node)
>>> +                 (and (eq (char-before (point)) ?\#)
>>> +                      (equal (treesit-node-type 
>>> (treesit-node-parent node))
>>> +                             "anon_fn_lit")))))
>> Thanks Juri. I tried your patch and for some reason it doesn't 
>> solve the
>> issue neither with 'raise-sexp' nor with 'C-M-f' navigation. The 
>> point is
>> moved from:
>>
>> #|(-> (.-value (.-target %)))
>>
>> to
>>
>> #(-> (.-value (.-target %))|)
>
> I tested that this patch fixes 'C-M-f' navigation where point moves 
> to
>
> #(-> (.-value (.-target %)))|
>
>> and 'raise-sexp' called from:
>>
>> #(-> |(.-value (.-target %)))
>>
>> produces:
>>
>> #(.-value (.-target %)))
>
> Indeed, it didn't fix this case.  For down/up-list it needs
> to use 'sexp' thing instead of 'list' thing.  Probably the simplest
> setting would be to redefine down-list-function/up-list-function.
>
> Ok, here is a complete tested patch that fixes all your test cases:
>
> [2. text/x-diff; clojure-ts-mode-sexp.patch]
> diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el
> index 8b6bca7..6ae8e16 100644
> --- a/clojure-ts-mode.el
> +++ b/clojure-ts-mode.el
> @@ -1549,6 +1549,12 @@ clojure-ts--thing-settings
>    `((clojure
>       (sexp ,(regexp-opt clojure-ts--sexp-nodes))
>       (list ,(regexp-opt clojure-ts--list-nodes))
> +     (sexp-default
> +      ;; For `C-M-f' in "#|(a)"
> +      ("(" . ,(lambda (node)
> +                 (and (eq (char-before (point)) ?\#)
> +                      (equal (treesit-node-type 
> (treesit-node-parent node))
> +                             "anon_fn_lit")))))
>       (text ,(regexp-opt '("comment")))
>       (defun ,#'clojure-ts--defun-node-p))
>      (when clojure-ts-use-markdown-inline
> @@ -2601,6 +2607,15 @@ clojure-ts-mode
>                  0
>                  t)
>
> +      (setq-local down-list-function
> +                  (lambda (&optional arg)
> +                    (let ((treesit-sexp-type-regexp 'sexp))
> +                      (treesit-down-list arg))))
> +      (setq-local up-list-function
> +                  (lambda (&optional arg escape-strings 
> no-syntax-crossing)
> +                    (let ((treesit-sexp-type-regexp 'sexp))
> +                      (treesit-up-list arg escape-strings 
> no-syntax-crossing))))
> +
>        ;; Workaround for treesit-transpose-sexps not correctly 
>        working with
>        ;; treesit-thing-settings on Emacs 30.
>        ;; Once treesit-transpose-sexps it working again this can be 
>        removed
Thank you very much for the patch Juri. It indeed solves all of the 
problems I mentioned.  However while I was testing the patch I noticed 
that the 'down-list' function behaves a bit odd. If I call it multiple 
times starting from the beginning of the line it moves the point to 
the following positions:

#|(-> (.-value (.-target %)))
#(->| (.-value (.-target %)))
#(-> (.-value| (.-target %)))
#(-> (.-value| (|.-target %)))
#(-> (.-value| (.-target| %)))
#(-> (.-value| (.-target %|)))

While it would be more logical to have:

#(|-> (.-value (.-target %)))
#(-> (|.-value (.-target %)))
#(-> (.-value (|.-target %)))

--
Best regards, Roman