Clojure and Tree-sitter

By now Emacs users are well aware of Tree-sitter1 and the built-in integration that comes with Emacs 29.2 I am a bit late to the party because only recently I have started to look into treesit-language-source-alist and treesit-install-language-grammar, but right now I am more interested in how Clojure programming can benefit from Tree-sitter.

Danny Freeman is the maintainer of clojure-ts-mode,3 and after I kindly nudged him he made it available on NonGNU ELPA.4 This means that the package is only a M-x package-install away.

Once installed Emacs has to know how to prefer clojure-ts-mode to good old clojure-mode. This is where majore-mode-remap-alist comes into play.

(push '(clojure-mode . clojure-ts-mode) major-mode-remap-alist)
(push '(clojurec-mode . clojurec-ts-mode) major-mode-remap-alist)
(push '(clojurescript-mode . clojurescript-ts-mode) major-mode-remap-alist)

Since I always use CIDER let’s leverage clojure-ts-mode-hook to enable it.

(add-hook 'clojure-ts-mode-hook #'cider-mode)

This is enough to activate clojure-ts-mode, but upon entering a project I noticed the lack of a couple of useful key bindings.

(with-eval-after-load 'clojure-ts-mode
  (keymap-set clojure-ts-mode-map "C-c C-r" 'clojure-refactor-map)
  (keymap-set clojure-ts-mode-map "C-c C-x" 'cider-start-map))

Note that clojure-ts-mode is still under development. For instance, Danny is working on allowing users to pick their preferred indentation style.5 As Daw-Ran Liou suggested, for now I can use clojure-mode-variables to make clojure-ts-mode follow my preferred formatting settings.6

(add-hook 'clojure-ts-mode-hook #'clojure-mode-variables)

I have been using clojure-ts-mode daily in the last two weeks and the experience has been great. They say Tree-sitter is a game changer. I say thank you, Danny Freeman.