reddit / emacs

Having trouble setting up tree-sitter

SnooPets20 · 2022-08-07 06:09
For those who don't know, tree-sitter is "an incremental parsing system". What it basically means is that it provides fancy syntax highlighting by doing a thorough analysis of the code. Emacs has treesitter integration via the `tree-sitter` package. I was using tree-sitter before on neovim and it really great. Well, I'm trying to set it up in emacs and it's pure frustration. I've installed the package, and it says that you should also install a language pack. Well, since I was already using treesitter before, then I prefer to not have to compile the language parsers twice, so I decided to unify it in a single directory for both neovim and emacs. Making the change on neovim was a piece of cake. On emacs, I pushed to the `tree-sitter-load-path` list, and it seems to work... kinda. If I manually call `tree-sitter-require 'python` for example, it does load the language parser, it even gets listed in `tree-sitter-languages`. However, it doesn't get enabled, the syntax is the same. The variable `tree-sitter-language` is not getting set. Apparently tree-sitter should map each language major mode to a tree-sitter parser, but it ain't doing it for some reason. Trying to enable treesitter on a python file results in the error "No language registered for "python-mode"". Also what about these major modes for each language? I don't want a major mode for each language. I want emacs to open a file, recognize what it is by its extension or otherwise, and that's it. I want highlighting to be handled by treesitter, but it apparently needs to interact with these major modes (of which I need to install more if I want to support other languages) that don't really add anything to me other than bloat. Is there a workaround?
MonsieurPi · 2022-08-07 09:23
As stated in the installation section of emacs tree sitter you need to load two packages, tree-sitter and tree-sitter-langs https://emacs-tree-sitter.github.io/installation/#installing-from-melpa With those packages loaded you can just hook your major-mode to tree-sitter and tree-sitter-hl-mode and it'll work perfectly And I don't know what to answer to your "major mode for each language". You don't have to load them if you don't want to and you can redo things on your own, they're just extremely useful. Like lsp.
adivinity · 2022-08-07 17:02
Here the relevant part of my configuration: https://www.lucacambiaghi.com/vanilla-emacs/readme.html#h:1FD9CDF3-4C5D-450E-99DE-034077C3D8BC
SnooPets20 · 2022-08-07 17:17
Ah thanks. I didn't install tree-sitter-langs since I thought it would redownload the language parsers that I already had, so I skipped it. Well, its working now, although I wouldn't know how to add more parsers (if not using neovim). Does emacs have an interface to do this or do I need to manually clone their repos and compile them?
MonsieurPi · 2022-08-07 18:15
https://github.com/emacs-tree-sitter/tree-sitter-langs Parsers for known languages are [here](https://github.com/emacs-tree-sitter/tree-sitter-langs/tree/master/repos) If the one you're looking for is missing that's an issue with tree-sitter, not the emacs package. And parsers are addedfor you by tree-sitter-langs if I understand correctly.