bug#81533: c-ts-mode: Improved block comment indentation

Björn A. Lindqvist <[email protected]>
Newsgroups gmane.emacs.bugs
Message-ID <[email protected]>
On 8/15/26 15:23, Eli Zaretskii wrote:
> I was about to install this, but this patch breaks the c-ts-mode
> tests:
> 
>    Test c-ts-mode-test-indentation backtrace:
>      signal(ert-test-failed (("Mismatch in test \"Multiline Block Comment
>      ert-fail(("Mismatch in test \"Multiline Block Comments 2 (bug#60270)
>      ert-test--erts-test(((code lambda nil (c-ts-mode) (setq-local indent
>      ert-test-erts-file("d:/gnu/git/emacs/trunk/test/lisp/progmodes/c-ts-
>      #f(lambda () [t] (let* ((fn-0 #'treesit-ready-p) (args-1 (condition-
>      #f(compiled-function () #<bytecode 0x1ea918ef364892c1>)()
>      handler-bind-1(#f(compiled-function () #<bytecode 0x1ea918ef364892c1
>      ert--run-test-internal(#s(ert--test-execution-info :test #s(ert-test
>      ert-run-test(#s(ert-test :name c-ts-mode-test-indentation :documenta
>      ert-run-or-rerun-test(#s(ert--stats :selector ... :tests ... :test-m
>      ert-run-tests((not (or (tag :unstable) (tag :nativecomp))) #f(compil
>      ert-run-tests-batch((not (or (tag :unstable) (tag :nativecomp))))
>      ert-run-tests-batch-and-exit((not (or (tag :unstable) (tag :nativeco
>      eval((ert-run-tests-batch-and-exit '(not (or (tag :unstable) (tag :n
>      command-line-1(("-L" ";." "-l" "ert" "--eval" "(setq treesit-extra-l
>      command-line()
>      normal-top-level()
>    Test c-ts-mode-test-indentation condition:
>        (ert-test-failed
>         ("Mismatch in test \"Multiline Block Comments 2 (bug#60270)\", file d:/gnu/git
>    /emacs/trunk/test/lisp/progmodes/c-ts-mode-resources/indent.erts"
> 	"/*\n   some comment\n */\n" "/*\n  some comment\n */\n"))
>       FAILED  2/4  c-ts-mode-test-indentation (0.207103 sec) at lisp/progmodes/c-ts-mode-tests.el:26
> 
> Could you please amend the code or the test to avoid the breakage, and
> then resubmit?

Sure, attached a new patch wit the tests fixed.

> Also, these changes are close to the limit of what we are able to
> accept from you without the copyright-assignment agreement.  If you
> agree to start the assignment paperwork at this time, I will send you
> the form to fill and the instructions to go with it.

Sure, go ahead!


-- 
mvh Björn Lindqvist
0001-c-ts-mode-Improved-block-comment-indentation.patch (text/x-patch, 2.6 KB)
From 5a9dd813e23f0f81a82d4e5afd33c1f23727d62f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Lindqvist?= <[email protected]>
Date: Sun, 2 Aug 2026 02:18:22 +0200
Subject: [PATCH] c-ts-mode: Improved block comment indentation

1) lines beginning with "*" are aligned to the first "*"
2) lines following the first or blank lines are indented three spaces
3) other lines are aligned to the previous line

* lisp/progmodes/c-ts-mode.el (c-ts-mode--simple-indent-rules): Improved
  block comment indentation.
---
 lisp/progmodes/c-ts-mode.el | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 5f064716a89..7c281c1e508 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -473,6 +473,24 @@ c-ts-mode--emacs-macro-rules
     (cons (treesit-node-start parent)
           c-ts-indent-offset))))
 
+(defun c-ts-mode--block-comment-offset (_n parent bol &rest _)
+  "Indentation offset for lines in block comments.
+
+One space if line starts with \"*\", three if the previous line is blank
+or the first line of the comment, and otherwise same as previous line."
+  (save-excursion
+    (beginning-of-line)
+    (let* ((c-point (treesit-node-start parent))
+	   (starred? (looking-at-p (rx (* blank) "*")))
+	   (first-or-second? (<= (line-beginning-position 0) c-point)))
+      (forward-line -1)
+      (let ((prev-indent (current-indentation)))
+        (cond (starred? 1)
+              ((or first-or-second? (= prev-indent 0)) 3)
+              (t (- prev-indent (progn (goto-char c-point)
+                                       (current-column)))))))))
+
+
 (defun c-ts-mode--simple-indent-rules (mode style)
   "Return the indent rules for MODE and STYLE.
 
@@ -513,15 +531,7 @@ c-ts-mode--simple-indent-rules
            ;; ((match nil "function_declarator" "parameters") parent 0)
            ;; ((parent-is "template_declaration") parent 0)
 
-           ;; `c-ts-common-looking-at-star' has to come before
-           ;; `c-ts-common-comment-2nd-line-matcher'.
-           ;; FIXME: consolidate into a single rule.
-           ((and (parent-is "comment") c-ts-common-looking-at-star)
-            c-ts-common-comment-start-after-first-star -1)
-           (c-ts-common-comment-2nd-line-matcher
-            c-ts-common-comment-2nd-line-anchor
-            1)
-           ((parent-is "comment") prev-adaptive-prefix 0)
+           ((parent-is "comment") parent c-ts-mode--block-comment-offset)
 
            ;; Preproc directives
            ((node-is "preproc_arg") no-indent)
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.