]> git.eshelyaron.com Git - emacs.git/commit
Rework range facility in treesit.el to support arbitrary nesting
authorYuan Fu <casouri@gmail.com>
Thu, 27 Feb 2025 11:10:47 +0000 (03:10 -0800)
committerEshel Yaron <me@eshelyaron.com>
Fri, 28 Feb 2025 11:20:10 +0000 (12:20 +0100)
commit2890996c01f2c6c46025aba7b68c5e652e88b78d
treedff0243993ff2974a38057c2f2dd2cfa71d5ed31
parent0ba5125be31aac09857d9a0c3c9695ba68f1edc0
Rework range facility in treesit.el to support arbitrary nesting

Make use of the newly added embed-level parser property, we now
iterativeLy create/update embedded parsers at each embed level,
allowing arbitrary levels of nesting.

At the beginning, we start with the primary parser.  We query it
with each range settings, gets some ranges and their
corresponding language. Then we create the first level of
embedded parsers using the ranges and language we got.  This is
one iteration. For the next iteration/level, we start with the
embedded parsers we create/updated in the previous
iteration/level, and query each of them with each of the range
settings, creating/updating the next level of embedded parsers.
And we keep doing this until we don't get more matches from the
queries.

We now also support the :embed keyword in 'treesit-range-rules'
to be a function that returns a language.  This allows major
modes like markdown and org mode to support code blocks of which
the language isn't known ahead of time.

* lisp/treesit.el (treesit-primary-parser): Move to front.
(treesit-query-range-by-language): New function.
(treesit-range-rules): Allow :embed to be a function, update docstring.
(treesit-local-parsers-on): Update docstring.
(treesit--parser-at-level):
(treesit--update-ranges-non-local): New functions.
(treesit--update-ranges-local): Use the new logic, support
:embed being a function.
(treesit--update-range-1): New function, has the meat of
original treesit-update-ranges.
(treesit-update-ranges): Implements the iteration logic using
treesit--update-range-1 to do the actual work.

(cherry picked from commit 1314272bf398e068385572ca16d4dfcd55a48828)
lisp/treesit.el