reddit / emacs

How to use Emacs 29 Tree-sitter?

ticinese00 · 2022-12-03 20:08
I've been looking everywhere, but I can't seems to find any documentation on how to setup tree-sitter (the official one integrated in emacs 29). I know it is new and documentation might be missing for now. I'm on mac and using these builds https://github.com/jimeh/emacs-builds and `C-h v` and `system-configuration-features` outputs this system-configuration-features is a variable defined in ‘C source code’. Its value is "ACL DBUS GIF GLIB GMP GNUTLS JPEG JSON LCMS2 LIBXML2 MODULES NATIVE_COMP NOTIFY KQUEUE NS PDUMPER PNG RSVG SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS TREE_SITTER WEBP XIM XWIDGETS ZLIB" So, it is compiled with tree-sitter. When I activate any major mode like `js-ts-mode` or `tsx-ts-mode` I get ⛔ Warning (treesit): Cannot activate tree-sitter, because language definition for javascript is unavailable (not-found): (libtree- sitter-javascript.so libtree-sitter-javascript.dylib) No such file or directory How do I find languages definition and install them and make them available to emacs official tree-sitter?
ZythumFaulfair · 2022-12-03 20:30
That's exactly what I was wondering today
[deleted] · 2022-12-03 20:35
[deleted]
leungbk · 2022-12-03 20:51
You can install Nix on your mac and use https://github.com/nix-community/emacs-overlay/, which supports all the existing tree-sitter-based major modes OOB.
[deleted] · 2022-12-03 20:54
[deleted]
badmaxton · 2022-12-03 21:01
https://git.savannah.gnu.org/cgit/emacs.git/tree/admin/notes/tree-sitter/starter-guide?h=feature/tree-sitter has all the documentation about how to enable it and how to obtain the official grammars
ducbueno__ · 2022-12-03 21:02
Take a look at this https://blog.phundrak.com/emacs-29-what-can-we-expect/ perhaps it can help
cenderis · 2022-12-03 21:48
Clone `git@github.com:casouri/tree-sitter-module.git`, run `./batch.sh` (you may need to install `tree-sitter` to do that, I forget), then do `mv dist ~/.config/emacs/tree-sitter`.
fritzgrabo · 2022-12-03 21:48
Make sure you have tree-sitter installed (I used `brew install tree-sitter`). Next, clone the Javascript grammar for tree-sitter from [https://github.com/tree-sitter/tree-sitter-javascript](https://github.com/tree-sitter/tree-sitter-javascript) and compile it with `cc -shared -fPIC -g -O2 -I src src/scanner.c src/parser.c -o ~/.emacs.d/tree-sitter/libtree-sitter-javascript.dylib`. Note that I output the resulting file into the `~/.emacs.d/tree-sitter` directory (you might need to create that first), which is where Emacs is looking for it on my local machine; see `treesit-extra-load-path` for details. With all of the above in place, run `js-ts-mode` again and you should be all set. Good luck!
fritzgrabo · 2022-12-03 21:52
You might want to check out Abin Simon's recent talk on the subject at [https://emacsconf.org/2022/talks/treesitter/](https://emacsconf.org/2022/talks/treesitter/) \-- it's really, really well done and packs a ton of useful information into just 10 minutes. Definitely worth your time, imo.
alecStewart1 · 2022-12-03 22:33
To start: Is `treesitter` installed on your machine? I would assume so. Second: If so, are there other packages in your distro's package repo for each language definition? Like `treesitter-javascript` or `treesitter-languages`? You can also look in, I believe, `/usr/lib`, `/usr/lib64` or just `/lib` or `/lib64/` and see if those files Emacs mentions are there already. For me, on Gentoo, I had to also install those packages as well as the treesitter package.
[deleted] · 2022-12-03 22:45
[deleted]
gdunk2 · 2022-12-04 00:07
Have you managed to get emacs to build with the `--with-tree-sitter` flag after installing tree-sitter from source, as described in the starter guide you linked? For me, the ./configure script fails saying that it can't find tree sitter even though everything is there under /usr/local (after running make && make install for tree-sitter)
kruetz · 2022-12-04 00:30
You might need to run `sudo ldconfig`?
alexjgriffith · 2022-12-04 00:33
To get it to run I installed tree sitter from the tree sitter github repo, using the latest tag https://github.com/tree-sitter/tree-sitter
tryptych · 2022-12-04 00:39
I wrote up my own notes: https://blog.markhepburn.com/posts/experimenting-with-the-built-in-treesitter-support-in-emacs/
badmaxton · 2022-12-04 00:50
I'm on OSX, and I'm building emacs 29 using emacs-plus, like this (the pbd lines were necessary for 28, not sure for 29): brew edit emacs-plus@29 > on line 74: "master" should be "emacs-29", then save and exit editor brew install tree-sitter brew install emacs-plus@29 --with-native-comp --with-imagemagick --with-poll alias pbd=/usr/libexec/PlistBuddy export emacsapp=`brew --prefix --installed emacs-plus@29`/Emacs.app export infoplist="${emacsapp}/Contents/Info.plist" pbd -c "Add :LSEnvironment dict" "${infoplist}" pbd -c "Add :LSEnvironment:PATH string" "${infoplist}" pbd -c "Set :LSEnvironment:PATH $(echo "$PATH")" "${infoplist}" pbd -c "Print :LSEnvironment" "${infoplist}" touch ${emacsapp} cp -R ${emacsapp} /Applications/Emacs-29.app Finally, using the emacs 29 instructions for tree-sitter to build the grammars, then to update init.el to enable tree-sitter and locate the compiled grammars
matixlol · 2022-12-04 00:51
Thanks for sharing, this looks awesome. Next level code edition.
jimehgeek · 2022-12-04 03:13
I’m the author of the macOS build of Emacs you’re using. And yes, out of the box the new built-in tree-sitter stuff is useless without relevant grammars. I’m hoping that Emacs’ own build process will be enhanced to support building and bundling the grammars. In the short term though, I’m planning to see if I can get my build setup to handle it. The author of the built-in tree-sitter implementation has published this repo with a build script to help you build various grammars: https://github.com/casouri/tree-sitter-module That said, if you want a more complete experience with tree-sitter right now, there’s a 3rd party implementation with support for a lot more languages, and also automatically downloads all supported grammars. It’s available here: https://github.com/emacs-tree-sitter/elisp-tree-sitter And [here](https://github.com/jimeh/.emacs.d/blob/4e33f79c290706099cc498743e0e3c1ab1d9e210/modules/text-editing/siren-tree-sitter.el) is my own config for it which I’ve been using for around a year now. Primarily it just adds improved and faster syntax highlighting for supported languages. It does offer functions to perform structural operations, but I don’t know if it enhances any of the supported major modes to take advantage of it. As the new built-in implementation comes in the form of new major modes, personally I’m planning to slowly switch from the 3rd party to the built-in implementation over time as things progress.
cradlemann · 2022-12-04 07:08
I installed tree-sitter-bash package for bash-ts-mode together with core tree-sitter package on Arch Linux
helm05 · 2022-12-04 07:45
See [https://lists.gnu.org/archive/html/bug-gnu-emacs/2022-11/msg02077.html](https://lists.gnu.org/archive/html/bug-gnu-emacs/2022-11/msg02077.html). Probably making a symlink of the \*.so.0 to /usr/lib/x86\_64-linux-gnu/ would work.
rr377 · 2022-12-04 17:27
>treesit-extra-load-path I don't have that variable. Where is it coming from ?
rr377 · 2022-12-04 17:37
I am trying to use it for c# but I can't get it to work even though I added \`csharp-mode\` to your config. Any idea?
jimehgeek · 2022-12-04 17:53
In the case of csharp-mode, it looks like it’s supported by tree-sitter-langs: https://github.com/emacs-tree-sitter/tree-sitter-langs/blob/388dcbbc237b2b1360e5b148e67b158eecede805/tree-sitter-langs.el#L105 However, there are no queries available for it to highlight csharp: https://github.com/emacs-tree-sitter/tree-sitter-langs/tree/388dcbbc237b2b1360e5b148e67b158eecede805/queries And the upstream tree-sitter-c-sharp grammar only has queries for tags, but not highlights: https://github.com/tree-sitter/tree-sitter-c-sharp/tree/70fd2cba742506903589b5e046c32e0e3e06404a/queries So it looks like it’s a matter of someone needing to write highlight queries for csharp-mode. I recently did so for yaml-mode for example. Tree-sitter features for structural querying and editing should work however.
rr377 · 2022-12-04 18:00
Any particular reasons you were looking at that old commit ? the latest master branch seems to have a [highlights.scm](https://github.com/tree-sitter/tree-sitter-c-sharp/tree/master/queries) file
jimehgeek · 2022-12-04 18:11
Oh, awesome. I just blindly followed the link from tree-sitter-langs and forgot to change to the latest commit 🤦‍♂️ That’s great news though, tree-sitter-langs basically just needs to update the csharp grammar and add the highlight queries. The extra good news is that you hopefully can get highlights working in a couple of minutes right now by a tweak to your emacs config. As long as the highlights don’t depend on anything new in the grammar parser itself, things should work with the older grammar included in tree-sitter-langs. To do so, you simply need to set `tree-sitter-hl-default-patterns` as a buffer-local variable within csharp buffers, populating it with the queries from that highlights files. I did a similar thing with yaml-mode highlight queries in my config before the highlights were merged in. You can see the relevant commit in my config here: https://github.com/jimeh/.emacs.d/commit/ebbdab36fb2a3d635d591cc3179b8f4ed9a476d0
rr377 · 2022-12-04 19:06
thanks!
fritzgrabo · 2022-12-04 19:11
It's part of the `treesit` package that's built into Emacs now (version 29 and master), and mentioned in the related [starter guide](https://github.com/emacs-mirror/emacs/blob/master/admin/notes/tree-sitter/starter-guide). Just to double-check: the new-ish `treesit` package is different from the Emacs `tree-sitter` package that's been around for a while now ([link](https://emacs-tree-sitter.github.io)).