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
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}
;;; 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
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
(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.
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
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))
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. */