From: Yuan Fu Date: Fri, 24 Mar 2023 19:25:19 +0000 (-0700) Subject: Switch buffer in Ftreesit_query_capture X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=db7e95531ac36ae842787b6c5f2859d0642c78cc;p=emacs.git Switch buffer in Ftreesit_query_capture This way both #pred and #match predicates runs in the node's buffer by default. * src/treesit.c: (treesit_predicate_match): No need to switch buffer anymore. (Ftreesit_query_capture): Switch buffer. * doc/lispref/parsing.texi (Pattern Matching): Update manual. --- diff --git a/doc/lispref/parsing.texi b/doc/lispref/parsing.texi index cba323d3a56..86a5d9f2e52 100644 --- a/doc/lispref/parsing.texi +++ b/doc/lispref/parsing.texi @@ -1311,7 +1311,8 @@ matches regular expression @var{regexp}. Matching is case-sensitive. @deffn Predicate pred fn &rest nodes Matches if function @var{fn} returns non-@code{nil} when passed each -node in @var{nodes} as arguments. +node in @var{nodes} as arguments. The function runs with the current +buffer set to the buffer of node being queried. @end deffn Note that a predicate can only refer to capture names that appear in diff --git a/src/treesit.c b/src/treesit.c index 7f4e6cb0e13..36a297ec7da 100644 --- a/src/treesit.c +++ b/src/treesit.c @@ -2533,10 +2533,6 @@ treesit_predicate_match (Lisp_Object args, struct capture_range captures, signal_data)) return false; - struct buffer *old_buffer = current_buffer; - struct buffer *buffer = XBUFFER (XTS_PARSER (XTS_NODE (node)->parser)->buffer); - set_buffer_internal (buffer); - TSNode treesit_node = XTS_NODE (node)->node; ptrdiff_t visible_beg = XTS_PARSER (XTS_NODE (node)->parser)->visible_beg; uint32_t start_byte_offset = ts_node_start_byte (treesit_node); @@ -2563,8 +2559,6 @@ treesit_predicate_match (Lisp_Object args, struct capture_range captures, ZV = old_zv; ZV_BYTE = old_zv_byte; - set_buffer_internal (old_buffer); - return (val > 0); } @@ -2870,6 +2864,10 @@ the query. */) Lisp_Object prev_result = result; Lisp_Object predicates_table = make_vector (patterns_count, Qt); Lisp_Object predicate_signal_data = Qnil; + + struct buffer *old_buf = current_buffer; + set_buffer_internal (buf); + while (ts_query_cursor_next_match (cursor, &match)) { /* Record the checkpoint that we may roll back to. */ @@ -2925,6 +2923,7 @@ the query. */) ts_query_delete (treesit_query); ts_query_cursor_delete (cursor); } + set_buffer_internal (old_buf); /* Some capture predicate signaled an error. */ if (!NILP (predicate_signal_data))