unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#67357: [PATCH] Fix c-ts-mode block indent when first-siblings are comments
@ 2023-11-22  1:50 Noah Peart
  2023-11-23  7:35 ` Eli Zaretskii
  0 siblings, 1 reply; 15+ messages in thread
From: Noah Peart @ 2023-11-22  1:50 UTC (permalink / raw)
  To: 67357


[-- Attachment #1.1: Type: text/plain, Size: 1870 bytes --]

Tags: patch

I wasn't sure if these should be two separate bugs, so I separated them
just
in case.

* lisp/progmodes/c-ts-mode.el(c-ts-mode--indent-styles): Fix indentation
in block statements for cases when the first-sibling(s) are comments or
nested keywords (else_clause/do while).

Bug: The first statement in a compound statement that is preceded by a
comment (or multiple comments) isn't indented.

For example, the `if` statement isnt indented in the following code in
`c-ts-mode` using `linux` style.

    int main() {
      while (true) { /* foo */
     if (true) { // this line isnt indented
          puts ("Hello"); // and this isnt either
        }
      }
    }

* lisp/progmodes/c-ts-mode.el(c-ts-mode--indent-styles): Fix indentation
for else_clause and do-while in bracket-less block statements using
`linux` style.

Bug: There is no matching indent rule for bracket-less else_clause
statements and the "while" in a bracket-less do-while statement is
indented to the same level as the do body.

To reproduce, using `linux` style in `c-ts-mode`.

    int main() {
      if (true)
        puts("Hello");
      else
      puts("No matched rule!");
      do
        puts("Hello");
        while (indented_as_part_of_block);
    }


In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
3.24.33, cairo version 1.16.0) of 2023-11-18 built on noah-X580VD
Repository revision: 47b497b4dac91e5ea56102018223bdeb5e21a93b
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101004
System Description: Ubuntu 22.04.3 LTS

Configured using:
 'configure --prefix=/usr/local --with-modules --with-tree-sitter
--with-threads --with-x-toolkit=gtk3 --with-xwidgets --with-gnutls
--with-json --with-mailutils --with-jpeg --with-png --with-rsvg
--with-tiff --with-xml2 --with-xpm --with-imagemagick CC=gcc-12
CXX=gcc-12'

[-- Attachment #1.2: Type: text/html, Size: 2201 bytes --]

[-- Attachment #2: c-ts-mode-first-sib-comments.patch --]
[-- Type: text/x-patch, Size: 2924 bytes --]

From f5c75a515b1fed19d98f41d7a1bc95d285dc4df0 Mon Sep 17 00:00:00 2001
From: nverno <noah.v.peart@gmail.com>
Date: Tue, 21 Nov 2023 16:33:04 -0800
Subject: [PATCH] fix c-ts-mode indentation when first-sibling is a comment

---
 lisp/progmodes/c-ts-mode.el                   |  6 +++-
 .../progmodes/c-ts-mode-resources/indent.erts | 32 ++++++++++++++++++-
 2 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 70717a90caa..a93d8beb272 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -443,7 +443,9 @@ c-ts-mode--indent-styles
 
            ;; Statement in {} blocks.
            ((or (match nil "compound_statement" nil 1 1)
-                (match null "compound_statement"))
+                (match null "compound_statement")
+                ;; Case when first sibling(s) are comments
+                (query "(compound_statement _ (comment) (_) @indent)"))
             standalone-parent c-ts-mode-indent-offset)
            ((parent-is "compound_statement") c-ts-mode--anchor-prev-sibling 0)
            ;; Opening bracket.
@@ -454,8 +456,10 @@ c-ts-mode--indent-styles
            ;; Tested by the "Bracketless Simple Statement" test.
            ((parent-is "if_statement") standalone-parent c-ts-mode-indent-offset)
            ((parent-is "for_statement") standalone-parent c-ts-mode-indent-offset)
+           ((match "while" "do_statement") parent-bol 0) ; (do_statement "while")
            ((parent-is "while_statement") standalone-parent c-ts-mode-indent-offset)
            ((parent-is "do_statement") standalone-parent c-ts-mode-indent-offset)
+           ((parent-is "else_clause") standalone-parent c-ts-mode-indent-offset)
 
            ((parent-is "case_statement") standalone-parent c-ts-mode-indent-offset)
 
diff --git a/test/lisp/progmodes/c-ts-mode-resources/indent.erts b/test/lisp/progmodes/c-ts-mode-resources/indent.erts
index 221b3d809af..d29370b41e6 100644
--- a/test/lisp/progmodes/c-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/c-ts-mode-resources/indent.erts
@@ -310,7 +310,7 @@ label:
 
 Name: Bracket-less Block-Statement (Linux Style) (bug#61026)
 
-=-=-=
+=-=
 int main() {
   while (true)
     if (true) {
@@ -331,6 +331,8 @@ int main() {
     if (true) {
       puts ("Hello");
     }
+    else
+      puts("Hello");
 }
 =-=-=
 
@@ -379,6 +381,34 @@ void foo(
 }
 =-=-=
 
+Name: Block-Statement where first siblings are comments (Linux Style)
+
+=-=
+int main() {
+  while (true) { /* foo */
+    if (true) { // bar
+      puts ("Hello");
+    }
+  }
+  for (;;) {  // 1. fooo
+    /* 2. baaa */
+    /* 3. rrr */
+    if (true)
+      // 2. baaa
+      puts ("Hello");
+  }
+  if (1) { // 1
+    /*
+     * 2
+     */
+    if (1) /*3*/ {
+      /* 4 */
+      puts("Hello");
+    }
+  }
+}
+=-=-=
+
 Name: Initializer List (Linux Style) (Bug#61398)
 
 =-=
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2023-12-11  1:11 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-22  1:50 bug#67357: [PATCH] Fix c-ts-mode block indent when first-siblings are comments Noah Peart
2023-11-23  7:35 ` Eli Zaretskii
2023-11-23 13:17   ` Dmitry Gutov
2023-11-23 14:33     ` Eli Zaretskii
2023-11-23 14:41       ` Dmitry Gutov
2023-11-24 13:47         ` Noah Peart
2023-11-24 14:07           ` Eli Zaretskii
2023-11-24 14:35             ` Noah Peart
2023-11-24 14:45               ` Noah Peart
2023-11-24 14:46               ` Eli Zaretskii
2023-11-29 13:47                 ` Eli Zaretskii
2023-12-09  8:23                   ` Eli Zaretskii
2023-12-10  9:09                     ` Yuan Fu
2023-12-10  9:39                       ` Eli Zaretskii
2023-12-11  1:11                         ` Yuan Fu

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).