reddit / emacs

yaml-pro v0.3: Tree-sitter support

Prudent-Dimension554 · 2022-12-22 14:55
[https://github.com/zkry/yaml-pro](https://github.com/zkry/yaml-pro) My first implementation of yaml-pro used an elisp yaml parser which was really slow. In the latest version of yaml-pro, I've re-implemented all of the features to use the new Emacs tree-sitter integration. Not only was it much easier to implement, but the resulting code also runs much faster and can handle syntax errors. Also, I was able to get eldoc and imenu support in using tree-sitter.
whyimihere2019 · 2022-12-22 18:45
How did you learn to use tree-sitter from elisp?
Prudent-Dimension554 · 2022-12-22 19:01
The documentation in the Emacs repository is really good. I'm not sure if it's online or not but you can find it here: [https://github.com/emacs-mirror/emacs/tree/master/admin/notes/tree-sitter/html-manual](https://github.com/emacs-mirror/emacs/tree/master/admin/notes/tree-sitter/html-manual) Here's a quick summary of the functions I used: - Use `treesit-ready-p` to check if you can use tree-sitter for a language and `treesit-parser-create` to create a parser. - `treesit-node-at` can be used to get a node at a point. `treesit-buffer-root-node` gets the topmost node. - The following functions get properties of nodes: `treesit-node-start`, `treesit-node-end`, `treesit-node-parent`, `treesit-node-prev-sibling`, `treesit-node-prev-sibling` - `treesit-node-index` can be used to determine what number child the node is to its parent. - `treesit-parent-until` is a helper function to repeatedly look up to find an ancestor that satisfies a certain condition. - `treesit-parse-string` can be used to parse an arbitrary string in an arbitrary language - `treesit-query-capture` can be used to run a tree-sitter query So for example, if we want to mark a YAML subtree, we first get the node where the point is with `(treesit-node-at (point))`. Then we use `treesit-parent-until` to find the parent that has type "block_mapping_pair". Then we can mark the start and end of this node with `treesit-node-start` and `treesit-node-end` to get the bounds of the tree.
vaxdar · 2022-12-23 00:04
Coincidentally, I just set this up yesterday. I've barely used it so far, but it's looking really good. Thanks so much for your work here, I've been wanting better yaml handling in emacs for a while.
-xylon · 2022-12-23 08:12
This is some "beggar question" but: do you know CUE? How do you see an emacs mode for it, would it be easy going from yaml or not?
northWe5t · 2022-12-23 09:27
I know absolutely nothing about treesitter’s capabilities, but do you mean `treesit-node-end` in the latter?
dakra · 2022-12-23 15:07
yaml-pro seems to have a lot of features, could it make sense to pull out a simple `yaml-ts-mode` with syntax highlighting, imenu support etc and upstream this to Emacs directly? With treesitter there seems to be quite a few new major-modes that where not supported before (e.g. json-ts-mode). I think a `yaml-ts-mode` in Emacs directly would nice.
Prudent-Dimension554 · 2022-12-23 17:49
That's a good idea. I was originally thinking to reach out to the author of yaml-mode to see what he thinks about adding it there, but adding something to Emacs directly is a good idea as well. I'll definitely see where I can take this.
Prudent-Dimension554 · 2022-12-23 17:49
oh yeah, there was a typo there
Prudent-Dimension554 · 2022-12-23 17:56
It definitely looks interesting but I haven't gotten the chance to use it yet so I'm not sure.