From f5c75a515b1fed19d98f41d7a1bc95d285dc4df0 Mon Sep 17 00:00:00 2001 From: nverno 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