emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

feature/tree-sitter 6a3caeab58 03/12: Remove treesit-query-in


From: Yuan Fu
Subject: feature/tree-sitter 6a3caeab58 03/12: Remove treesit-query-in
Date: Wed, 5 Oct 2022 22:52:20 -0400 (EDT)

branch: feature/tree-sitter
commit 6a3caeab580f61f88c4ad49ff32cdf74eea4fb97
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>

    Remove treesit-query-in
    
    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.
---
 doc/lispref/parsing.texi | 21 +++++----------------
 lisp/treesit.el          | 34 ++++------------------------------
 src/treesit.c            | 30 ++++++++++++++++++++++++++----
 3 files changed, 35 insertions(+), 50 deletions(-)

diff --git a/doc/lispref/parsing.texi b/doc/lispref/parsing.texi
index 32d151d45b..4ae2c4e341 100644
--- a/doc/lispref/parsing.texi
+++ b/doc/lispref/parsing.texi
@@ -867,6 +867,11 @@ 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.
 
+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
@@ -879,22 +884,6 @@ 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 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
 
diff --git a/lisp/treesit.el b/lisp/treesit.el
index cf586f9978..91e3d05a51 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -20,11 +20,12 @@
 ;;; 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)
 
@@ -227,33 +228,6 @@ If NAMED is non-nil, count named child only."
 
 ;;; 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."
@@ -272,7 +246,7 @@ QUERY, SOURCE, BEG, END are the same as in
 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))))
@@ -847,7 +821,7 @@ to the offending pattern and highlight the pattern."
     (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
diff --git a/src/treesit.c b/src/treesit.c
index 1a61e354cf..c9bccb123f 100644
--- a/src/treesit.c
+++ b/src/treesit.c
@@ -1691,13 +1691,17 @@ 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 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))
@@ -1707,11 +1711,29 @@ query.  */)
        || 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);
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]