4 ms·
When I first read about the integration of tree-sitter into Emacs, I was very excited. I work with a DSL, for which I maintain a tree-sitter grammar and highlig
by vzaliva 2y ago
When I first read about the integration of tree-sitter into Emacs, I was very excited. I work with a DSL, for which I maintain a tree-sitter grammar and highlighting rules. I can view source files with highlighting from the command line, and I was hoping I could now easily re-use this grammar in Emacs to edit files in my DSL with proper highlighting.
Unfortunately, it wasn't as straightforward as I hoped. You need to create a custom major mode for your language and manually integrate the tree-sitter highlighting.
What I'd really like to see one day is an Emacs mode that allows you to automatically plug in any tree-sitter grammar with just a couple of lines of configuration in your .emacs, and instantly get syntax highlighting. Is that too much to ask?
- toomim 2y agoThat should be easy to build. In 2002 I built harmonia-mode, which did that for the harmonia research project that inspired tree-sitter. It did that.
- pama 2y agoI would contribute to a repo that works towards this goal.
- toomim 2y agoThe best way forward is similar to how you describe. Instead of making one mode per language -- just make a generic "tree-sitter" mode, and attach that mode to all the filetypes you want it load via regexp patterns in `auto-mode-alist`. Then when the file goes into tree-sitter-mode, you can check the filetype again, and map that into the language to load into tree-sitter. Keep a buffer-local variable to remember that current language, so that you can use it for any additional language-specific customization that you want as well. Keep in mind that there's nothing about a major mode in Emacs that has to be specific to a programming language. It's totally cool to have a major mode that works for multiple programming languages!
- vzaliva 2y agoI think the main problem is that highligthing framework used by tree-sitter (https://github.com/tree-sitter/tree-sitter/tree/master/highlight https://github.com/tree-sitter/tree-sitter/tree/master/highl...) is not easily pluggable into emacs font-lock-mode.
- toomim 2y agoI wrote similar code in emacs for harmonia-mode in the past. Let me find and share the source code. We don't need font-lock. We can just borrow the styles it uses. *Update:* I found the code. Instead of using font-lock, we simply draw our own overlays over the text and color them, which is what font-lock does. Font-lock IIRC is specifically designed to use regexps to parse the text. We don't need that. So throw font-lock away. Tree-Sitter itself knows how the parse tree maps to text regions. Just use that information to draw the overlays directly. It's way simpler that way. Lemme know if anyone wants the code.
- vzaliva 2y agoIt's starting to sound like it's time to create a GitHub repository and start putting it all together. My elisp skills are basic, so I could help more with testing, coordination, publishing packages, or documentation. Shall I create the repo?
- vzaliva 2y agorepo created. I've sent you a request on github.
- amitp 2y agoWith the older tree-sitter package[1], I was able to use it with the existing major modes. The new built-in emacs tree-sitter seems to be more ambitious, involving new major modes. [1] https://emacs-tree-sitter.github.io/ https://emacs-tree-sitter.github.io/
- lambda_foo 2y agoSure, you will need to write a major mode but honestly it's not that hard to get syntax highlighting working. If you follow something like https://www.masteringemacs.org/article/lets-write-a-treesitter-major-mode https://www.masteringemacs.org/article/lets-write-a-treesitt... plus using M-x treesit-explore-mode it took me 1 day to get treesitter support for OCaml, starting from knowing nothing about how Treesitter works with Emacs to a semi-decent highlighting and indentation setup. The indentation was harder to get working, but all up 300 lines of elisp including whitespace and comments.