From: Yuan Fu Date: Thu, 8 Sep 2022 19:52:25 +0000 (-0700) Subject: Add node-only parameter to treesit-query-capture X-Git-Tag: emacs-29.0.90~1887 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=1d3234988a32d32570729b4dfcc00712636ec450;p=emacs.git Add node-only parameter to treesit-query-capture * doc/lispref/parsing.texi (Pattern Matching): Mention the new parameter. * lisp/treesit.el (treesit-query-in): Add node-only. * src/treesit.c (Ftreesit_query_capture): Add node-only. --- diff --git a/doc/lispref/parsing.texi b/doc/lispref/parsing.texi index 0a025bd2499..ac156d9996d 100644 --- a/doc/lispref/parsing.texi +++ b/doc/lispref/parsing.texi @@ -874,15 +874,17 @@ name @code{biexp}: Now we can introduce the query functions. -@defun treesit-query-capture node query &optional beg end +@defun treesit-query-capture node query &optional beg end node-only This function matches patterns in @var{query} in @var{node}. Argument @var{query} can be either a string, a s-expression, or a compiled query object. For now, we focus on the string syntax; s-expression syntax and compiled query are described at the end of the section. The function returns all captured nodes in a list of -@code{(@var{capture_name} . @var{node})}. If @var{beg} and @var{end} -are both non-nil, it only pattern matches nodes in that range. +@code{(@var{capture_name} . @var{node})}. If @var{node-only} is +non-nil, a list of node is returned instead. If @var{beg} and +@var{end} are both non-nil, this function only pattern matches nodes +in that range. @vindex treesit-query-error This function raise a @var{treesit-query-error} if @var{query} is @@ -890,11 +892,12 @@ malformed. The signal data contains a description of the specific error. You can use @code{treesit-query-validate} to debug the query. @end defun -@defun treesit-query-in source query &optional beg end +@defun treesit-query-in source query &optional beg end node-only This function matches patterns in @var{query} in @var{source}, and returns all captured nodes in a list of @code{(@var{capture_name} -. @var{node})}. If @var{beg} and @var{end} are both non-nil, it only -pattern match nodes in that range. +. @var{node})}. If @var{node-only} is non-nil, a list of node is +returned instead. If @var{beg} and @var{end} are both non-nil, it +only pattern match nodes in that range. Argument @var{source} designates a node, it can be a language symbol, a parser, or simply a node. If a language symbol, @var{source} diff --git a/lisp/treesit.el b/lisp/treesit.el index a374ceda6d5..709f826f325 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -350,7 +350,7 @@ If NAMED is non-nil, count named child only." ;;; Query API supplement -(defun treesit-query-in (source query &optional beg end) +(defun treesit-query-in (source query &optional beg end node-only) "Query the current buffer with QUERY. SOURCE can be a language symbol, a parser, or a node. If a @@ -366,7 +366,7 @@ one, so it is recommend to compile your queries if it will be used over and over. BEG and END, if _both_ non-nil, specifies the range in which the query -is executed. +is executed. If NODE-ONLY non-nil, return a list of nodes. Raise an treesit-query-error if QUERY is malformed." (treesit-query-capture @@ -375,7 +375,7 @@ Raise an treesit-query-error if QUERY is malformed." (treesit-parser-root-node source)) ((treesit-node-p source) source)) query - beg end)) + beg end node-only)) (defun treesit-query-string (string query language) "Query STRING with QUERY in LANGUAGE. diff --git a/src/treesit.c b/src/treesit.c index 48de9436d2b..2b3ab643fa7 100644 --- a/src/treesit.c +++ b/src/treesit.c @@ -1678,7 +1678,7 @@ query. */) DEFUN ("treesit-query-capture", Ftreesit_query_capture, - Streesit_query_capture, 2, 4, 0, + Streesit_query_capture, 2, 5, 0, doc: /* Query NODE with patterns in QUERY. Return a list of (CAPTURE_NAME . NODE). CAPTURE_NAME is the name @@ -1691,13 +1691,13 @@ query is much faster than a string or sexp one, so it is recommend to compile your queries if it will be used over and over. BEG and END, if both non-nil, specifies the range in which the query -is executed. +is executed. If NODE-ONLY is non-nil, return a list of nodes. Signals treesit-query-error if QUERY is malformed or something else goes wrong. You can use `treesit-query-validate' to debug the query. */) (Lisp_Object node, Lisp_Object query, - Lisp_Object beg, Lisp_Object end) + Lisp_Object beg, Lisp_Object end, Lisp_Object node_only) { ts_check_node (node); if (!NILP (beg)) @@ -1775,11 +1775,20 @@ query. */) TSQueryCapture capture = captures[idx]; Lisp_Object captured_node = make_ts_node(lisp_parser, capture.node); - const char *capture_name = ts_query_capture_name_for_id - (ts_query, capture.index, &capture_name_len); - Lisp_Object cap = - Fcons (intern_c_string_1 (capture_name, capture_name_len), - captured_node); + + Lisp_Object cap; + if (NILP (node_only)) + { + const char *capture_name = ts_query_capture_name_for_id + (ts_query, capture.index, &capture_name_len); + cap = + Fcons (intern_c_string_1 (capture_name, capture_name_len), + captured_node); + } + else + { + cap = captured_node; + } result = Fcons (cap, result); } /* Get predicates. */