bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#66732: tree-sitter fontification doesn't update multi-line syntax re


From: Yuan Fu
Subject: bug#66732: tree-sitter fontification doesn't update multi-line syntax reliably
Date: Tue, 12 Dec 2023 19:28:26 -0800
User-agent: Mozilla Thunderbird



On 12/12/23 4:43 AM, Dmitry Gutov wrote:
On 12/12/2023 09:50, Yuan Fu wrote:

I feel that font-lock and syntax-ppss have different problems and requires different solutions. For font-lock, we need to mark affected range unfontified; we just need to make sure we do that after jit-lock-fontify-now. For syntax-ppss, we need to force a reparse before entering syntax-ppss so that syntax text properties are up-to-date when syntax-ppss do its work.

font-lock *could* have a different solution, but I don't see why any solution that works for syntax-ppss properly, shouldn't work just as well for font-lock. And if it's possible - why not do that, saving on variations in code. We also save on having to handle two differently callbacks from the parser.

The proposed alternative is not without problems. For example, that unfontifying a region inside a timer, you trigger a second fontification rather than having the job finished in one go. Possibly introducing a extra "blink" as well. That is a minor inconvenience by itself, but ideally we'd avoid it.

I've come around it, right, extending the to-be-fontified region before jit-lock runs is also a solution (and is probably better than fontifying twice).

Here's a draft solution based on *-extend-region-functions, attached.

Alas, while it works fine in python-ts-mode (for both syntax and font-lock), making it behave better than python-mode, in c-ts-mode it doesn't quite have the same effect: when you backspace over the closing "/", the highlighting is properly updated only after you make the next edit (any edit), or select another window. I'm not sure, though, if it's due to my own problems with Emacs's failure to redisplay (reported elsewhere), so more testing is welcome.

But that might also be related to the use of c-ts-mode--emacs-set-ranges: printing a backtrace calls inside treesit--font-lock-notifier shows that the last notification comes also during font-lock but after treesit--font-lock-extend-region, inside c-ts-mode--emacs-set-ranges. I don't quite understand this design where the ranges are applied inside the font-lock code.
c-ts-mode--emacs-set-ranges is registered as a range rule, so many tree-sitter function calls it before doing anything to make sure range is up-to-date. treesit-font-lock-fontify-region calls treesit-update-ranges at the beginning of its body, and treesit-update-ranges calls c-ts-mode--emacs-set-ranges.

That seems to mean that any feature accessing the parse tree should call treesit-update-ranges first. Including syntax-propertize-functions and *-extend-region-functions, which we currently don't do.

But which boundaries is it supposed to use? Should treesit--syntax-extend-region call treesit-update-ranges, when wait for the parser updates, then possibly call treesit-update-ranges again on the extended boundaries, and so on, until the parser stops sending notifications? Will it stop?

It'll stop. If the ranges don't change, no reparse will happen. And only buffer content change can cause range change. Reparse itself can't. So at most you'll see reparse -> update ranges -> reparse.

Anyway, could you try my patch? Like I said, I'm not sure if the insufficient fontification I'm observing in c-ts-mode is due to the problem with the solution, or due to the other redisplay-related problems on my system.

Yeah, it doesn't solve the problem in c-ts-mode regarding block comments.

We might need to run (progn (force-parse) (update-ranges) (force-parse)) before jit-lock-fontify-now and sytax-ppss.

Yuan





reply via email to

[Prev in Thread] Current Thread [Next in Thread]