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.
This commit is contained in:
Yuan Fu 2023-03-24 12:25:19 -07:00
parent a37f19b14a
commit db7e95531a
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
2 changed files with 7 additions and 7 deletions

View file

@ -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

View file

@ -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))