diff options
author | Yuan Fu <casouri@gmail.com> | 2023-04-13 19:20:53 -0700 |
---|---|---|
committer | Yuan Fu <casouri@gmail.com> | 2023-04-14 15:52:30 -0700 |
commit | a4de6d8dd3b6a5ac39737812e17a2a8d766e9f3a (patch) | |
tree | a86782f09707ad75a937fe4e00ae6076f0fef2db /src | |
parent | c60b59e04c3776a90adaa8c8fe53af3075a833b8 (diff) | |
download | emacs-a4de6d8dd3b6a5ac39737812e17a2a8d766e9f3a.tar.gz |
Add treesit-node-match-p
* src/treesit.c (Ftreesit_node_match_p): New function.
Diffstat (limited to 'src')
-rw-r--r-- | src/treesit.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/treesit.c b/src/treesit.c index dbbfa29c19d..b15a53236e4 100644 --- a/src/treesit.c +++ b/src/treesit.c @@ -3612,6 +3612,37 @@ a regexp. */) return parent; } +DEFUN ("treesit-node-match-p", + Ftreesit_node_match_p, + Streesit_node_match_p, 2, 2, 0, + doc: /* Check whether NODE matches PREDICATE. + +PREDICATE can be a regexp matching node type, a predicate function, +and more, see `treesit-things-definition' for detail. Return non-nil +if NODE matches PRED, nil otherwise. */) + (Lisp_Object node, Lisp_Object predicate) +{ + CHECK_TS_NODE (node); + + Lisp_Object signal_data = Qnil; + if (!treesit_traverse_validate_predicate (predicate, &signal_data)) + xsignal1 (Qtreesit_invalid_predicate, signal_data); + + Lisp_Object parser = XTS_NODE (node)->parser; + TSTreeCursor cursor = ts_tree_cursor_new (XTS_NODE (node)->node); + + specpdl_ref count = SPECPDL_INDEX (); + record_unwind_protect_ptr (treesit_traverse_cleanup_cursor, &cursor); + + bool match = false; + match = treesit_traverse_match_predicate (&cursor, predicate, + parser, false); + + unbind_to (count, Qnil); + + return match ? Qt : Qnil; +} + DEFUN ("treesit-subtree-stat", Ftreesit_subtree_stat, Streesit_subtree_stat, 1, 1, 0, @@ -3879,6 +3910,7 @@ then in the system default locations for dynamic libraries, in that order. */); defsubr (&Streesit_search_subtree); defsubr (&Streesit_search_forward); defsubr (&Streesit_induce_sparse_tree); + defsubr (&Streesit_node_match_p); defsubr (&Streesit_subtree_stat); #endif /* HAVE_TREE_SITTER */ defsubr (&Streesit_available_p); |