emacs-devel
[Top][All Lists]
Advanced

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

Re: treesit indentation "blinking"


From: Yuan Fu
Subject: Re: treesit indentation "blinking"
Date: Sat, 1 Apr 2023 18:49:46 -0700


> On Apr 1, 2023, at 12:39 PM, Yuan Fu <casouri@gmail.com> wrote:
> 
> 
> 
>> On Mar 30, 2023, at 7:58 AM, Eli Zaretskii <eliz@gnu.org> wrote:
>> 
>>> From: Herman, Géza <geza.herman@gmail.com>
>>> Cc: João Távora <joaotavora@gmail.com>,
>>> dgutov@yandex.ru, dancol@dancol.org,
>>> casouri@gmail.com, theo@thornhill.no, emacs-devel@gnu.org
>>> Date: Thu, 30 Mar 2023 12:26:16 +0200
>>> 
>>> Type this example into a c++-ts-mode buffer (I used "emacs -Q"):
>>> 
>>> --8<---------------cut here---------------start------------->8---
>>> int main() {
>>> for (;;) {
>>> printf("Hello world\n");
>>> }
>>> }
>>> --8<---------------cut here---------------end--------------->8---
>>> 
>>> This is how it will be indented as I wrote it here. c++-ts-mode doesn't
>>> re-indent anything during typing, even though it had the chance to do
>>> that, because electric-indent-mode is enabled by default, and
>>> electric-indent-chars contains the necessary characters.
>>> 
>>> Or, another example. Put the "void foo() { }" part first into a
>>> c++-ts-mode buffer, then write the main function:
>>> 
>>> --8<---------------cut here---------------start------------->8---
>>> int main() {
>>> int a = 0;
>>> for (;;) {
>>> printf("Hello!\n");
>>> }
>>> }
>>> 
>>> void foo() {
>>> }
>>> --8<---------------cut here---------------end--------------->8---
>>> 
>>> Again, c++-ts-mode doesn't indent anything.
>>> 
>>> If you change the example to contain "void foo();" instead of "void
>>> foo() { }", then indenting happens during typing the main function.
>>> 
>>> If you try adding "int a = 0;" into the first example, then it will be
>>> indented at typing the ";". But then for loop is still not get indented,
>>> and the buffer will look like this:
>>> 
>>> --8<---------------cut here---------------start------------->8---
>>> int main() {
>>> int a = 0;
>>> for (;;) {
>>> printf("Hello world\n");
>>> }
>>> }
>>> --8<---------------cut here---------------end--------------->8---
>>> 
>>> If you start by adding an empty comment, and then write the first
>>> example above the empty comment, the buffer will look like this:
>>> 
>>> --8<---------------cut here---------------start------------->8---
>>> int main() {
>>> for (;;) {
>>> printf("Hello world\n");
>>> }
>>> }
>>> 
>>> /**/
>>> --8<---------------cut here---------------end--------------->8---
>> 
>> Yuan and Theo, can you please look into these examples?  A simple
>> "C-c C-q" fixes the indentation, but the question is why it doesn't
>> happen while typing?
> 
> I’ll have a look!

Ok, the reason is that while you were typing, because the closing bracket was 
missing, the parser couldn’t produce a “good” parse tree. OTOH, the C parser 
can reasonably fix the source and produce something easy to use by our 
indenting rules (perhaps due to its simpler grammar) so you don’t see this 
problem in c-ts-mode.

If you enable electric-pair-mode so that the brackets are always balanced, then 
both c-ts-mode and c++-ts-mode should work fine.

Trying to solve this with heuristics is going against to grain, IMO. I think 
it’s acceptable to say that users of ts-modes should enabled 
electric-pair-mode, since it’s based on a parser, after all.

As for the blinking, I’ll let Theo to assess the solutions mentioned earlier 
(removing some indentation rules set on error nodes). 

Yuan


reply via email to

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