New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Clojure-mode tree sitter support in Emacs v29 #640
Comments
The current wisdom from the Emacs mailing list is to introduce tree-sitter support in a new distinct mode, in this case With that in mind, I really do think creating a separate I don't really want to do that though without talking to the maintainers of this package first. What are your thoughts? Were there already plans underway or in the making to do this? |
While they always introduced a new mode they only introduced completely new files when it made sense. |
Including that code in the same file of python.el is a lot easier when it's part of Emacs though since it doesn't need to work with emacs 28 and below. Even if a clojure-ts-mode is written here I think there is a strong argument for keeping it in a separate file / package.
What else is in clojure-mode besides some refactoring tools, font-locking, and indentation (all I personally rely on is font-locking and indent)? I believe tree sitter will take over the last two.
I'd imagine a lot of the refactoring code could greatly benefit from tree-sitter, and rewriting them to use tree-sitter would simplify the code and make it more performant. That's mostly speculation on my part though. |
I'd welcome help with the introduction of tree-sitter support, but I think it'd be a huge mistake to move for something to be included in Emacs. We'd gain almost nothing, but we'd limit who can contribute to the package. Not to mention that now essentially the same people people maintain Even today clojure-mode is part of NonGNU ELPA, so it's trivial to install out-of-the-box. I honest don't get people's obsession to push more things into Emacs's core with all the implications of this. |
Probably you're right. I'm not particularly familiar with TreeSitter, so I can't say much on the topic. I'd certainly welcome simpler font-locking and indentation logic (much of the indentation logic we've borrowed from lisp-mode). There's the practical matter that people will actually need Emacs 29 for this to work, that all Clojure dev tools depend on clojure-mode (e.g. to identify the boundaries of expressions or to font-lock results) and that we have to be careful for the sake of people who can't easily upgrade their Emacs (e.g. people constrained by some company policies). That'd be a tricky problem to solve. My suggestion would be to start the new mode that you propose here, alongside the existing mode and to figure out how to integrate with the broader ecosystem as we go. As noted above I don't see much point in trying to have a Clojure mode built-into Emacs. If it were up to me - I'd move 2/3 of Emacs into packages - slim down the core and make it easier to release updates. And get rid of the damn contributor agreement. |
I skipped over this part, so clearly you're aware of the main problem with the introduction of the tree-sitter support. Again, I'm not against creating a separate package. I think somewhere in the issues here you'll find some plans to completely decouple the current package from lisp-mode, as there's plenty of legacy coming directly from it. Perhaps that's the right opportunity for a clean start. My only preference is to keep developing all Clojure tools for Emacs under the same umbrella, as we've done for the past decade. (I brought all the main Clojure projects for Emacs together in a single organization in early 2013 if I recall correctly) |
Keeping it here is fine by me, adding something to Emacs was just a thought I wanted to float. I didn't realize clojure was in non-gnu elpa. Having it available in one of the built in package repositories is the next best thing. I won't touch your other comments about the Emacs contribution process 😉
How about this for a start: I'll see what it looks like to make a separate |
@dannyfreeman Sounds like a great plan to me! |
Here is a POC with tree sitter parsing clojure files: https://github.com/dannyfreeman/clojure-mode/blob/master/clojure-ts-mode.el Something interesting we will need to figure out is how to distribute the tree-sitter grammer, which are compiled dynamic libraries. Do we precompile them for major OS/architectures combinations, do we include some setup script for users to compile it on their machines? Edit: right now it's not clear to me how the built in tree sitter modes like for python and javascript are going to be distributed. I'll dig into that and look through the mailing list to see if the question of external packages adding tree sitter grammers had come up yet. |
Nice! Do functions like
I think most modes that require modules to be compiled to some combination of both - you can compile the module locally or download it precompiled. The only one I've recently used was
Roger that! Btw, is there also some official docs of the tree sitter support in Emacs that we can peruse? I'm curious about everything it has to offer. |
Here are the tree-sitter notes in the emacs mirror repo, including a starter guide for how to add support for major modes: https://github.com/emacs-mirror/emacs/tree/master/admin/notes/tree-sitter |
I'm using an existing grammer, I think the same one used by neovim: https://github.com/sogaiu/tree-sitter-clojure Here's an example, where
I'm not sure what the problem is because the tree sitter grammer correctly identifies the keyword as 1 unit, a
I've been using the same thing @kommen posted. I've also been looking in the master branch's |
Looking very promising. Something that is stumping me right now though is highlighting namespaced keywords like clojure-mode does, where the namespace part and name part have different faces. I've asked in the grammer repo and in the mailing list about it today: This grammer is really pleasant to work with. It identifies metadata easily, so we can apply font locking to metadata, typehints without any trouble. I added a special rule that highlights the |
Some package managers like Nix, are going to have built-in support for grammars, and easily be able to include clojure. Outside, of this, we may be able to petition emacs-tree-sitter/tree-sitter-langs#143 to get clojure added as one of the supported language which does prebuilt binaries as you're saying. I downloaded https://github.com/emacs-tree-sitter/tree-sitter-langs/releases/download/0.12.8/tree-sitter-grammars-macos-0.12.8.tar.gz but they have virtually every language - but clojure. As a final fallback, vterm approach sounds viable, the compiled library can be added to
amazing work! i will try out your POC when I get a chance. |
If anyone is looking for a build of emacs with tree sitter support and clojure grammar, this should work on all platforms
|
That could be useful. It should be noted that I'm not using the emacs-tree-sitter package. I'm using the one built into Emacs 29. They have different APIs, and the one in Emacs core is much faster. |
No doubt, but the parser binaries in tree-sitter-langs should still work with the new emacs master 'treesit', afaik. |
I made a draft pr for this to start gathering feedback #644 |
@bbatsov I'm starting to think that the development of a clojure-ts-mode might be better done under a different repository rather than in this repository clojure mode. Users could opt into installing it if they are interested and can use new functionality in Emacs like It's going to remain in some kind of "beta" stage for a long time I suspect. There are some outstanding bugs with tree-sitter that we have discovered that may not be fixed for while: sogaiu/tree-sitter-clojure#32 These bugs affect basic functions like What do you think about that? Would it be possible to create a separate repo under the clojure-emacs org for this? I would happily maintain it if given access. When it stabilizes and Emacs 29 is more widespread we could consider merging it back in here. I think that would be a year or more down the line though. |
I've been thinking about this myself and I completely agree, so here you go https://github.com/clojure-emacs/clojure-ts-mode You'll have admin access to the repo and the original clojure-mode repo once you accept the invite I sent you. I think a separate repo will simplify distribution and documentation a lot anyways, and I really want to publish your work very soon to MELPA (at least), so it'd be easier for people to play with it. |
Thank you! I'll get to work on that repo soon. I can handle getting it on melpa and nongnu elpa. I can try to get it published there a little after Christmas. I'll also close down my PR on this repo. |
@dannyfreeman Sounds like a plan to me! Let's close this ticket and have all the conversations about I'll add a mention to clojure-mode's README about it, so it's easier to discover. |
Feature Request: Clojure-mode tree sitter support in Emacs v29
The text was updated successfully, but these errors were encountered: