Function: treesit-node-top-level

treesit-node-top-level is a byte-compiled function defined in treesit.el.gz.

Signature

(treesit-node-top-level NODE &optional PRED INCLUDE-NODE)

Documentation

Return the top-level equivalent of NODE.

Specifically, return the highest parent of NODE that has the same type as it. If no such parent exists, return nil.

If PRED is non-nil, match each parent's type with PRED rather than using NODE's type. PRED can also be a predicate function, and more. See treesit-thing-settings for details.

If INCLUDE-NODE is non-nil, return NODE if it satisfies PRED.

Other relevant functions are documented in the treesit group.

View in manual

Shortdoc

;; treesit
(treesit-node-top-level node)
    e.g. => #<treesit-node (declaration) in 1-11>

Source Code

;; Defined in /usr/src/emacs/lisp/treesit.el.gz
(defun treesit-node-top-level (node &optional pred include-node)
  "Return the top-level equivalent of NODE.

Specifically, return the highest parent of NODE that has the same
type as it.  If no such parent exists, return nil.

If PRED is non-nil, match each parent's type with PRED rather
than using NODE's type.  PRED can also be a predicate function,
and more.  See `treesit-thing-settings' for details.

If INCLUDE-NODE is non-nil, return NODE if it satisfies PRED."
  (let ((pred (or pred (rx bos (literal (treesit-node-type node)) eos)))
        (result nil))
    (cl-loop for cursor = (if include-node node
                            (treesit-node-parent node))
             then (treesit-node-parent cursor)
             while cursor
             if (treesit-node-match-p cursor pred t)
             do (setq result cursor))
    result))