aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuan Fu2023-09-04 23:45:21 -0700
committerYuan Fu2023-09-05 21:03:37 -0700
commitcf0986401cee914207e2d81febf62189f74ab40d (patch)
tree5337d011c2df5b7513feb8fa8a69411524d72989
parent686d4ddb87ca2d87bfc0cd8a29ac84decab90ff6 (diff)
downloademacs-cf0986401cee914207e2d81febf62189f74ab40d.tar.gz
emacs-cf0986401cee914207e2d81febf62189f74ab40d.zip
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.
-rw-r--r--doc/lispref/parsing.texi19
-rw-r--r--src/treesit.c29
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
409@var{language} with @var{tag} in @var{buffer}, but if @var{no-reuse} 409@var{language} with @var{tag} in @var{buffer}, but if @var{no-reuse}
410is non-@code{nil}, this function always creates a new parser. 410is non-@code{nil}, this function always creates a new parser.
411 411
412@var{tag} should be a symbol and defaults to @code{nil}. Different 412@var{tag} can be any symbol except @code{t}, and defaults to
413parsers can have the same tag. 413@code{nil}. Different parsers can have the same tag.
414 414
415If that buffer is an indirect buffer, its base buffer is used instead. 415If that buffer is an indirect buffer, its base buffer is used instead.
416That is, indirect buffers use their base buffer's parsers. If the 416That 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,
453Emacs updates parsers in this list so they can update their syntax 453Emacs updates parsers in this list so they can update their syntax
454tree incrementally. 454tree incrementally.
455 455
456@defun treesit-parser-list &optional buffer 456@defun treesit-parser-list &optional buffer language tag
457This function returns the parser list of @var{buffer}. If 457This function returns the parser list of @var{buffer}, filtered by
458@var{buffer} is @code{nil} or omitted, it defaults to the current 458@var{language} and @var{tag}. If @var{buffer} is @code{nil} or
459buffer. If that buffer is an indirect buffer, its base buffer is used 459omitted, it defaults to the current buffer. If that buffer is an
460instead. That is, indirect buffers use their base buffer's parsers. 460indirect buffer, its base buffer is used instead. That is, indirect
461buffers use their base buffer's parsers.
462
463If @var{language} is non-@var{nil}, only include parsers for that
464language. And only include parsers with @var{tag}. @var{tag} defaults
465to @code{nil}.
461@end defun 466@end defun
462 467
463@defun treesit-parser-delete parser 468@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
1390already has a parser for LANGUAGE with TAG, return that parser, but if 1390already has a parser for LANGUAGE with TAG, return that parser, but if
1391NO-REUSE is non-nil, always create a new parser. 1391NO-REUSE is non-nil, always create a new parser.
1392 1392
1393TAG should be a symbol and defaults to nil. Different parsers can 1393TAG can be any symbol except t, and defaults to nil. Different
1394have the same tag. 1394parsers can have the same tag.
1395 1395
1396If that buffer is an indirect buffer, its base buffer is used instead. 1396If that buffer is an indirect buffer, its base buffer is used instead.
1397That is, indirect buffers use their base buffer's parsers. Lisp 1397That is, indirect buffers use their base buffer's parsers. Lisp
@@ -1415,6 +1415,9 @@ an indirect buffer. */)
1415 if (buf->base_buffer) 1415 if (buf->base_buffer)
1416 buf = buf->base_buffer; 1416 buf = buf->base_buffer;
1417 1417
1418 if (EQ (tag, Qt))
1419 xsignal2(Qwrong_type_argument, list2(Qnot, Qt), Qt);
1420
1418 treesit_check_buffer_size (buf); 1421 treesit_check_buffer_size (buf);
1419 1422
1420 /* See if we can reuse a parser. */ 1423 /* See if we can reuse a parser. */
@@ -1472,15 +1475,22 @@ See `treesit-parser-list' for the buffer's parser list. */)
1472 return Qnil; 1475 return Qnil;
1473} 1476}
1474 1477
1478/* If TAG is t, include all parsers regardless of their tag. But
1479 don't document this until there's some actual need for it. */
1475DEFUN ("treesit-parser-list", 1480DEFUN ("treesit-parser-list",
1476 Ftreesit_parser_list, Streesit_parser_list, 1481 Ftreesit_parser_list, Streesit_parser_list,
1477 0, 1, 0, 1482 0, 3, 0,
1478 doc: /* Return BUFFER's parser list. 1483 doc: /* Return BUFFER's parser list, filtered by LANGUAGE and TAG.
1479 1484
1480BUFFER defaults to the current buffer. If that buffer is an indirect 1485BUFFER defaults to the current buffer. If that buffer is an indirect
1481buffer, its base buffer is used instead. That is, indirect buffers 1486buffer, its base buffer is used instead. That is, indirect buffers
1482use their base buffer's parsers. */) 1487use their base buffer's parsers.
1483 (Lisp_Object buffer) 1488
1489If LANGUAGE is non-nil, only return parsers for that language.
1490
1491The returned list only contain parsers with TAG. TAG defaults to
1492nil. */)
1493 (Lisp_Object buffer, Lisp_Object language, Lisp_Object tag)
1484{ 1494{
1485 struct buffer *buf; 1495 struct buffer *buf;
1486 if (NILP (buffer)) 1496 if (NILP (buffer))
@@ -1501,7 +1511,12 @@ use their base buffer's parsers. */)
1501 tail = BVAR (buf, ts_parser_list); 1511 tail = BVAR (buf, ts_parser_list);
1502 1512
1503 FOR_EACH_TAIL (tail) 1513 FOR_EACH_TAIL (tail)
1504 return_list = Fcons (XCAR (tail), return_list); 1514 {
1515 struct Lisp_TS_Parser *parser = XTS_PARSER (XCAR (tail));
1516 if ((NILP (language) || EQ (language, parser->language_symbol))
1517 && (EQ (tag, Qt) || EQ (tag, parser->tag)))
1518 return_list = Fcons (XCAR (tail), return_list);
1519 }
1505 1520
1506 return Freverse (return_list); 1521 return Freverse (return_list);
1507} 1522}