GNU bug report logs - #60496
29.0.60; c-ts-mode: Broken indentation with linux style conditionals

Previous Next

Package: emacs;

Reported by: Mohammed Sadiq <sadiq <at> sadiqpk.org>

Date: Mon, 2 Jan 2023 15:58:01 UTC

Severity: normal

Merged with 59686, 60398

Found in versions 29.0.60, 30.0.50

Full log


Message #14 received at 60496 <at> debbugs.gnu.org (full text, mbox):

From: Theodor Thornhill <theo <at> thornhill.no>
To: Mohammed Sadiq <sadiq <at> sadiqpk.org>
Cc: Yuan Fu <casouri <at> gmail.com>, 60496 <at> debbugs.gnu.org
Subject: Re: bug#60496: 29.0.60; c-ts-mode: Broken indentation with linux
 style conditionals
Date: Sat, 07 Jan 2023 13:18:38 +0100
Mohammed Sadiq <sadiq <at> sadiqpk.org> writes:

> On 2023-01-03 03:59, Yuan Fu wrote:
>> Mohammed Sadiq <sadiq <at> sadiqpk.org> writes:
>> 
>>> Indentation of the following C code with linux style is broken:
>>> int main (void)
>>> {
>>>   if (a) {
>>>     func_a ();
>>>   } else if (b) {
>>> 	   func_b ();
>>> 	 } else {
>>> 	   func_c ();
>>> 	 }
>>> }
>>> 
>> Thanks. I’m not familiar with the linux style, is the problem with the
>> closing brackets? Could you give me a correct example so I know exactly
>> what’s wrong?
>> Yuan
>
>
> The expected indentation style for the given code (this one is provided
> by c-mode), with the default indentation values:
>
> int
> main (void)
> {
>   if (a) {
>     func_a ();
>   } else if (b) {
>     func_b ();
>   } else {
>     func_c ();
>   }
> }

Yuan, this is a regression after the
c-ts-mode--bracket-children-anchor. IIRC this was one of the cases that
gave me headeaches.  IIUC now the grand-parent cannot help because we
could have infinite else ifs, right?  And the c grammar nests these
"alternative:" nodes deeper and deeper.

Reverting to

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index dec866f762f..6c8c671550a 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -118,7 +118,7 @@ c-ts-mode--indent-styles
          `(((parent-is "translation_unit") parent-bol 0)
            ((node-is ")") parent 1)
            ((node-is "]") parent-bol 0)
-           ((node-is "}") c-ts-mode--bracket-children-anchor 0)
+           ((node-is "}") parent-bol 0)
            ((node-is "else") parent-bol 0)
            ((node-is "case") parent-bol 0)
            ((node-is "preproc_arg") no-indent)
@@ -134,7 +134,7 @@ c-ts-mode--indent-styles
            ((match "preproc_function_def" "compound_statement") point-min 0)
            ((match "preproc_call" "compound_statement") point-min 0)
            ((parent-is "compound_statement")
-            c-ts-mode--bracket-children-anchor c-ts-mode-indent-offset)
+            parent-bol c-ts-mode-indent-offset)
            ((parent-is "function_definition") parent-bol 0)
            ((parent-is "conditional_expression") first-sibling 0)
            ((parent-is "assignment_expression") parent-bol c-ts-mode-indent-offset)
@@ -155,7 +155,7 @@ c-ts-mode--indent-styles
                '(((node-is "access_specifier") parent-bol 0)))
            ((parent-is "field_declaration_list") parent-bol c-ts-mode-indent-offset)
            ((parent-is "initializer_list") parent-bol c-ts-mode-indent-offset)
-           ((parent-is "if_statement") parent-bol c-ts-mode-indent-offset)
+           ((parent-is "if_statement") c-ts-mode--bracket-children-anchor c-ts-mode-indent-offset)
            ((parent-is "for_statement") parent-bol c-ts-mode-indent-offset)
            ((parent-is "while_statement") parent-bol c-ts-mode-indent-offset)
            ((parent-is "switch_statement") parent-bol c-ts-mode-indent-offset)

Fixes this issue, but then we again have the problem with code such as:

```
int
main (void)
{
  for(;
    ;) {
      // wrong...
    }
}
```

Theo




This bug report was last modified 2 years and 150 days ago.

Previous Next


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