unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Graham Marlow <graham@mgmarlow.com>
To: 68226@debbugs.gnu.org
Subject: bug#68226: [PATCH] Improve block_node handling for yaml-ts-mode fill-paragraph
Date: Tue, 2 Jan 2024 14:10:53 -0800	[thread overview]
Message-ID: <9d583305-9cdb-54bb-6e72-063b2c2a611e@mgmarlow.com> (raw)

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

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?

[-- Attachment #2: 0001-Improve-block_node-handling-for-yaml-ts-mode-fill-pa.patch --]
[-- Type: text/x-patch, Size: 2138 bytes --]

From 43640b2d45f87b0b394817d2957cde341736d200 Mon Sep 17 00:00:00 2001
From: Graham Marlow <graham@mgmarlow.com>
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)


             reply	other threads:[~2024-01-02 22:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-02 22:10 Graham Marlow [this message]
2024-01-03  4:41 ` bug#68226: [PATCH] Improve block_node handling for yaml-ts-mode fill-paragraph Yuan Fu
2024-01-03  5:21   ` Graham Marlow
2024-01-05  0:42     ` Yuan Fu

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=9d583305-9cdb-54bb-6e72-063b2c2a611e@mgmarlow.com \
    --to=graham@mgmarlow.com \
    --cc=68226@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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).