Subject: bug#68226: [PATCH] Improve block_node handling for yaml-ts-mode fill-paragraph

From: Graham Marlow <[email-masked]>
Date: Tue, 2 Jan 2024 14:10:53 -0800

Hello Emacs maintainers,

I noticed that the yaml-ts-mode fill-paragraph function doesn't play 
nicely with block nodes, making it difficult to tidy paragraphs. For 
example,

foo: |
   line-one
   line-two

Becomes

foo: | line-one line-two

Effectively undoing the block.

My proposed changes (see attached patch) fixes this behavior so that 
block nodes remain formatted correctly:

foo: |
   line-one line-two

It also plays nicely with longer pieces of text and multiple paragraphs.

I tried to base my changes off of the fill-paragraph function from 
c-ts-common, though the YAML version is much simpler since it doesn't 
need to account for comment insertion. I've not written a fill-paragraph 
function before so I'm sure I messed something up.

What do you think?

From 43640b2d45f87b0b394817d2957cde341736d200 Mon Sep 17 00:00:00 2001
From: Graham Marlow <[email-masked]>
Date: Tue, 2 Jan 2024 13:58:22 -0800
Subject: [PATCH] Improve block_node handling for yaml-ts-mode fill-paragraph

When using fill-paragraph on a block_scalar (the element within a
block_node) fill the paragraph such that the contents remain
within the block_node. This fixes the previous behavior that would
clobber a block_node.

* lisp/textmodes/yaml-ts-mode.el: Add yaml-ts-mode--fill-paragraph
---
 lisp/textmodes/yaml-ts-mode.el | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/lisp/textmodes/yaml-ts-mode.el b/lisp/textmodes/yaml-ts-mode.el
index 2b57b384300..08fe4c49733 100644
--- a/lisp/textmodes/yaml-ts-mode.el
+++ b/lisp/textmodes/yaml-ts-mode.el
@@ -117,6 +117,26 @@
    '((ERROR) @font-lock-warning-face))
   "Tree-sitter font-lock settings for `yaml-ts-mode'.")
 
+(defun yaml-ts-mode--fill-paragraph (&optional justify)
+  "Fill paragraph.
+Behaves like `fill-paragraph', but respects block node
+boundaries.  JUSTIFY is passed to `fill-paragraph'."
+  (interactive "*P")
+  (save-restriction
+    (widen)
+    (let ((node (treesit-node-at (point))))
+      (when (string= "block_scalar" (treesit-node-type node))
+        (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))))))
+
 ;;;###autoload
 (define-derived-mode yaml-ts-mode text-mode "YAML"
   "Major mode for editing YAML, powered by tree-sitter."
@@ -141,6 +161,8 @@
                   (constant escape-sequence number property)
                   (bracket delimiter error misc-punctuation)))
 
+    (setq-local fill-paragraph-function #'yaml-ts-mode--fill-paragraph)
+
     (treesit-major-mode-setup)))
 
 (if (treesit-ready-p 'yaml)
-- 
2.39.3 (Apple Git-145)