Subject: Re: bug#62412: 29.0.60; strange c++ indentation behavior with tree sitter

From: João Távora <[email-masked]>
Date: Sat, 25 Mar 2023 11:43:45 +0000

On Sat, Mar 25, 2023 at 10:26 AM Herman, Géza <[email-masked]> wrote:
>
> That's the difference. c++-ts-mode appends "{}():;,#" to
> electric-indent-chars, while c++-mode doesn't do this.
>
> Nevertheless, I think that the calculated intendation should be a
> correct number. Even if the user turns off electric-indent-mode, but
> dedices to call re-indenting manually for a half-written for loop, emacs
> should re-intent the line properly. At least, this specific example
> works OK with c++-mode.
>
> (note that I understand that this problem is not trivial, tree
> sitter/emacs may get confused if the buffer cannot be properly parsed)

There can be no "correct" indentation in a buffer with an invalid state.

But there are heuristics.  Here, it can be argued that c++-mode's
heuristics are better.

Let's assume you turn off electric-indent-mode. In c++-mode, pressing RET
after:

   int main() {

"correctly" indents the next line.  In c++-ts-mode, it doesn't.

Both programs are ill-formed but you're right that after correcting
that, by say adding 'return 0; RET }', the c++-mode version of the
same program is closer to being correctly indented.

But this heuristic is not always great, so it's a stick with two ends.

Now let's take another invalid program:
   int foo()
   class bar { | <- cursor here
   }

In c++-mode typing TAB indents the class line to the second column,
which is arguably worse than c++-ts-mode, which doesn't do anything.
That's because you may well want to work on that class and then only
remember that you need the ';' for the declaration of 'foo'.

IMO, it's a question of getting used to it in the end.  And using
electric-pair-mode helps a lot, as some have pointed out.

João