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/16/26 06:32, Eli Zaretskii wrote: >> Sure, attached a new patch wit the tests fixed. > > Hmm... looks exactly like the previous patch to me. Are you sure you > sent the right patch? Oops! This one is right. -- mvh Björn Lindqvist
0001-c-ts-mode-Improved-block-comment-indentation.patch
(text/x-patch, 3.8 KB)
From 95c85e3deb1a63eacc0ba47b7e92498823b5ad2a 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 +++++++++++------ .../progmodes/c-ts-mode-resources/indent.erts | 30 +++++++++++++------ 2 files changed, 40 insertions(+), 18 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) diff --git a/test/lisp/progmodes/c-ts-mode-resources/indent.erts b/test/lisp/progmodes/c-ts-mode-resources/indent.erts index 3d875e3113e..fc956b770e8 100644 --- a/test/lisp/progmodes/c-ts-mode-resources/indent.erts +++ b/test/lisp/progmodes/c-ts-mode-resources/indent.erts @@ -342,11 +342,11 @@ Name: Multiline Block Comments 1 (bug#60270) */ =-=-= -Name: Multiline Block Comments 2 (bug#60270) +Name: Multiline Block Comments 2 (bug#60270, bug#81533) =-= /* - some comment + some comment */ =-=-= @@ -365,7 +365,7 @@ Name: Multiline Block Comments 4 (bug#60270) */ =-=-= -Name: Multiline Block Comments 5 (bug#60270) +Name: Multiline Block Comments 5 (bug#60270, bug#81533) =-= /* @@ -374,12 +374,24 @@ line 2 */ =-= /* - line one - line 2 + line one + line 2 */ =-=-= -Name: Block Comment prefixes (Bug#61314) +Name: Dangler (bug#81533) + +=-= +int put; /* line one + +line 2 */ +=-= +int put; /* line one + + line 2 */ +=-=-= + +Name: Block Comment prefixes (Bug#61314, bug#81533) =-=-= /* @@ -389,9 +401,9 @@ Name: Block Comment prefixes (Bug#61314) */ =-=-= /* - - item1 - - item2 - - item3 + - item1 + - item2 + - item3 */ =-=-= -- 2.55.0