Write a treesit-based origami parser for yaml

This commit is contained in:
Jeremy Dormitzer 2024-05-03 21:40:25 -04:00
parent 178b7038ca
commit 7c67af0b5f

View File

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