master 7ef7433eb51 1/2: c-ts-mode: Improve block comment indentation

Eli Zaretskii <[email protected]>
Newsgroups gmane.emacs.diffs
Message-ID <[email protected]>
branch: master
commit 7ef7433eb51f25be43d84372cec99c6cd0240548
Author: Björn Lindqvist <[email protected]>
Commit: Eli Zaretskii <[email protected]>

    c-ts-mode: Improve 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): Improve
    block comment indentation.  (Bug#81533)
    
    * test/lisp/progmodes/c-ts-mode-resources/indent.erts: Adjust test
    data to new behavior.
---
 lisp/progmodes/c-ts-mode.el                        | 28 +++++++++++++-------
 .../lisp/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 @@ PARENT is the same as other simple-indent 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 @@ MODE can be `c' or `cpp'.  STYLE can be `gnu', `k&r', `linux', `bsd'."
            ;; ((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
  */
 =-=-=
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.