Function: treesit-forward-sexp

treesit-forward-sexp is an interactive and byte-compiled function defined in treesit.el.gz.

Signature

(treesit-forward-sexp &optional ARG)

Documentation

Tree-sitter implementation for forward-sexp-function.

ARG is described in the docstring of forward-sexp-function.

If point is inside a text environment where tree-sitter is not supported, go forward a sexp using forward-sexp-default-function. If point is inside code, use tree-sitter functions with the following behavior. If there are no further sexps to move across, signal scan-error like forward-sexp does. If point is already at top-level, return nil without moving point.

What constitutes as text and source code sexp is determined by text and sexp in treesit-thing-settings.

View in manual

Probably introduced at or before Emacs version 30.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/treesit.el.gz
(defun treesit-forward-sexp (&optional arg)
  "Tree-sitter implementation for `forward-sexp-function'.

ARG is described in the docstring of `forward-sexp-function'.

If point is inside a text environment where tree-sitter is not
supported, go forward a sexp using `forward-sexp-default-function'.
If point is inside code, use tree-sitter functions with the
following behavior.  If there are no further sexps to move across,
signal `scan-error' like `forward-sexp' does.  If point is already
at top-level, return nil without moving point.

What constitutes as text and source code sexp is determined
by `text' and `sexp' in `treesit-thing-settings'."
  (interactive "^p")
  (let ((arg (or arg 1))
        (pred (or treesit-sexp-type-regexp 'sexp))
        (node-at-point
         (treesit-node-at (point) (treesit-language-at (point)))))
    (or (when (and node-at-point
                   ;; Make sure point is strictly inside node.
                   (< (treesit-node-start node-at-point)
                      (point)
                      (treesit-node-end node-at-point))
                   (treesit-node-match-p node-at-point 'text t))
          (forward-sexp-default-function arg)
          t)
        (if (> arg 0)
            (treesit-end-of-thing pred (abs arg) 'restricted)
          (treesit-beginning-of-thing pred (abs arg) 'restricted))
        ;; If we couldn't move, we should signal an error and report
        ;; the obstacle, like `forward-sexp' does.  If we couldn't
        ;; find a parent, we simply return nil without moving point,
        ;; then functions like `up-list' will signal "at top level".
        (when-let* ((parent (treesit-thing-at (point) pred t))
                    (boundary (if (> arg 0)
                                  (treesit-node-child parent -1)
                                (treesit-node-child parent 0))))
          (signal 'scan-error (list "No more sexp to move across"
                                    (treesit-node-start boundary)
                                    (treesit-node-end boundary)))))))