emacs-diffs
[Top][All Lists]
Advanced

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

master cf0986401ce 3/7: Allow filter by tag in treesit-parser-list


From: Yuan Fu
Subject: master cf0986401ce 3/7: Allow filter by tag in treesit-parser-list
Date: Wed, 6 Sep 2023 00:03:49 -0400 (EDT)

branch: master
commit cf0986401cee914207e2d81febf62189f74ab40d
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>

    Allow filter by tag in treesit-parser-list
    
    * doc/lispref/parsing.texi: Update manual.
    * src/treesit.c (Ftreesit_parser_create): Disallow using t for tag.
    (Ftreesit_parser_list): Add LANGUAGE and TAG parameter.
---
 doc/lispref/parsing.texi | 19 ++++++++++++-------
 src/treesit.c            | 29 ++++++++++++++++++++++-------
 2 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/doc/lispref/parsing.texi b/doc/lispref/parsing.texi
index 87c381b161d..738ce322c57 100644
--- a/doc/lispref/parsing.texi
+++ b/doc/lispref/parsing.texi
@@ -409,8 +409,8 @@ By default, this function reuses a parser if one already 
exists for
 @var{language} with @var{tag} in @var{buffer}, but if @var{no-reuse}
 is non-@code{nil}, this function always creates a new parser.
 
-@var{tag} should be a symbol and defaults to @code{nil}.  Different
-parsers can have the same tag.
+@var{tag} can be any symbol except @code{t}, and defaults to
+@code{nil}.  Different parsers can have the same tag.
 
 If that buffer is an indirect buffer, its base buffer is used instead.
 That is, indirect buffers use their base buffer's parsers.  If the
@@ -453,11 +453,16 @@ internal parser list.  Every time a change is made to the 
buffer,
 Emacs updates parsers in this list so they can update their syntax
 tree incrementally.
 
-@defun treesit-parser-list &optional buffer
-This function returns the parser list of @var{buffer}.  If
-@var{buffer} is @code{nil} or omitted, it defaults to the current
-buffer.  If that buffer is an indirect buffer, its base buffer is used
-instead.  That is, indirect buffers use their base buffer's parsers.
+@defun treesit-parser-list &optional buffer language tag
+This function returns the parser list of @var{buffer}, filtered by
+@var{language} and @var{tag}.  If @var{buffer} is @code{nil} or
+omitted, it defaults to the current buffer.  If that buffer is an
+indirect buffer, its base buffer is used instead.  That is, indirect
+buffers use their base buffer's parsers.
+
+If @var{language} is non-@var{nil}, only include parsers for that
+language. And only include parsers with @var{tag}.  @var{tag} defaults
+to @code{nil}.
 @end defun
 
 @defun treesit-parser-delete parser
diff --git a/src/treesit.c b/src/treesit.c
index 13be9594963..dfd098e3c85 100644
--- a/src/treesit.c
+++ b/src/treesit.c
@@ -1390,8 +1390,8 @@ is nil or omitted, it defaults to the current buffer.  If 
BUFFER
 already has a parser for LANGUAGE with TAG, return that parser, but if
 NO-REUSE is non-nil, always create a new parser.
 
-TAG should be a symbol and defaults to nil.  Different parsers can
-have the same tag.
+TAG can be any symbol except t, and defaults to nil.  Different
+parsers can have the same tag.
 
 If that buffer is an indirect buffer, its base buffer is used instead.
 That is, indirect buffers use their base buffer's parsers.  Lisp
@@ -1415,6 +1415,9 @@ an indirect buffer.  */)
   if (buf->base_buffer)
     buf = buf->base_buffer;
 
+  if (EQ (tag, Qt))
+    xsignal2(Qwrong_type_argument, list2(Qnot, Qt), Qt);
+
   treesit_check_buffer_size (buf);
 
   /* See if we can reuse a parser.  */
@@ -1472,15 +1475,22 @@ See `treesit-parser-list' for the buffer's parser list. 
 */)
   return Qnil;
 }
 
+/* If TAG is t, include all parsers regardless of their tag.  But
+   don't document this until there's some actual need for it.  */
 DEFUN ("treesit-parser-list",
        Ftreesit_parser_list, Streesit_parser_list,
-       0, 1, 0,
-       doc: /* Return BUFFER's parser list.
+       0, 3, 0,
+       doc: /* Return BUFFER's parser list, filtered by LANGUAGE and TAG.
 
 BUFFER defaults to the current buffer.  If that buffer is an indirect
 buffer, its base buffer is used instead.  That is, indirect buffers
-use their base buffer's parsers.  */)
-  (Lisp_Object buffer)
+use their base buffer's parsers.
+
+If LANGUAGE is non-nil, only return parsers for that language.
+
+The returned list only contain parsers with TAG.  TAG defaults to
+nil.  */)
+  (Lisp_Object buffer, Lisp_Object language, Lisp_Object tag)
 {
   struct buffer *buf;
   if (NILP (buffer))
@@ -1501,7 +1511,12 @@ use their base buffer's parsers.  */)
   tail = BVAR (buf, ts_parser_list);
 
   FOR_EACH_TAIL (tail)
-    return_list = Fcons (XCAR (tail), return_list);
+    {
+      struct Lisp_TS_Parser *parser = XTS_PARSER (XCAR (tail));
+      if ((NILP (language) || EQ (language, parser->language_symbol))
+         && (EQ (tag, Qt) || EQ (tag, parser->tag)))
+       return_list = Fcons (XCAR (tail), return_list);
+    }
 
   return Freverse (return_list);
 }



reply via email to

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