diff options
| author | Eli Zaretskii | 2025-02-28 09:12:05 +0200 |
|---|---|---|
| committer | Eli Zaretskii | 2025-02-28 09:12:05 +0200 |
| commit | 6ed119d3052ffebd20450ec0c7fb3abf863b3a49 (patch) | |
| tree | 01b6fdb5cdfeecebda3004861bb27d76a2f57948 | |
| parent | becdfb11a5b13bc773729236763e99e513806451 (diff) | |
| download | emacs-6ed119d3052ffebd20450ec0c7fb3abf863b3a49.tar.gz emacs-6ed119d3052ffebd20450ec0c7fb3abf863b3a49.zip | |
; Fix documentation of recent treesit changes
* src/treesit.c (Ftreesit_query_capture)
(Ftreesit_parser_embed_level, Ftreesit_parser_set_embed_level)
(Ftreesit_parser_set_parent_node):
* lisp/treesit.el (treesit-query-range)
(treesit-query-range-by-language, treesit-range-settings)
(treesit-range-rules, treesit--parser-at-level)
(treesit--update-ranges-non-local, treesit--update-ranges-local)
(treesit--update-range-1): Fix wording and typos in doc strings.
* doc/lispref/parsing.texi (Pattern Matching): Fix wording.
| -rw-r--r-- | doc/lispref/parsing.texi | 5 | ||||
| -rw-r--r-- | lisp/treesit.el | 38 | ||||
| -rw-r--r-- | src/treesit.c | 17 | ||||
| -rw-r--r-- | src/treesit.h | 15 |
4 files changed, 39 insertions, 36 deletions
diff --git a/doc/lispref/parsing.texi b/doc/lispref/parsing.texi index 2b224036de0..ade7b819756 100644 --- a/doc/lispref/parsing.texi +++ b/doc/lispref/parsing.texi | |||
| @@ -1247,9 +1247,8 @@ matching node whose span overlaps with the region between @var{beg} | |||
| 1247 | and @var{end} is captured; it doesn't have to be completely contained | 1247 | and @var{end} is captured; it doesn't have to be completely contained |
| 1248 | in the region. | 1248 | in the region. |
| 1249 | 1249 | ||
| 1250 | If @var{grouped} is non-@code{nil}, instead of returning a list of | 1250 | If @var{grouped} is non-@code{nil}, this function returns a grouped list |
| 1251 | @w{@code{(@var{capture_name} . @var{node})}}, this function returns a | 1251 | of lists of captured nodes. The grouping is determined by @var{query}. |
| 1252 | list of list of it. The grouping is determined by @var{query}. | ||
| 1253 | Captures in the same match of a pattern in @var{query} are grouped | 1252 | Captures in the same match of a pattern in @var{query} are grouped |
| 1254 | together. | 1253 | together. |
| 1255 | 1254 | ||
diff --git a/lisp/treesit.el b/lisp/treesit.el index e05e3526c15..e958b2cad64 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el | |||
| @@ -153,7 +153,7 @@ of max unsigned 32-bit value for byte offsets into buffer text." | |||
| 153 | 153 | ||
| 154 | ;;; Parser API supplement | 154 | ;;; Parser API supplement |
| 155 | 155 | ||
| 156 | ;; The primary parser will be access frequently (after each re-parse, | 156 | ;; The primary parser will be accessed frequently (after each re-parse, |
| 157 | ;; before redisplay, etc, see | 157 | ;; before redisplay, etc, see |
| 158 | ;; `treesit--font-lock-mark-ranges-to-fontify'), so we don't want to | 158 | ;; `treesit--font-lock-mark-ranges-to-fontify'), so we don't want to |
| 159 | ;; allow it to be a callback function which returns the primary parser | 159 | ;; allow it to be a callback function which returns the primary parser |
| @@ -559,8 +559,8 @@ respective offset values are added to each (START . END) range | |||
| 559 | being returned. Capture names generally don't matter, but names | 559 | being returned. Capture names generally don't matter, but names |
| 560 | that starts with an underscore are ignored. | 560 | that starts with an underscore are ignored. |
| 561 | 561 | ||
| 562 | RANGE-FN, if non-nil, is a function that takes a node and OFFSET, and | 562 | RANGE-FN, if non-nil, is a function that takes a NODE and OFFSET, and |
| 563 | returns the ranges to use for that node." | 563 | returns the ranges to use for that NODE." |
| 564 | (let ((offset-left (or (car offset) 0)) | 564 | (let ((offset-left (or (car offset) 0)) |
| 565 | (offset-right (or (cdr offset) 0))) | 565 | (offset-right (or (cdr offset) 0))) |
| 566 | (cl-loop for capture | 566 | (cl-loop for capture |
| @@ -585,8 +585,8 @@ Query NODE with QUERY, the captured nodes generates ranges. Nodes | |||
| 585 | captured by the `@language' capture name are converted to language | 585 | captured by the `@language' capture name are converted to language |
| 586 | symbols with LANGUAGE-FN. | 586 | symbols with LANGUAGE-FN. |
| 587 | 587 | ||
| 588 | RANGE-FN, if non-nil, is a function that takes a node and OFFSET, and | 588 | RANGE-FN, if non-nil, is a function that takes a NODE and OFFSET, and |
| 589 | returns the ranges to use for that node. | 589 | returns the ranges to use for that NODE. |
| 590 | 590 | ||
| 591 | BEG, END, OFFSET are the same as in `treesit-query-range'." | 591 | BEG, END, OFFSET are the same as in `treesit-query-range'." |
| 592 | (let ((offset-left (or (car offset) 0)) | 592 | (let ((offset-left (or (car offset) 0)) |
| @@ -644,13 +644,13 @@ using a single parser for all the ranges. If OFFSET is non-nil, it | |||
| 644 | should be a cons of numbers (START-OFFSET . END-OFFSET), where the start | 644 | should be a cons of numbers (START-OFFSET . END-OFFSET), where the start |
| 645 | and end offset are added to each queried range to get the result ranges. | 645 | and end offset are added to each queried range to get the result ranges. |
| 646 | 646 | ||
| 647 | If RANGE-FN is non-nil, it should be a function, Emacs uses this | 647 | If RANGE-FN is non-nil, it should be a function; Emacs uses this |
| 648 | function to compute the ranges to use for the embedded parser. The | 648 | function to compute the ranges to use for the embedded parser. The |
| 649 | function is passed the captured node and OFFSET, and should return a | 649 | function is passed the captured node and OFFSET, and should return a |
| 650 | list of ranges, where each range is a cons of the start and end | 650 | list of ranges, where each range is a cons of the start and end |
| 651 | position. | 651 | position. |
| 652 | 652 | ||
| 653 | Capture names generally don't matter, but names that starts with | 653 | Capture names generally don't matter, but names that start with |
| 654 | an underscore are ignored. | 654 | an underscore are ignored. |
| 655 | 655 | ||
| 656 | QUERY can also be a function, in which case it is called with 2 | 656 | QUERY can also be a function, in which case it is called with 2 |
| @@ -672,8 +672,8 @@ like this: | |||
| 672 | Each QUERY is a tree-sitter query in either the string, | 672 | Each QUERY is a tree-sitter query in either the string, |
| 673 | s-expression or compiled form. | 673 | s-expression or compiled form. |
| 674 | 674 | ||
| 675 | Capture names generally don't matter, but names that starts with an | 675 | Capture names generally don't matter, but names that start with an |
| 676 | underscore are ignored. And `@language' is reserved. | 676 | underscore are ignored. The `@language' capture name is reserved. |
| 677 | 677 | ||
| 678 | For each QUERY, :KEYWORD and VALUE pairs add meta information to | 678 | For each QUERY, :KEYWORD and VALUE pairs add meta information to |
| 679 | it. For example, | 679 | it. For example, |
| @@ -690,8 +690,8 @@ this way: Emacs queries QUERY in the host language's parser, | |||
| 690 | computes the ranges spanned by the captured nodes, and applies | 690 | computes the ranges spanned by the captured nodes, and applies |
| 691 | these ranges to parsers for the embedded language. | 691 | these ranges to parsers for the embedded language. |
| 692 | 692 | ||
| 693 | If the embed language is dynamic, then a function in place of the embed | 693 | If the embed language is dynamic, then `:embed' can specify a |
| 694 | language symbol. This function will by passed a node and should return | 694 | function. This function will by passed a node and should return |
| 695 | the language symbol for the embedded code block. The node is the one | 695 | the language symbol for the embedded code block. The node is the one |
| 696 | captured from QUERY with capture name `@language'. Also make sure the | 696 | captured from QUERY with capture name `@language'. Also make sure the |
| 697 | code block and language capture are in the same match group. | 697 | code block and language capture are in the same match group. |
| @@ -896,10 +896,10 @@ it." | |||
| 896 | (delete-overlay ov))))) | 896 | (delete-overlay ov))))) |
| 897 | 897 | ||
| 898 | (defsubst treesit--parser-at-level (parsers level &optional include-null) | 898 | (defsubst treesit--parser-at-level (parsers level &optional include-null) |
| 899 | "Filter for parsers in PARSERS that has embed level equal to LEVEL. | 899 | "Filter for parsers in PARSERS that have embed level equal to LEVEL. |
| 900 | 900 | ||
| 901 | If INCLUDE-NULL is non-nil, also include parsers that has a nil embed | 901 | If INCLUDE-NULL is non-nil, also include parsers whose embed level |
| 902 | level." | 902 | is nil." |
| 903 | (seq-filter (lambda (parser) | 903 | (seq-filter (lambda (parser) |
| 904 | (or (eq (treesit-parser-embed-level parser) level) | 904 | (or (eq (treesit-parser-embed-level parser) level) |
| 905 | (and include-null | 905 | (and include-null |
| @@ -918,7 +918,7 @@ Use QUERY to get the ranges, and set ranges for embedded parsers to | |||
| 918 | those ranges. HOST-PARSER and QUERY must match. | 918 | those ranges. HOST-PARSER and QUERY must match. |
| 919 | 919 | ||
| 920 | EMBED-LANG is either a language symbol or a function that takes a node | 920 | EMBED-LANG is either a language symbol or a function that takes a node |
| 921 | and return a language symbol. | 921 | and returns a language symbol. |
| 922 | 922 | ||
| 923 | EMBED-LEVEL is the embed level for the local parsers being created or | 923 | EMBED-LEVEL is the embed level for the local parsers being created or |
| 924 | updated. When looking for existing local parsers, only look for parsers | 924 | updated. When looking for existing local parsers, only look for parsers |
| @@ -928,7 +928,7 @@ level. | |||
| 928 | RANGE-FN, if non-nil, is a function that takes a node and OFFSET, and | 928 | RANGE-FN, if non-nil, is a function that takes a node and OFFSET, and |
| 929 | returns the ranges to use for that node. | 929 | returns the ranges to use for that node. |
| 930 | 930 | ||
| 931 | Return updated parsers in a list." | 931 | Return updated parsers as a list." |
| 932 | (let ((ranges-by-lang | 932 | (let ((ranges-by-lang |
| 933 | (if (functionp embed-lang) | 933 | (if (functionp embed-lang) |
| 934 | (treesit-query-range-by-language | 934 | (treesit-query-range-by-language |
| @@ -998,7 +998,7 @@ for the local parser. | |||
| 998 | RANGE-FN, if non-nil, is a function that takes a node and OFFSET, and | 998 | RANGE-FN, if non-nil, is a function that takes a node and OFFSET, and |
| 999 | returns the ranges to use for that node. | 999 | returns the ranges to use for that node. |
| 1000 | 1000 | ||
| 1001 | Return the created local parsers in a list." | 1001 | Return the created local parsers as a list." |
| 1002 | ;; Update range. | 1002 | ;; Update range. |
| 1003 | (let ((ranges-by-lang | 1003 | (let ((ranges-by-lang |
| 1004 | (if (functionp embedded-lang) | 1004 | (if (functionp embedded-lang) |
| @@ -1050,8 +1050,8 @@ Return the created local parsers in a list." | |||
| 1050 | "Given a HOST-PARSER, update ranges between BEG and END. | 1050 | "Given a HOST-PARSER, update ranges between BEG and END. |
| 1051 | 1051 | ||
| 1052 | Go over each settings in SETTINGS, try to create or update the embedded | 1052 | Go over each settings in SETTINGS, try to create or update the embedded |
| 1053 | language in that setting. Return the created or updated embedded | 1053 | language in that setting. Return the list of the created or updated |
| 1054 | language parsers in a list. | 1054 | embedded language parsers. |
| 1055 | 1055 | ||
| 1056 | EMBED-LEVEL is the embed level for the embedded parser being created or | 1056 | EMBED-LEVEL is the embed level for the embedded parser being created or |
| 1057 | updated. When looking for existing embedded parsers, only look for | 1057 | updated. When looking for existing embedded parsers, only look for |
diff --git a/src/treesit.c b/src/treesit.c index c8af17a5b8b..20c3626732a 100644 --- a/src/treesit.c +++ b/src/treesit.c | |||
| @@ -1827,8 +1827,8 @@ DEFUN ("treesit-parser-embed-level", | |||
| 1827 | 1827 | ||
| 1828 | The embed level can be either nil or a non-negative integer. A value of | 1828 | The embed level can be either nil or a non-negative integer. A value of |
| 1829 | nil means the parser isn't part of the embedded parser tree. The | 1829 | nil means the parser isn't part of the embedded parser tree. The |
| 1830 | primary parser has embed level 0, from it, each layer of embedded parser | 1830 | primary parser has embed level 0, and each additional layer of parser |
| 1831 | has +1 embed level. */) | 1831 | embedding increments the embed level by 1. */) |
| 1832 | (Lisp_Object parser) | 1832 | (Lisp_Object parser) |
| 1833 | { | 1833 | { |
| 1834 | treesit_check_parser (parser); | 1834 | treesit_check_parser (parser); |
| @@ -1839,7 +1839,9 @@ has +1 embed level. */) | |||
| 1839 | DEFUN ("treesit-parser-set-embed-level", | 1839 | DEFUN ("treesit-parser-set-embed-level", |
| 1840 | Ftreesit_parser_set_embed_level, Streesit_parser_set_embed_level, | 1840 | Ftreesit_parser_set_embed_level, Streesit_parser_set_embed_level, |
| 1841 | 2, 2, 0, | 1841 | 2, 2, 0, |
| 1842 | doc: /* Set the embed level for PARSER to LEVEL. */) | 1842 | doc: /* Set the embed level for PARSER to LEVEL. |
| 1843 | LEVEL can be nil, for a parser that is not part of an embedded parser | ||
| 1844 | tree; otherwise it must be a non-negative integer. */) | ||
| 1843 | (Lisp_Object parser, Lisp_Object level) | 1845 | (Lisp_Object parser, Lisp_Object level) |
| 1844 | { | 1846 | { |
| 1845 | treesit_check_parser (parser); | 1847 | treesit_check_parser (parser); |
| @@ -1859,7 +1861,7 @@ DEFUN ("treesit-parser-parent-node", | |||
| 1859 | 1, 1, 0, | 1861 | 1, 1, 0, |
| 1860 | doc: /* Return PARSER's parent node, if one exists. | 1862 | doc: /* Return PARSER's parent node, if one exists. |
| 1861 | 1863 | ||
| 1862 | Only embeded local parser can have parent node. When Emacs uses a node | 1864 | Only embeded local parsers can have parent node. When Emacs uses a node |
| 1863 | in the host parser to create this local parser, that node is considered | 1865 | in the host parser to create this local parser, that node is considered |
| 1864 | the parent node of the local parser. */) | 1866 | the parent node of the local parser. */) |
| 1865 | (Lisp_Object parser) | 1867 | (Lisp_Object parser) |
| @@ -1871,7 +1873,7 @@ the parent node of the local parser. */) | |||
| 1871 | DEFUN ("treesit-parser-set-parent-node", | 1873 | DEFUN ("treesit-parser-set-parent-node", |
| 1872 | Ftreesit_parser_set_parent_node, Streesit_parser_set_parent_node, | 1874 | Ftreesit_parser_set_parent_node, Streesit_parser_set_parent_node, |
| 1873 | 2, 2, 0, | 1875 | 2, 2, 0, |
| 1874 | doc: /* Return PARSER's parent node to NODE. */) | 1876 | doc: /* Make NODE be the parent node of PARSER. */) |
| 1875 | (Lisp_Object parser, Lisp_Object node) | 1877 | (Lisp_Object parser, Lisp_Object node) |
| 1876 | { | 1878 | { |
| 1877 | treesit_check_parser (parser); | 1879 | treesit_check_parser (parser); |
| @@ -3289,8 +3291,9 @@ in which the query is executed. Any matching node whose span overlaps | |||
| 3289 | with the region between BEG and END are captured, it doesn't have to | 3291 | with the region between BEG and END are captured, it doesn't have to |
| 3290 | be completely in the region. | 3292 | be completely in the region. |
| 3291 | 3293 | ||
| 3292 | If GROUPED is non-nil, group captures into matches and return a list of | 3294 | If GROUPED is non-nil, ther function groups the returned list of |
| 3293 | MATCH, where each MATH is a list of (CAPTURE_NAME . NODE). | 3295 | captures into matches and return a list of MATCH, where each MATCH is |
| 3296 | a list of the form (CAPTURE_NAME . NODE). | ||
| 3294 | 3297 | ||
| 3295 | If NODE-ONLY is non-nil, return nodes only, and don't include | 3298 | If NODE-ONLY is non-nil, return nodes only, and don't include |
| 3296 | CAPTURE_NAME. | 3299 | CAPTURE_NAME. |
diff --git a/src/treesit.h b/src/treesit.h index d19a0e76216..d4cfc47ee5d 100644 --- a/src/treesit.h +++ b/src/treesit.h | |||
| @@ -64,12 +64,13 @@ struct Lisp_TS_Parser | |||
| 64 | = 0, end_byte = UINT32_MAX). */ | 64 | = 0, end_byte = UINT32_MAX). */ |
| 65 | Lisp_Object last_set_ranges; | 65 | Lisp_Object last_set_ranges; |
| 66 | /* Parsers for embedded code blocks will have a non-zero embed level. | 66 | /* Parsers for embedded code blocks will have a non-zero embed level. |
| 67 | The primary parser has level 0, and each layer of embedded parser | 67 | The primary parser has level 0, and each additional layer of parser |
| 68 | gets +1 level. The embed level can be either a non-negative | 68 | embedding increments the leve by 1. The embed level can be either |
| 69 | integer or nil. Every parser created by treesit-parser-create | 69 | a non-negative integer or nil. Every parser created by |
| 70 | starts with a nil level. If the value is nil, that means the range | 70 | 'treesit-parser-create' starts with a nil level. If the value is |
| 71 | functions (treesit-update-ranges and friends) haven't touched this | 71 | nil, that means the range functions (treesit-update-ranges and |
| 72 | parser yet, and this parser isn't part of the embed parser tree. */ | 72 | friends) haven't touched this parser yet, and this parser isn't |
| 73 | part of the embed parser tree. */ | ||
| 73 | Lisp_Object embed_level; | 74 | Lisp_Object embed_level; |
| 74 | /* Some comments: Technically you could calculate embed_level by | 75 | /* Some comments: Technically you could calculate embed_level by |
| 75 | following parent_node, but parent_node might be outdated so it's a | 76 | following parent_node, but parent_node might be outdated so it's a |
| @@ -80,7 +81,7 @@ struct Lisp_TS_Parser | |||
| 80 | for a parser will be useful beyond this. And we can always convert | 81 | for a parser will be useful beyond this. And we can always convert |
| 81 | these to properties later, but not vice versa. */ | 82 | these to properties later, but not vice versa. */ |
| 82 | /* When an embedded parser is created, it's usually based on a node in | 83 | /* When an embedded parser is created, it's usually based on a node in |
| 83 | the host parser. This field saves that node so it possible to | 84 | the host parser. This field saves that node so it's possible to |
| 84 | climb up and out of the embedded parser into the host parser. Note | 85 | climb up and out of the embedded parser into the host parser. Note |
| 85 | that the range of the embedded parser doesn't have to match that of | 86 | that the range of the embedded parser doesn't have to match that of |
| 86 | the parent node. */ | 87 | the parent node. */ |