]> git.eshelyaron.com Git - emacs.git/commitdiff
Improve yaml-ts-mode fill-paragraph (bug#68226)
authorGraham Marlow <graham@mgmarlow.com>
Tue, 2 Jan 2024 21:58:22 +0000 (13:58 -0800)
committerYuan Fu <casouri@gmail.com>
Fri, 5 Jan 2024 00:40:35 +0000 (16:40 -0800)
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

index 2b57b3843003fff1301b04ab3de7bc5030437ff0..08fe4c49733f25df0efd7a5e8d383215e997a1a5 100644 (file)
    '((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."
                   (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)