Subject: [PATCH] js-ts-mode: Fix filling of jsdoc blocks

From: Damien Cassou <[email-masked]>
Date: Mon, 29 Jul 2024 21:36:02 +0200

Tags: patch

Hi,

the attached patches

1. add "description" (the kind of treesit node used for texts by the
jsdoc parser) to `c-ts-common--comment-regexp' in js-ts-mode;

2. make `c-ts-common--fill-block-comment' only remove a mask if it has
added it in the first place.

Best

-- 
Damien Cassou

"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill
>From e444692026cae97dfaecebf5972507ae95f5e488 Mon Sep 17 00:00:00 2001
From: Damien Cassou <[email-masked]>
Date: Sun, 21 Jul 2024 21:32:34 +0200
Subject: [PATCH 1/2] js-ts-mode: Make jsdoc's "description" block a comment

* lisp/progmodes/js.el (js-ts-mode): Add "description" to
`c-ts-common--comment-regexp'.
---
 lisp/progmodes/js.el | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index f8140c14a49..75c8111035c 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -3946,7 +3946,9 @@ js-ts-mode
                    :embed 'jsdoc
                    :host 'javascript
                    :local t
-                   `(((comment) @capture (:match ,js--treesit-jsdoc-beginning-regexp @capture))))))
+                   `(((comment) @capture (:match ,js--treesit-jsdoc-beginning-regexp @capture)))))
+
+      (setq c-ts-common--comment-regexp (rx (or "comment" "line_comment" "block_comment" "description"))))
 
     ;; Imenu
     (setq-local treesit-simple-imenu-settings
-- 
2.45.2

>From cf14e3b25245dac98adcf90723e4a0c601f65528 Mon Sep 17 00:00:00 2001
From: Damien Cassou <[email-masked]>
Date: Sun, 21 Jul 2024 21:33:59 +0200
Subject: [PATCH 2/2] c-ts-common: Don't insert wrong characters during filling

* lisp/progmodes/c-ts-common.el (c-ts-common--fill-block-comment): If
masking hasn't been done, don't unmask.
---
 lisp/progmodes/c-ts-common.el | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/lisp/progmodes/c-ts-common.el b/lisp/progmodes/c-ts-common.el
index 3882a697c48..14dd29e07e2 100644
--- a/lisp/progmodes/c-ts-common.el
+++ b/lisp/progmodes/c-ts-common.el
@@ -151,7 +151,9 @@ c-ts-common--fill-block-comment
          (orig-point (point-marker))
          (start-marker (point-marker))
          (end-marker nil)
-         (end-len 0))
+         (end-len 0)
+         (start-mask-done nil)
+         (end-mask-done nil))
     (move-marker start-marker start)
     ;; We mask "/*" and the space before "*/" like
     ;; `c-fill-paragraph' does.
@@ -162,6 +164,7 @@ c-ts-common--fill-block-comment
                             (group "/") "*"))
         (goto-char (match-beginning 1))
         (move-marker start-marker (point))
+        (setq start-mask-done t)
         (replace-match " " nil nil nil 1))
 
       ;; Include whitespaces before /*.
@@ -179,6 +182,7 @@ c-ts-common--fill-block-comment
         (goto-char (match-beginning 1))
         (setq end-marker (point-marker))
         (setq end-len (- (match-end 1) (match-beginning 1)))
+        (setq end-mask-done t)
         (replace-match (make-string end-len ?x)
                        nil nil nil 1))
 
@@ -206,11 +210,11 @@ c-ts-common--fill-block-comment
         (fill-region (max start-marker para-start) (min end para-end) arg))
 
       ;; Unmask.
-      (when start-marker
+      (when (and start-mask-done start-marker)
         (goto-char start-marker)
         (delete-char 1)
         (insert "/"))
-      (when end-marker
+      (when (and end-mask-done start-marker)
         (goto-char end-marker)
         (delete-region (point) (+ end-len (point)))
         (insert (make-string end-len ?\s)))
-- 
2.45.2