emacs-diffs
[Top][All Lists]
Advanced

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

master d9a0e781978: * lisp/treesit.el (treesit-transpose-sexps): Improve


From: Juri Linkov
Subject: master d9a0e781978: * lisp/treesit.el (treesit-transpose-sexps): Improve (bug#60655).
Date: Sat, 4 Jan 2025 13:31:42 -0500 (EST)

branch: master
commit d9a0e781978725953ab44556f5222b1be32df7a7
Author: Juri Linkov <juri@linkov.net>
Commit: Juri Linkov <juri@linkov.net>

    * lisp/treesit.el (treesit-transpose-sexps): Improve (bug#60655).
    
    Use 'treesit-thing-next' and 'treesit-thing-prev' with
    'treesit-node-named' to transpose named siblings that leaves the right
    punctuation in anonymous nodes between named siblings.  Fall back
    to 'transpose-sexps-default-function' inside strings and comments.
    (treesit-node-named): New helper function.
---
 lisp/treesit.el | 51 +++++++++++++++++++++++++++++----------------------
 1 file changed, 29 insertions(+), 22 deletions(-)

diff --git a/lisp/treesit.el b/lisp/treesit.el
index d45acb00188..e643eb48654 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -2624,32 +2624,33 @@ ARG is described in the docstring of `up-list'."
   "Tree-sitter `transpose-sexps' function.
 ARG is the same as in `transpose-sexps'.
 
-Locate the node closest to POINT, and transpose that node with
-its sibling node ARG nodes away.
+Locate the named node closest to POINT, and transpose that node with
+its named sibling node ARG nodes away.
 
 Return a pair of positions as described by
 `transpose-sexps-function' for use in `transpose-subr' and
 friends."
-  ;; First arrive at the right level at where the node at point is
-  ;; considered a sexp. If sexp isn't defined, or we can't find any
-  ;; node that's a sexp, use the node at point.
-  (let* ((node (or (treesit-thing-at-point 'sexp 'nested)
-                   (treesit-node-at (point))))
-         (parent (treesit-node-parent node))
-         (child (treesit-node-child parent 0 t)))
-    (named-let loop ((prev child)
-                     (next (treesit-node-next-sibling child t)))
-      (when (and prev next)
-        (if (< (point) (treesit-node-end next))
-            (if (= arg -1)
-                (cons (treesit-node-start prev)
-                      (treesit-node-end prev))
-              (when-let* ((n (treesit-node-child
-                              parent (+ arg (treesit-node-index prev t)) t)))
-                (cons (treesit-node-end n)
-                      (treesit-node-start n))))
-          (loop (treesit-node-next-sibling prev t)
-                (treesit-node-next-sibling next t)))))))
+  (let* ((pred #'treesit-node-named)
+         (arg (or arg 1))
+         (cnt arg)
+         (inc (if (> arg 0) 1 -1))
+         (pos (point))
+         first sibling)
+    (while (and pos (/= cnt 0))
+      (setq sibling (if (> arg 0)
+                        (treesit-thing-next pos pred)
+                      (treesit-thing-prev pos pred)))
+      (unless first
+        (setq first (if (> arg 0)
+                        (treesit-node-start sibling)
+                      (treesit-node-end sibling))))
+      (setq pos (when sibling
+                  (if (> arg 0)
+                      (treesit-node-end sibling)
+                    (treesit-node-start sibling))))
+      (setq cnt (- cnt inc)))
+    (or (and sibling (cons pos first))
+        (transpose-sexps-default-function arg))))
 
 ;;; Navigation, defun, things
 ;;
@@ -3620,6 +3621,12 @@ before calling this function."
       (treesit-parser-delete parser)
       (delete-overlay ov))))
 
+;;; Helpers
+
+(defun treesit-node-named (node)
+  "Return non-nil if NODE has the property `named'."
+  (treesit-node-check node 'named))
+
 ;;; Debugging
 
 (defvar-local treesit--inspect-name nil



reply via email to

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