Function: treesit-search-subtree
treesit-search-subtree is a function defined in treesit.c.
Signature
(treesit-search-subtree NODE PREDICATE &optional BACKWARD ALL DEPTH)
Documentation
Traverse the parse tree of NODE depth-first using PREDICATE.
Traverse the subtree of NODE, and match PREDICATE with each node along the way. PREDICATE is a regexp string that matches against each node's type, or a function that takes a node and returns nil/non-nil.
By default, only traverse named nodes, but if ALL is non-nil, traverse all nodes. If BACKWARD is non-nil, traverse backwards. If DEPTH is non-nil, only traverse nodes up to that number of levels down in the tree. If DEPTH is nil, default to 1000.
Return the first matched node, or nil if none matches.
Other relevant functions are documented in the treesit group.
Shortdoc
;; treesit
(treesit-search-subtree node "function_definition")
e.g. => #<treesit-node (function_definition) in 57-146>
Source Code
// Defined in /usr/src/emacs/src/treesit.c
{
CHECK_TS_NODE (node);
CHECK_TYPE (STRINGP (predicate) || FUNCTIONP (predicate),
list3 (Qor, Qstringp, Qfunctionp), predicate);
CHECK_SYMBOL (all);
CHECK_SYMBOL (backward);
/* We use a default limit of 1000. See bug#59426 for the
discussion. */
ptrdiff_t the_limit = treesit_recursion_limit;
if (!NILP (depth))
{
CHECK_FIXNUM (depth);
the_limit = XFIXNUM (depth);
}
treesit_initialize ();
Lisp_Object parser = XTS_NODE (node)->parser;
Lisp_Object return_value = Qnil;
TSTreeCursor cursor;
if (!treesit_cursor_helper (&cursor, XTS_NODE (node)->node, parser))
return return_value;
specpdl_ref count = SPECPDL_INDEX ();
record_unwind_protect_ptr (treesit_traverse_cleanup_cursor, &cursor);
if (treesit_search_dfs (&cursor, predicate, parser, NILP (backward),
NILP (all), the_limit, false))
{
TSNode node = ts_tree_cursor_current_node (&cursor);
return_value = make_treesit_node (parser, node);
}
return unbind_to (count, return_value);
}