all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Rudolf Schlatte <rudi@constantly.at>
To: 68781@debbugs.gnu.org
Subject: bug#68781: [PATCH] Don't fill yaml except comments and block scalars.
Date: Sun, 28 Jan 2024 14:15:58 +0100	[thread overview]
Message-ID: <m18r49iooh.fsf@constantly.at> (raw)

[-- Attachment #1: Type: text/plain, Size: 625 bytes --]

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'.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Don-t-fill-yaml-except-comments-and-block-scalars.patch --]
[-- Type: text/patch, Size: 2220 bytes --]

From 003e9f0dbd20059dbf49761a7537658947a98d55 Mon Sep 17 00:00:00 2001
From: Rudolf Schlatte <rudi@Cafeolix.local>
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


             reply	other threads:[~2024-01-28 13:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-28 13:15 Rudolf Schlatte [this message]
2024-01-28 14:52 ` bug#68781: [PATCH] Don't fill yaml except comments and block scalars Rudolf Schlatte
2024-01-29  3:47 ` Randy Taylor
2024-01-29  8:20   ` Rudolf Schlatte
2024-01-29 14:08     ` Randy Taylor
2024-01-30  1:20       ` Graham Marlow
2024-01-30 10:15         ` Rudolf Schlatte
2024-01-30 19:25         ` Randy Taylor
2024-02-01 10:31           ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m18r49iooh.fsf@constantly.at \
    --to=rudi@constantly.at \
    --cc=68781@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.