Function: treesit-search-forward-goto
treesit-search-forward-goto is a byte-compiled function defined in
treesit.el.gz.
Signature
(treesit-search-forward-goto NODE PREDICATE &optional START BACKWARD ALL)
Documentation
Search forward for a node and move to its end position.
Stop at the first node after NODE that matches PREDICATE. PREDICATE can be either a regexp that matches against each node's type case-insensitively, or a function that takes a node and returns nil/non-nil for match/no match.
If a node matches, move to that node and return the node, otherwise return nil. If START is non-nil, stop at the beginning rather than the end of a node.
This function guarantees that the matched node it returns makes progress in terms of buffer position: the start/end position of the returned node is always STRICTLY greater/less than that of NODE.
BACKWARD and ALL are the same as in treesit-search-forward.
Other relevant functions are documented in the treesit group.
Shortdoc
;; treesit
(treesit-search-forward-goto node "function_definition")
e.g. => #<treesit-node (function_definition) in 57-146>
Source Code
;; Defined in /usr/src/emacs/lisp/treesit.el.gz
;;; Search
(defun treesit-search-forward-goto
(node predicate &optional start backward all)
"Search forward for a node and move to its end position.
Stop at the first node after NODE that matches PREDICATE.
PREDICATE can be either a regexp that matches against each node's
type case-insensitively, or a function that takes a node and
returns nil/non-nil for match/no match.
If a node matches, move to that node and return the node,
otherwise return nil. If START is non-nil, stop at the
beginning rather than the end of a node.
This function guarantees that the matched node it returns makes
progress in terms of buffer position: the start/end position of
the returned node is always STRICTLY greater/less than that of
NODE.
BACKWARD and ALL are the same as in `treesit-search-forward'."
(when-let* ((start-pos (if start
(treesit-node-start node)
(treesit-node-end node)))
(current-pos start-pos))
;; When searching forward and stopping at beginnings, or search
;; backward stopping at ends, it is possible to "roll back" in
;; position. Take three nodes N1, N2, N3 as an example, if we
;; start at N3, search for forward for beginning, and N1 matches,
;; we would stop at beg of N1, which is backwards! So we skip N1
;; and keep going.
;;
;; |<--------N1------->|
;; |<--N2-->| |<--N3-->|
(while (and node (if backward
(>= current-pos start-pos)
(<= current-pos start-pos)))
(setq node (treesit-search-forward
node predicate backward all))
(setq current-pos (if start
(treesit-node-start node)
(treesit-node-end node))))
(cond
;; When there is a match and match made progress, go to the
;; result position.
((and node
(if backward
(< current-pos (point))
(> current-pos (point))))
(goto-char current-pos)))
node))