Subject: Re: bug#62333: 30.0.50; Issue with tree-sitter syntax tree during certain changes

From: Yuan Fu <[email-masked]>
Date: Fri, 24 Mar 2023 00:34:58 -0700

> On Mar 23, 2023, at 11:05 PM, Eli Zaretskii <[email-masked]> wrote:
> 
>> From: Yuan Fu <[email-masked]>
>> Date: Thu, 23 Mar 2023 16:59:28 -0700
>> Cc: Eli Zaretskii <[email-masked]>,
>> Wilhelm Kirschbaum <[email-masked]>,
>> [email-masked]
>> 
>>> On Mar 23, 2023, at 3:06 PM, Dmitry Gutov <[email-masked]> wrote:
>>> 
>>> On 23/03/2023 23:18, Yuan Fu wrote:
>>>> I guess the question now is why redisplay is triggered in blink-matching-open
>>> 
>>> blink-matching-open calls sit-for after adjusting overlays.
>>> 
>>> sit-for starts with a redisplay.
>> 
>> But it’s not called when narrow-to-region is in effect.
> 
> Exactly.  So the question is now: why does treesit.c see ZV changed,
> if by the time it is supposed to be called from redisplay the
> restriction is restored?
> 
> Is the problematic code in treesit.c really called from redisplay
> triggered by sit-for in blink-matching-open?  Can you show the C and
> Lisp backtrace from the call that "sees that BUF_ZV_BYTE is smaller
> than its visible_end"?

Ah, I made a mistake, it isn’t that it doesn’t reproduce on Linux, but rather it doesn’t reproduce on emacs-29. Since I’m able to reproduce it on Linux, I set a breakpoint and the backtrace explains it: blink-match-open calls forward-sexp, which calls elixir-ts—forward-sexp, which accesses the parse tree, which causes tree-sitter to reparse in the narrowed context.

But normally tree-sitter will reparse again when redisplay fontifies the buffer, because now the visible portion is back to the whole buffer. Tree-sitter didn’t do that because I made a mistake when modifying treesit_ensure_parsed. I moved the code that checks for the need_reparse flag before treesit_sync_visible_region, which sets the need_reparse flag if it detects that narrow situation has changed.

Well, this is a bit embarrassing, there is even a comment warning this mistake, but anyway, I pushed a fix, once it gets merged into master, the problem should go away. 

As things stands right now, every time blink-match-open blinks the matching parenthesis, tree-sitter would reparse the buffer twice, that’s not exactly ideal. Blink-match-open’s use of narrowing is legit, and we can’t just automatically widen in tree-sitter functions.

Yuan