diff --git a/emacs/.emacs.d/config/init-treesit.el b/emacs/.emacs.d/config/init-treesit.el index 7ea16a3..9fb898b 100644 --- a/emacs/.emacs.d/config/init-treesit.el +++ b/emacs/.emacs.d/config/init-treesit.el @@ -107,6 +107,32 @@ (->> (treesit-query-capture args '((string (string_content) @name))) (-filter (lambda (n) (eq (car n) 'name))) (-map #'cdr) - (-mapcat (lambda (n) (treesit-node-text n t))))))) + (-mapcat (lambda (n) (treesit-node-text n t)))))) + + (defun origami-yaml-treesit-parser (create) + (lambda (content) + (let* ((parser (treesit-parser-create 'yaml)) + (root (treesit-parser-root-node parser))) + (cl-labels ((get-node-folds (node) + (if (equal (treesit-node-type node) "block_mapping_pair") + (let ((key-node (treesit-node-child-by-field-name node "key")) + (value-node (treesit-node-child-by-field-name node "value"))) + (list (funcall create + (save-excursion + (goto-char (treesit-node-start node)) + (line-beginning-position)) + (treesit-node-end node) + (save-excursion + (goto-char (treesit-node-start key-node)) + (+ 1 (- (treesit-node-end key-node) (line-beginning-position)))) + (-mapcat #'get-node-folds (treesit-node-children value-node))))) + (-mapcat #'get-node-folds (treesit-node-children node))))) + (get-node-folds root))))) + + (with-eval-after-load 'origami + (add-to-list 'origami-parser-alist '(yaml-ts-mode . origami-yaml-treesit-parser)) + (defun yaml-ts-origami-setup () + (evil-local-set-key 'normal (kbd "zM") #'origami-toggle-all-nodes-same-level)) + (add-hook 'yaml-ts-mode-hook #'yaml-ts-origami-setup))) (provide 'init-treesit)