Fix the range handling in treesit.c
authorYuan Fu <casouri@gmail.com>
Mon, 9 Sep 2024 00:50:52 +0000 (17:50 -0700)
committerYuan Fu <casouri@gmail.com>
Mon, 9 Sep 2024 03:52:20 +0000 (20:52 -0700)
commit3435464452b4947098b8ccbe93e5c0afdd2ed06e
tree63bb296af999b1cdee76d2ba60f2b383464b7f3e
parent3fcec09f754af9822339eff3ea15d43eb7d19014
Fix the range handling in treesit.c

1. In treesit_sync_visible_region, reduce the ranges for a parser so it
doesn't go beyond the visible range.

2. To avoid possible infinite recursion, add a within_reparse field to
parsers.  Previously we were using the need_reparse field to avoid
infinite recursion, but lisp programs in a parser's after change hook
might make some buffer edit which turns need_reparse to true. To avoid
that, we now use an explicit field.  If a parser's after change function
makes a buffer edit, lisp program ends up with a desynced parse tree,
but that's better than possible infinite recursion.  Also after change
function shouldn't edit the buffer.

3. In treesit_make_ranges, use parser's visible_beg instead of buffer's
BEGV.  I mean technically whenever we make ranges, buffer's BEGV should
be equal to parser's visible_beg, but better not take that uncertainty,
also makes the code more readable.

4. In Ftreesit_parser_included_ranges, move visible region sync code
before the body of the function.

* src/treesit.c (treesit_sync_visible_region): Minimally fix ranges so
it doesn't exceed parser's visible range.
(treesit_call_after_change_functions): Update calling sigature to
treesit_make_ranges.
(treesit_ensure_parsed, make_treesit_parser): Use the new field
within_reparse.
(treesit_make_ranges): Use parser's visible_beg instead of buffer's
BEGV.
(Ftreesit_parser_included_ranges): Move visible region check before
function body.
* src/treesit.h (Lisp_TS_Parser): Add new field within_reparse.
src/treesit.c
src/treesit.h