unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#68226: [PATCH] Improve block_node handling for yaml-ts-mode fill-paragraph
@ 2024-01-02 22:10 Graham Marlow
  2024-01-03  4:41 ` Yuan Fu
  0 siblings, 1 reply; 4+ messages in thread
From: Graham Marlow @ 2024-01-02 22:10 UTC (permalink / raw)
  To: 68226

[-- 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)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* bug#68226: [PATCH] Improve block_node handling for yaml-ts-mode fill-paragraph
  2024-01-02 22:10 bug#68226: [PATCH] Improve block_node handling for yaml-ts-mode fill-paragraph Graham Marlow
@ 2024-01-03  4:41 ` Yuan Fu
  2024-01-03  5:21   ` Graham Marlow
  0 siblings, 1 reply; 4+ messages in thread
From: Yuan Fu @ 2024-01-03  4:41 UTC (permalink / raw)
  To: Graham Marlow; +Cc: 68226



> On Jan 2, 2024, at 2:10 PM, Graham Marlow <graham@mgmarlow.com> wrote:
> 
> 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?<0001-Improve-block_node-handling-for-yaml-ts-mode-fill-pa.patch>

Thank you! This is great. The function assumes that the block starts at the line below “|”, but I think it’s very rare when people write string block right after the “|”.

Have you signed the copyright assignment?

Yuan




^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#68226: [PATCH] Improve block_node handling for yaml-ts-mode fill-paragraph
  2024-01-03  4:41 ` Yuan Fu
@ 2024-01-03  5:21   ` Graham Marlow
  2024-01-05  0:42     ` Yuan Fu
  0 siblings, 1 reply; 4+ messages in thread
From: Graham Marlow @ 2024-01-03  5:21 UTC (permalink / raw)
  To: Yuan Fu; +Cc: 68226

> Thank you! This is great. The function assumes that the block starts at 
> the line below “|”, but I think it’s very rare when people write string 
> block right after the “|”.

Happy to help!

> Have you signed the copyright assignment?

I have indeed. 

Best,
Graham





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#68226: [PATCH] Improve block_node handling for yaml-ts-mode fill-paragraph
  2024-01-03  5:21   ` Graham Marlow
@ 2024-01-05  0:42     ` Yuan Fu
  0 siblings, 0 replies; 4+ messages in thread
From: Yuan Fu @ 2024-01-05  0:42 UTC (permalink / raw)
  To: Graham Marlow; +Cc: 68226-done



> On Jan 2, 2024, at 9:21 PM, Graham Marlow <graham@mgmarlow.com> wrote:
> 
>> Thank you! This is great. The function assumes that the block starts at 
>> the line below “|”, but I think it’s very rare when people write string 
>> block right after the “|”.
> 
> Happy to help!
> 
>> Have you signed the copyright assignment?
> 
> I have indeed. 

Installed on master, thanks!

Yuan




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-01-05  0:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-02 22:10 bug#68226: [PATCH] Improve block_node handling for yaml-ts-mode fill-paragraph Graham Marlow
2024-01-03  4:41 ` Yuan Fu
2024-01-03  5:21   ` Graham Marlow
2024-01-05  0:42     ` Yuan Fu

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