DEV Community

Erick πŸ™ƒ Navarro
Erick πŸ™ƒ Navarro

Posted on • Originally published at erick.navarro.io on

Moving to Emacs Tree Sitter Modes

I'm currently using emacs 30 and this version has support for more languages using the new ts-modes, I've been using emacs-tree-sitter package since Emacs added dynamic modules feature.

Now using emacs 30 I give a try to use only ts-modes and maybe delete some external packages I was using.

How to install grammars

Emacs doesn't have a built-in way to install automatically most common grammar so we're going to use a package that can handle that, we use tree-sitter-langs package, which is a repository for grammars.

When we install it it download and install all the available grammars in its own directory but to be used by emacs ts-modes we need to put them inside /.emacs.d/tree-sitter directory. I write a helper function to accomplish this:

(defun my/copy-grammars-to-emacs-tree-sitter-dir ()
  "Copy tree-sitter grammar files to native Emacs dir."
  (interactive)
  (let* ((files (directory-files (tree-sitter-langs--bin-dir) nil "\\.dylib$")))
    (dolist (grammar-file files)
      (copy-file (concat (tree-sitter-langs--bin-dir) grammar-file) (concat (expand-file-name user-emacs-directory) "tree-sitter/" "libtree-sitter-" grammar-file) t)
      (message "%s grammar files copied" (length files)))))
Enter fullscreen mode Exit fullscreen mode

The migration

I use straight.el to manage my packages so I'm going to use it to configure built-in ts-modes, by now I only migrated a few modes.

For elixir:

(use-package elixir-ts-mode
  :straight (:type built-in)
  :mode (("\\.ex\\'" . elixir-ts-mode)
         ("\\.exs\\'" . elixir-ts-mode)
         ("\\mix.lock\\'" . elixir-ts-mode)))
Enter fullscreen mode Exit fullscreen mode

For docker:

(use-package dockerfile-ts-mode
  :straight (:type built-in)
  :defer t
  :mode (("\\Dockerfile\\'" . dockerfile-ts-mode)
         ("\\.dockerignore\\'" . dockerfile-ts-mode)))
Enter fullscreen mode Exit fullscreen mode

For typescript

(use-package typescript-ts-mode
  :straight (:type built-in)
  :defer t
  :mode "\\.tsx?\\'")
Enter fullscreen mode Exit fullscreen mode

For toml

(use-package toml-ts-mode
  :straight (:type built-in)
  :mode "\\.toml\\'"
  :defer t)
Enter fullscreen mode Exit fullscreen mode

For yaml

(use-package yaml-ts-mode
  :straight (:type built-in)
  :mode "\\.ya?ml\\'")
Enter fullscreen mode Exit fullscreen mode

With this now I have 5 dependencies less in my configuration :).

Conclusion

I'm in the process to migrate others modes like python-mode, go-mode and so on but in some cases syntax highlighting is not the best. For example shell-script-mode has better highlight than bash-ts-mode. I'm been using these "new" ts-modes for a few weeks and everything is working well.

Bonus

I also updated a package I use to run ispell on text nodes to support built-in tree-sitter support. Now it supports tree-sitter.el and treesit packages.

Top comments (0)