Subject: bug#77095: 31.0.50; Using fill-region doesn't produce commented lines in yaml-ts-mode

From: Randy Taylor <[email-masked]>
Date: Sat, 05 Apr 2025 20:21:05 +0000

On Sunday, March 23rd, 2025 at 01:52, the_wurfkreuz <[email-masked]> wrote:
> 
> 
> I cleared the scratch buffer and pasted this yaml:
> 
> `apiVersion: v1 kind: PersistentVolumeClaim metadata: # some comment aosiertn oairsetn oarisetn oarisetn oarisent oarisetn oa risetn oairsetn name: hostpath-pvc-reboot-test spec: accessModes: - ReadWriteOnce storageClassName: manual-reboot-test resources: requests: storage: 500Mi`
> 
> Then i enabled yaml-ts-mode.
> I went to the commented line and did `back-to-indentation`, then `set-mark-command` followed by `move-end-of-line` and `fill-region`.
> And this is the result i got:
> 
> `apiVersion: v1 kind: PersistentVolumeClaim metadata: # some comment aosiertn oairsetn oarisetn oarisetn oarisent oarisetn oa risetn oairsetn name: hostpath-pvc-reboot-test spec: accessModes: - ReadWriteOnce storageClassName: manual-reboot-test resources: requests: storage: 500Mi`
> 
> I expect it to be like this:
> 
> `apiVersion: v1 kind: PersistentVolumeClaim metadata: # some comment aosiertn oairsetn oarisetn oarisetn oarisent oarisetn # oa risetn oairsetn name: hostpath-pvc-reboot-test spec: accessModes: - ReadWriteOnce storageClassName: manual-reboot-test resources: requests: storage: 500Mi`
> 
> I use emacs from the master branch.
> 

Thanks, and sorry for the delay.

I've attached a patch that fixes it for me.

From f13b1c275ba454678baca532e810f1ce0a7909bf Mon Sep 17 00:00:00 2001
From: Randy Taylor <[email-masked]>
Date: Sat, 5 Apr 2025 15:52:45 -0400
Subject: [PATCH] Fix yaml-ts-mode comment filling (Bug#77095)

* lisp/textmodes/yaml-ts-mode.el (yaml-ts-mode--fill-paragraph):
Handle comment filling differently than block_scalars.
---
 lisp/textmodes/yaml-ts-mode.el | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/lisp/textmodes/yaml-ts-mode.el b/lisp/textmodes/yaml-ts-mode.el
index 7d11223aefa..0fa2f489ea7 100644
--- a/lisp/textmodes/yaml-ts-mode.el
+++ b/lisp/textmodes/yaml-ts-mode.el
@@ -129,18 +129,21 @@ yaml-ts-mode--fill-paragraph
   (save-restriction
     (widen)
     (let ((node (treesit-node-at (point))))
-      (if (member (treesit-node-type node) '("block_scalar" "comment"))
-        (let* ((start (treesit-node-start node))
-               (end (treesit-node-end node))
-               (start-marker (point-marker))
-               (fill-paragraph-function nil))
-          (save-excursion
-            (goto-char start)
-            (forward-line)
-            (move-marker start-marker (point))
-            (narrow-to-region (point) end))
-          (fill-region start-marker end justify))
-        t))))
+      (pcase (treesit-node-type node)
+        ("block_scalar"
+         (let* ((start (treesit-node-start node))
+                (end (treesit-node-end node))
+                (start-marker (point-marker))
+                (fill-paragraph-function nil))
+           (save-excursion
+             (goto-char start)
+             (forward-line)
+             (move-marker start-marker (point))
+             (narrow-to-region (point) end))
+           (fill-region start-marker end justify)))
+        ("comment"
+         (fill-comment-paragraph justify))))
+    t))
 
 (defun yaml-ts-mode--defun-name (node)
   "Return the defun name of NODE.
-- 
2.48.1