GNU bug report logs - #62412
29.0.60; strange c++ indentation behavior with tree sitter

Previous Next

Package: emacs;

Reported by: "Herman, Geza" <geza.herman <at> gmail.com>

Date: Thu, 23 Mar 2023 20:44:02 UTC

Severity: normal

Found in version 29.0.60

Done: Stefan Kangas <stefankangas <at> gmail.com>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: João Távora <joaotavora <at> gmail.com>
To: Herman, Géza <geza.herman <at> gmail.com>
Cc: Yuan Fu <casouri <at> gmail.com>, Theodor Thornhill <theo <at> thornhill.no>, 62412 <at> debbugs.gnu.org
Subject: bug#62412: 29.0.60; strange c++ indentation behavior with tree sitter
Date: Sat, 25 Mar 2023 11:43:45 +0000
On Sat, Mar 25, 2023 at 10:26 AM Herman, Géza <geza.herman <at> gmail.com> 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




This bug report was last modified 1 year and 313 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.