diff options
| author | Yuan Fu | 2023-09-04 23:45:21 -0700 |
|---|---|---|
| committer | Yuan Fu | 2023-09-05 21:03:37 -0700 |
| commit | cf0986401cee914207e2d81febf62189f74ab40d (patch) | |
| tree | 5337d011c2df5b7513feb8fa8a69411524d72989 | |
| parent | 686d4ddb87ca2d87bfc0cd8a29ac84decab90ff6 (diff) | |
| download | emacs-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.texi | 19 | ||||
| -rw-r--r-- | 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 | |||
| 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} |
| 410 | is non-@code{nil}, this function always creates a new parser. | 410 | is 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 |
| 413 | parsers can have the same tag. | 413 | @code{nil}. Different parsers can have the same tag. |
| 414 | 414 | ||
| 415 | If that buffer is an indirect buffer, its base buffer is used instead. | 415 | If that buffer is an indirect buffer, its base buffer is used instead. |
| 416 | That is, indirect buffers use their base buffer's parsers. If the | 416 | 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, | |||
| 453 | Emacs updates parsers in this list so they can update their syntax | 453 | Emacs updates parsers in this list so they can update their syntax |
| 454 | tree incrementally. | 454 | tree incrementally. |
| 455 | 455 | ||
| 456 | @defun treesit-parser-list &optional buffer | 456 | @defun treesit-parser-list &optional buffer language tag |
| 457 | This function returns the parser list of @var{buffer}. If | 457 | This 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 |
| 459 | buffer. If that buffer is an indirect buffer, its base buffer is used | 459 | omitted, it defaults to the current buffer. If that buffer is an |
| 460 | instead. That is, indirect buffers use their base buffer's parsers. | 460 | indirect buffer, its base buffer is used instead. That is, indirect |
| 461 | buffers use their base buffer's parsers. | ||
| 462 | |||
| 463 | If @var{language} is non-@var{nil}, only include parsers for that | ||
| 464 | language. And only include parsers with @var{tag}. @var{tag} defaults | ||
| 465 | to @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 | |||
| 1390 | already has a parser for LANGUAGE with TAG, return that parser, but if | 1390 | already has a parser for LANGUAGE with TAG, return that parser, but if |
| 1391 | NO-REUSE is non-nil, always create a new parser. | 1391 | NO-REUSE is non-nil, always create a new parser. |
| 1392 | 1392 | ||
| 1393 | TAG should be a symbol and defaults to nil. Different parsers can | 1393 | TAG can be any symbol except t, and defaults to nil. Different |
| 1394 | have the same tag. | 1394 | parsers can have the same tag. |
| 1395 | 1395 | ||
| 1396 | If that buffer is an indirect buffer, its base buffer is used instead. | 1396 | If that buffer is an indirect buffer, its base buffer is used instead. |
| 1397 | That is, indirect buffers use their base buffer's parsers. Lisp | 1397 | That 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. */ | ||
| 1475 | DEFUN ("treesit-parser-list", | 1480 | DEFUN ("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 | ||
| 1480 | BUFFER defaults to the current buffer. If that buffer is an indirect | 1485 | BUFFER defaults to the current buffer. If that buffer is an indirect |
| 1481 | buffer, its base buffer is used instead. That is, indirect buffers | 1486 | buffer, its base buffer is used instead. That is, indirect buffers |
| 1482 | use their base buffer's parsers. */) | 1487 | use their base buffer's parsers. |
| 1483 | (Lisp_Object buffer) | 1488 | |
| 1489 | If LANGUAGE is non-nil, only return parsers for that language. | ||
| 1490 | |||
| 1491 | The returned list only contain parsers with TAG. TAG defaults to | ||
| 1492 | nil. */) | ||
| 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 | } |