aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEli Zaretskii2025-02-28 09:12:05 +0200
committerEli Zaretskii2025-02-28 09:12:05 +0200
commit6ed119d3052ffebd20450ec0c7fb3abf863b3a49 (patch)
tree01b6fdb5cdfeecebda3004861bb27d76a2f57948
parentbecdfb11a5b13bc773729236763e99e513806451 (diff)
downloademacs-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.texi5
-rw-r--r--lisp/treesit.el38
-rw-r--r--src/treesit.c17
-rw-r--r--src/treesit.h15
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}
1247and @var{end} is captured; it doesn't have to be completely contained 1247and @var{end} is captured; it doesn't have to be completely contained
1248in the region. 1248in the region.
1249 1249
1250If @var{grouped} is non-@code{nil}, instead of returning a list of 1250If @var{grouped} is non-@code{nil}, this function returns a grouped list
1251@w{@code{(@var{capture_name} . @var{node})}}, this function returns a 1251of lists of captured nodes. The grouping is determined by @var{query}.
1252list of list of it. The grouping is determined by @var{query}.
1253Captures in the same match of a pattern in @var{query} are grouped 1252Captures in the same match of a pattern in @var{query} are grouped
1254together. 1253together.
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
559being returned. Capture names generally don't matter, but names 559being returned. Capture names generally don't matter, but names
560that starts with an underscore are ignored. 560that starts with an underscore are ignored.
561 561
562RANGE-FN, if non-nil, is a function that takes a node and OFFSET, and 562RANGE-FN, if non-nil, is a function that takes a NODE and OFFSET, and
563returns the ranges to use for that node." 563returns 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
585captured by the `@language' capture name are converted to language 585captured by the `@language' capture name are converted to language
586symbols with LANGUAGE-FN. 586symbols with LANGUAGE-FN.
587 587
588RANGE-FN, if non-nil, is a function that takes a node and OFFSET, and 588RANGE-FN, if non-nil, is a function that takes a NODE and OFFSET, and
589returns the ranges to use for that node. 589returns the ranges to use for that NODE.
590 590
591BEG, END, OFFSET are the same as in `treesit-query-range'." 591BEG, 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
644should be a cons of numbers (START-OFFSET . END-OFFSET), where the start 644should be a cons of numbers (START-OFFSET . END-OFFSET), where the start
645and end offset are added to each queried range to get the result ranges. 645and end offset are added to each queried range to get the result ranges.
646 646
647If RANGE-FN is non-nil, it should be a function, Emacs uses this 647If RANGE-FN is non-nil, it should be a function; Emacs uses this
648function to compute the ranges to use for the embedded parser. The 648function to compute the ranges to use for the embedded parser. The
649function is passed the captured node and OFFSET, and should return a 649function is passed the captured node and OFFSET, and should return a
650list of ranges, where each range is a cons of the start and end 650list of ranges, where each range is a cons of the start and end
651position. 651position.
652 652
653Capture names generally don't matter, but names that starts with 653Capture names generally don't matter, but names that start with
654an underscore are ignored. 654an underscore are ignored.
655 655
656QUERY can also be a function, in which case it is called with 2 656QUERY can also be a function, in which case it is called with 2
@@ -672,8 +672,8 @@ like this:
672Each QUERY is a tree-sitter query in either the string, 672Each QUERY is a tree-sitter query in either the string,
673s-expression or compiled form. 673s-expression or compiled form.
674 674
675Capture names generally don't matter, but names that starts with an 675Capture names generally don't matter, but names that start with an
676underscore are ignored. And `@language' is reserved. 676underscore are ignored. The `@language' capture name is reserved.
677 677
678For each QUERY, :KEYWORD and VALUE pairs add meta information to 678For each QUERY, :KEYWORD and VALUE pairs add meta information to
679it. For example, 679it. For example,
@@ -690,8 +690,8 @@ this way: Emacs queries QUERY in the host language's parser,
690computes the ranges spanned by the captured nodes, and applies 690computes the ranges spanned by the captured nodes, and applies
691these ranges to parsers for the embedded language. 691these ranges to parsers for the embedded language.
692 692
693If the embed language is dynamic, then a function in place of the embed 693If the embed language is dynamic, then `:embed' can specify a
694language symbol. This function will by passed a node and should return 694function. This function will by passed a node and should return
695the language symbol for the embedded code block. The node is the one 695the language symbol for the embedded code block. The node is the one
696captured from QUERY with capture name `@language'. Also make sure the 696captured from QUERY with capture name `@language'. Also make sure the
697code block and language capture are in the same match group. 697code 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
901If INCLUDE-NULL is non-nil, also include parsers that has a nil embed 901If INCLUDE-NULL is non-nil, also include parsers whose embed level
902level." 902is 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
918those ranges. HOST-PARSER and QUERY must match. 918those ranges. HOST-PARSER and QUERY must match.
919 919
920EMBED-LANG is either a language symbol or a function that takes a node 920EMBED-LANG is either a language symbol or a function that takes a node
921and return a language symbol. 921and returns a language symbol.
922 922
923EMBED-LEVEL is the embed level for the local parsers being created or 923EMBED-LEVEL is the embed level for the local parsers being created or
924updated. When looking for existing local parsers, only look for parsers 924updated. When looking for existing local parsers, only look for parsers
@@ -928,7 +928,7 @@ level.
928RANGE-FN, if non-nil, is a function that takes a node and OFFSET, and 928RANGE-FN, if non-nil, is a function that takes a node and OFFSET, and
929returns the ranges to use for that node. 929returns the ranges to use for that node.
930 930
931Return updated parsers in a list." 931Return 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.
998RANGE-FN, if non-nil, is a function that takes a node and OFFSET, and 998RANGE-FN, if non-nil, is a function that takes a node and OFFSET, and
999returns the ranges to use for that node. 999returns the ranges to use for that node.
1000 1000
1001Return the created local parsers in a list." 1001Return 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
1052Go over each settings in SETTINGS, try to create or update the embedded 1052Go over each settings in SETTINGS, try to create or update the embedded
1053language in that setting. Return the created or updated embedded 1053language in that setting. Return the list of the created or updated
1054language parsers in a list. 1054embedded language parsers.
1055 1055
1056EMBED-LEVEL is the embed level for the embedded parser being created or 1056EMBED-LEVEL is the embed level for the embedded parser being created or
1057updated. When looking for existing embedded parsers, only look for 1057updated. 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
1828The embed level can be either nil or a non-negative integer. A value of 1828The embed level can be either nil or a non-negative integer. A value of
1829nil means the parser isn't part of the embedded parser tree. The 1829nil means the parser isn't part of the embedded parser tree. The
1830primary parser has embed level 0, from it, each layer of embedded parser 1830primary parser has embed level 0, and each additional layer of parser
1831has +1 embed level. */) 1831embedding 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. */)
1839DEFUN ("treesit-parser-set-embed-level", 1839DEFUN ("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.
1843LEVEL can be nil, for a parser that is not part of an embedded parser
1844tree; 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
1862Only embeded local parser can have parent node. When Emacs uses a node 1864Only embeded local parsers can have parent node. When Emacs uses a node
1863in the host parser to create this local parser, that node is considered 1865in the host parser to create this local parser, that node is considered
1864the parent node of the local parser. */) 1866the 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. */)
1871DEFUN ("treesit-parser-set-parent-node", 1873DEFUN ("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
3289with the region between BEG and END are captured, it doesn't have to 3291with the region between BEG and END are captured, it doesn't have to
3290be completely in the region. 3292be completely in the region.
3291 3293
3292If GROUPED is non-nil, group captures into matches and return a list of 3294If GROUPED is non-nil, ther function groups the returned list of
3293MATCH, where each MATH is a list of (CAPTURE_NAME . NODE). 3295captures into matches and return a list of MATCH, where each MATCH is
3296a list of the form (CAPTURE_NAME . NODE).
3294 3297
3295If NODE-ONLY is non-nil, return nodes only, and don't include 3298If NODE-ONLY is non-nil, return nodes only, and don't include
3296CAPTURE_NAME. 3299CAPTURE_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. */