Subject: [PATCH] Don't fill yaml except comments and block scalars.
From: Rudolf Schlatte <[email-masked]>
To: [email-masked]
Date: Sun, 28 Jan 2024 14:15:58 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

Tags: patch

Hi,

Currently, yaml-ts-mode fills comments and block scalars (multi-line
text literals) as expected, but re-fills the whole file when point is
outside of either of these constructs.  Since yaml line breaks and
whitespace are significant, I'd say that this is never the correct
behavior.

This patch against current master inhibits M-q (fill-paragraph) outside
of comments and block scalars.  In my tests default fill-paragraph
worked as expected both with and without justify, correctly detecting
comment and block literal boundaries, so I did not preserve the previous
code in `yaml-ts-mode--fill-paragraph'.

From 003e9f0dbd20059dbf49761a7537658947a98d55 Mon Sep 17 00:00:00 2001
From: Rudolf Schlatte <[email-masked]>
Date: Sun, 28 Jan 2024 13:54:35 +0100
Subject: [PATCH] Don't fill yaml except comments and block scalars.

Indentation and line breaks are significant syntax, so only fill
reflowable nodes.

* lisp/textmodes/yaml-ts-mode.el (yaml-ts-mode--fill-paragraph): Check
if point is inside a comment or block scalar, do not fill otherwise.
---
 lisp/textmodes/yaml-ts-mode.el | 28 ++++++++++------------------
 1 file changed, 10 insertions(+), 18 deletions(-)

diff --git a/lisp/textmodes/yaml-ts-mode.el b/lisp/textmodes/yaml-ts-mode.el
index c0185457bc2..301d7ba03a9 100644
--- a/lisp/textmodes/yaml-ts-mode.el
+++ b/lisp/textmodes/yaml-ts-mode.el
@@ -120,25 +120,17 @@ yaml-ts-mode--font-lock-settings
    '((ERROR) @font-lock-warning-face))
   "Tree-sitter font-lock settings for `yaml-ts-mode'.")
 
-(defun yaml-ts-mode--fill-paragraph (&optional justify)
+(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))))))
+Hand over to `fill-paragraph' if point is inside a comment or
+block scalar; do nothing otherwise."
+  (let ((node (treesit-node-at (point)))
+        (fillable-types '("block_scalar" "comment")))
+    (if (member (treesit-node-type node) fillable-types)
+        ;; Explicitly return these two specific values; see
+        ;; `fill-paragraph-function' documentation.
+        nil
+      t)))
 
 ;;;###autoload
 (define-derived-mode yaml-ts-mode text-mode "YAML"
-- 
2.43.0