Function: treesit-parent-while

treesit-parent-while is a byte-compiled function defined in treesit.el.gz.

Signature

(treesit-parent-while NODE PRED)

Documentation

Return the furthest parent of NODE (including NODE) that satisfies PRED.

This function successively examines NODE, the parent of NODE, then the parent of the parent, etc., until it finds a node which no longer satisfies the predicate PRED; it returns the last examined node that satisfies PRED. If no node satisfies PRED, it returns nil.

PRED should be a function that takes one argument, the node to examine, and returns a boolean value indicating whether that node is a match.

Other relevant functions are documented in the treesit group.

View in manual

Shortdoc

;; treesit
(treesit-parent-while node (lambda (p) (eq (treesit-node-start p) (point))))
    e.g. => #<treesit-node (declaration) in 1-11>

Source Code

;; Defined in /usr/src/emacs/lisp/treesit.el.gz
(defun treesit-parent-while (node pred)
  "Return the furthest parent of NODE (including NODE) that satisfies PRED.

This function successively examines NODE, the parent of NODE,
then the parent of the parent, etc., until it finds a node which
no longer satisfies the predicate PRED; it returns the last
examined node that satisfies PRED.  If no node satisfies PRED, it
returns nil.

PRED should be a function that takes one argument, the node to
examine, and returns a boolean value indicating whether that
node is a match."
  (let ((last nil))
    (while (and node (funcall pred node))
      (setq last node
            node (treesit-node-parent node)))
    last))