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.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here β†’

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay