Skip to content

Commit 814b457

Browse files
committed
Add treesit-query-first-valid
This should help major modes to support grammar versions. * doc/lispref/parsing.texi (Pattern Matching): Mention new function. * lisp/treesit.el (treesit-query-first-valid): New function. (treesit-query-valid-p): New function.
1 parent d3a2ec5 commit 814b457

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

doc/lispref/parsing.texi

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,22 +1539,35 @@ the specific error. You can use @code{treesit-query-validate} to
15391539
validate and debug the query.
15401540
@end defun
15411541

1542-
@defun treesit-query-language query
1543-
This function returns the language of @var{query}.
1544-
@end defun
1542+
@findex treesit-query-language
1543+
@findex treesit-query-expand
1544+
@findex treesit-pattern-expand
1545+
@findex treesit-query-valid-p
1546+
There are some additional functions for queries:
1547+
@code{treesit-query-language} returns the language of a query;
1548+
@code{treesit-query-valid-p} checks whether a query is valid;
1549+
@code{treesit-query-expand} converts a s-expression query into the
1550+
string format; and @code{treesit-pattern-expand} converts a pattern.
1551+
1552+
@findex treesit-query-first-valid
1553+
Tree-sitter grammars change overtime. To support multiple possible
1554+
versions of a grammar, a Lisp program can use
1555+
@code{treesit-query-first-valid} to pick the right query to use. For
1556+
example, if a grammar has a @code{(defun)} node in one version, and
1557+
later renamed it to @code{(function_definition)}, a Lisp program can use
15451558

1546-
@defun treesit-query-expand query
1547-
This function converts the s-expression @var{query} into the string
1548-
format.
1549-
@end defun
1559+
@example
1560+
@group
1561+
(treesit-query-first-valid 'lang
1562+
'((defun) @defun)
1563+
'((function_definition) @defun))
1564+
@end group
1565+
@end example
15501566

1551-
@defun treesit-pattern-expand pattern
1552-
This function converts the s-expression @var{pattern} into the string
1553-
format.
1554-
@end defun
1567+
to support both versions of the grammar.
15551568

1556-
For more details, read the tree-sitter project's documentation about
1557-
pattern-matching, which can be found at
1569+
For more details, consider reading the tree-sitter project's
1570+
documentation about pattern-matching. The documentation can be found at
15581571
@uref{https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries}.
15591572

15601573
@node User-defined Things

lisp/treesit.el

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,23 @@ that starts with an underscore are ignored."
505505
collect (cons (+ (treesit-node-start node) offset-left)
506506
(+ (treesit-node-end node) offset-right)))))
507507

508+
(defun treesit-query-valid-p (language query)
509+
"Return non-nil if QUERY is valid in LANGUAGE, nil otherwise."
510+
(ignore-errors
511+
(treesit-query-compile language query t)
512+
t))
513+
514+
(defun treesit-query-first-valid (language &rest queries)
515+
"Return the first query in QUERIES that is valid in LANGUAGE.
516+
If none are valid, return nil."
517+
(declare (indent 1))
518+
(let (query)
519+
(catch 'valid
520+
(while (setq query (pop queries))
521+
(ignore-errors
522+
(treesit-query-compile language query t)
523+
(throw 'valid query))))))
524+
508525
;;; Range API supplement
509526

510527
(defvar-local treesit-range-settings nil
@@ -4636,6 +4653,8 @@ If anything goes wrong, this function signals an `treesit-error'."
46364653
(treesit-query-language
46374654
:no-eval (treesit-query-language compiled-query)
46384655
:eg-result c)
4656+
(treesit-query-valid-p)
4657+
(treesit-query-first-valid)
46394658
(treesit-query-expand
46404659
:eval (treesit-query-expand '((identifier) @id "return" @ret)))
46414660
(treesit-pattern-expand

0 commit comments

Comments
 (0)