emacs-devel
[Top][All Lists]
Advanced

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

Re: treesit indentation "blinking"


From: Alan Mackenzie
Subject: Re: treesit indentation "blinking"
Date: Sun, 2 Apr 2023 14:26:04 +0000

Hello, Yuan.

On Sat, Apr 01, 2023 at 18:49:46 -0700, Yuan Fu wrote:


> > 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.

That is not a solution.  Some people dislike electric-pair-mode, and they
would be faced with the unwelcome choice of having to put up with e-p-m
or having source code not indenting (or jumping all over the place) on
the text line being input.

> Trying to solve this with heuristics is going against to grain, IMO.

Does the parser not produce adequate information for source code which
isn't syntactically valid (which is most of the time when editing is in
progress)?

> I think it’s acceptable to say that users of ts-modes should enable
> electric-pair-mode, since it’s based on a parser, after all.

electric-pair-mode is a user option.  We shouldn't be mandating such
things to users, they should be individual choices.  I've had to use a
proprietary editor where e-p-m couldn't be disabled (or at least I didn't
know how to), and I hated it.  Emacs should be better than such editors.

That electric-pair-mode isn't needed to get reasonable electric
indentation is demonstrated by CC Mode, where electric indentation occurs
when needed, but rarely happens more than once per source line.

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

> Yuan

-- 
Alan Mackenzie (Nuremberg, Germany).



reply via email to

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