Function: treesit-transpose-sexps

treesit-transpose-sexps is a byte-compiled function defined in treesit.el.gz.

Signature

(treesit-transpose-sexps &optional ARG)

Documentation

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.

Return a pair of positions as described by transpose-sexps-function for use in transpose-subr and friends.

Probably introduced at or before Emacs version 30.1.

Source Code

;; Defined in /usr/src/emacs/lisp/treesit.el.gz
(defun treesit-transpose-sexps (&optional arg)
  "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.

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