Because treesit-query-capture can now do everything it does.
* doc/lispref/parsing.texi (Pattern Matching): Update manual.
* lisp/treesit.el (treesit-query-in): Remove function.
* src/treesit.c (Ftreesit_query_capture): Accept parser and language
symbol as NODE.
s-expression syntax and compiled query are described at the end of the
section.
+Parameter @var{node} can also be a parser or a language symbol. A
+parser means using its root node, a language symbol means find or
+create a parser for that language in the current buffer, and use the
+root node.
+
The function returns all captured nodes in a list of
@code{(@var{capture_name} . @var{node})}. If @var{node-only} is
non-nil, a list of node is returned instead. If @var{beg} and
error. You can use @code{treesit-query-validate} to debug the query.
@end defun
-@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{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}
-represents the root node of the first parser for that language in the
-current buffer; if a parser, @var{source} represents the root node of
-that parser.
-
-This function also raises @var{treesit-query-error}.
-@end defun
-
For example, suppose @var{node}'s content is @code{1 + 2}, and
@var{query} is
;;; Commentary:
;;
;; Note to self: we don't create parsers automatically in any provided
-;; functions.
+;; functions if we don't know what language to use.
;;; Code:
(eval-when-compile (require 'cl-lib))
+(eval-when-compile (require 'subr-x)) ; For `string-join'.
(require 'cl-seq)
(require 'font-lock)
;;; Query API supplement
-(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
-language symbol, use the root node of the first parser for that
-language; if a parser, use the root node of that parser; if a
-node, use that node.
-
-QUERY is either a string query, a sexp query, or a compiled
-query. See Info node `(elisp)Pattern Matching' for how to write
-a query in either string or s-expression form. When using
-repeatedly, a compiled 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. If NODE-ONLY non-nil, return a list of nodes.
-
-Raise an treesit-query-error if QUERY is malformed."
- (treesit-query-capture
- (cond ((symbolp source) (treesit-buffer-root-node source))
- ((treesit-parser-p source)
- (treesit-parser-root-node source))
- ((treesit-node-p source) source))
- query
- beg end node-only))
-
(defun treesit-query-string (string query language)
"Query STRING with QUERY in LANGUAGE.
See `treesit-query-capture' for QUERY."
of (START . END), where START and END specifics the range of each
captured node. Capture names don't matter."
(cl-loop for capture
- in (treesit-query-in source query beg end)
+ in (treesit-query-capture source query beg end)
for node = (cdr capture)
collect (cons (treesit-node-start node)
(treesit-node-end node))))
(with-temp-buffer
(treesit-parser-create language)
(condition-case err
- (progn (treesit-query-in language query)
+ (progn (treesit-query-capture language query)
(message "QUERY is valid"))
(treesit-query-error
(with-current-buffer buf
BEG and END, if both non-nil, specifies the range in which the query
is executed. If NODE-ONLY is non-nil, return a list of nodes.
+Besides a node, NODE can also be a parser, then the root node of that
+parser is used; NODE can be a language symbol, then the root node of a
+parser for that language is used. If such a parser doesn't exist, it
+is created.
+
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 node_only)
{
- ts_check_node (node);
if (!NILP (beg))
CHECK_INTEGER (beg);
if (!NILP (end))
|| CONSP (query) || STRINGP (query)))
wrong_type_argument (Qtreesit_query_p, query);
+
+ Lisp_Object lisp_node;
+ if (TS_NODEP (node))
+ lisp_node = node;
+ else if (TS_PARSERP (node))
+ lisp_node = Ftreesit_parser_root_node (node);
+ else if (SYMBOLP (node))
+ {
+ Lisp_Object parser
+ = Ftreesit_parser_create (node, Fcurrent_buffer (), Qnil);
+ lisp_node = Ftreesit_parser_root_node (parser);
+ }
+ else
+ xsignal2 (Qwrong_type_argument,
+ list4 (Qor, Qtreesit_node_p,
+ Qtreesit_parser_p, Qsymbolp),
+ node);
+
/* Extract C values from Lisp objects. */
- TSNode ts_node = XTS_NODE (node)->node;
- Lisp_Object lisp_parser = XTS_NODE (node)->parser;
+ TSNode ts_node = XTS_NODE (lisp_node)->node;
+ Lisp_Object lisp_parser = XTS_NODE (lisp_node)->parser;
ptrdiff_t visible_beg =
- XTS_PARSER (XTS_NODE (node)->parser)->visible_beg;
+ XTS_PARSER (XTS_NODE (lisp_node)->parser)->visible_beg;
const TSLanguage *lang = ts_parser_language
(XTS_PARSER (lisp_parser)->parser);