Subject: 30.0.50; Missing indent rules in c-ts-mode

From: Augustin Chéneau (BTuin) <[email-masked]>
Date: Tue, 26 Sep 2023 14:29:07 +0200

I found some more issues.

First, there is no rule for an element in an else clause:

if (true)
   do_something();
else
do_something_else();

"do_something_else" is not indented.

There is also something weird with the bsd indent style.  It indents 
do_something and do_something_else to parent-bol with 0 offset, so the 
result is:

if (true)
do_something();
else
do_something_else();

It is very different from the bsd style from c-mode, which produces this:

if (true)
	do_something();
else
	do_something_else();

By looking at random files from OpenBSD, they do indent their 
expressions inside if and else:

<https://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/net/bsd-comp.c?rev=1.17&content-type=text/plain>
(lines 277, 484, and 527)

Also there is a second patch which tries to handle specific types of 
comments.  I don't know how popular it is, but some people use these 
kind of comments:

/*---------.
| %param.  |
`---------*/

Here we want the vertical bar ("|") to be indented at the same column as 
the first "/".

Currently the result is this:

/*---------.
   | %param.  |
   `---------*/

This patch is a just proof of concept.  If this new behavior is indeed 
wanted, I would clean it up and adapt other modes.

From d050619743b2604dfa3b1342fa12ee99a4aa550a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Augustin=20Ch=C3=A9neau?= <[email-masked]>
Date: Tue, 26 Sep 2023 14:03:18 +0200
Subject: [PATCH 1/2] c-ts-mode: Add indentation rule for else_clause

---
 lisp/progmodes/c-ts-mode.el | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 314f37f7ec5..cb236447368 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -455,6 +455,7 @@ c-ts-mode--indent-styles
            ;; These rules are for cases where the body is bracketless.
            ;; Tested by the "Bracketless Simple Statement" test.
            ((parent-is "if_statement") standalone-parent c-ts-mode-indent-offset)
+           ((parent-is "else_clause") standalone-parent c-ts-mode-indent-offset)
            ((parent-is "for_statement") standalone-parent c-ts-mode-indent-offset)
            ((parent-is "while_statement") standalone-parent c-ts-mode-indent-offset)
            ((parent-is "do_statement") standalone-parent c-ts-mode-indent-offset)
-- 
2.42.0

From 0074db4224b4269eec464ac00eb751d919aec408 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Augustin=20Ch=C3=A9neau?= <[email-masked]>
Date: Tue, 26 Sep 2023 14:10:40 +0200
Subject: [PATCH 2/2] c-ts-mode: handle specific comment formatting

---
 lisp/progmodes/c-ts-common.el | 11 +++++++++++
 lisp/progmodes/c-ts-mode.el   |  4 +++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/c-ts-common.el b/lisp/progmodes/c-ts-common.el
index 3b0814970ad..1ea41260ab8 100644
--- a/lisp/progmodes/c-ts-common.el
+++ b/lisp/progmodes/c-ts-common.el
@@ -56,6 +56,17 @@
 
 ;;; Comment indentation and filling
 
+
+(defun c-ts-common-looking-at (char &rest chars)
+  (lambda (_n _p bol &rest _)
+    (let ((res nil))
+      (setq res (eq (char-after bol) char))
+      (while (and chars (not res))
+        (setq char (car chars)
+              chars (cdr chars)
+              res (eq (char-after bol) char)))
+      res)))
+
 (defun c-ts-common-looking-at-star (_n _p bol &rest _)
   "A tree-sitter simple indent matcher.
 Matches if there is a \"*\" after BOL."
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index cb236447368..76cbad50d06 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -371,8 +371,10 @@ c-ts-mode--indent-styles
            ((node-is "preproc_arg") no-indent)
            ;; `c-ts-common-looking-at-star' has to come before
            ;; `c-ts-common-comment-2nd-line-matcher'.
-           ((and (parent-is "comment") c-ts-common-looking-at-star)
+           ((and (parent-is "comment") c-ts-common-looking-at ?*)
             c-ts-common-comment-start-after-first-star -1)
+           ((and (parent-is "comment") (c-ts-common-looking-at ?| ?\\ ?`))
+            c-ts-common-comment-start-after-first-star -2)
            (c-ts-common-comment-2nd-line-matcher
             c-ts-common-comment-2nd-line-anchor
             1)
-- 
2.42.0