Building Emacs Major Modes with TreeSitter: Lessons Learned
Over the past year I’ve been spending a lot of time building TreeSitter-powered major modes for Emacs – clojure-ts-mode (as co-maintainer), neocaml (from scratch), and asciidoc-mode (also from scratch). Between the three projects I’ve accumulated enough battle scars to write about the experience. This post distills the key lessons for anyone thinking about writing a TreeSitter-based major mode, or curious about what it’s actually like.

Over the past year, I’ve been deeply involved in building TreeSitter-powered major modes for Emacs, including clojure-ts-mode (as a co-maintainer), neocaml (from scratch), and asciidoc-mode (also from scratch). These projects have provided valuable insights into the process of creating a TreeSitter-based major mode, and I’ve learned a lot along the way. In this article, I’ll distill the key lessons for anyone considering writing a TreeSitter-based major mode or those curious about the experience.
First and foremost, understanding the TreeSitter ecosystem is crucial. TreeSitter is a language server that provides robust parsing, semantic analysis, and code navigation capabilities. It’s designed to be language-agnostic, meaning it can be configured to support any programming language. For Emacs users, integrating TreeSitter into major modes allows for enhanced editing experiences, including real-time syntax highlighting, autocompletion, and error highlighting.
One of the initial challenges I faced was setting up the TreeSitter environment. TreeSitter requires a language definition file (a .g.ts file) that specifies the grammar and semantics of the language. These files are generated using the TreeSitter Generator, which is a Ruby-based tool. The process involves writing a grammar in a domain-specific language (DSL) that describes the syntax of the language. For example, when working on clojure-ts-mode, I had to collaborate with the existing maintainers to ensure our grammar was accurate and up-to-date with Clojure’s evolving syntax.
Another significant learning point was the integration of TreeSitter with Emacs. Emacs provides the `tree-sitter-mode` package, which simplifies the setup process. However, there are nuances to consider. For instance, the `tree-sitter-mode` package requires the `company-mode` for autocompletion, which can be customized to enhance the user experience. Additionally, ensuring that the major mode correctly initializes the language server and handles language-specific settings was essential.
When developing a major mode from scratch, such as neocaml and asciidoc-mode, the process involved more than just writing the TreeSitter grammar. I had to create the necessary Emacs Lisp code to define the major mode, syntax highlighting, and key bindings. This required a solid understanding of Emacs Lisp and its extensibility model. For example, in asciidoc-mode, I had to define a custom parser to handle the unique syntax of AsciiDoc, which includes special characters like backticks and pipes.
A key lesson I learned was the importance of testing. Testing the TreeSitter grammar and the Emacs integration thoroughly was critical. The TreeSitter Generator provides a test suite that validates the grammar against a set of test cases. Similarly, testing the Emacs major mode with various files and edge cases helped identify and fix issues early on. For instance, when working on neocaml-mode, I encountered unexpected behavior with certain Caml constructs, which were resolved by refining the grammar and adjusting the Emacs code accordingly.
Documentation and community collaboration proved to be invaluable. Maintaining clear and concise documentation for the major mode, including setup instructions and usage examples, is essential for users. Additionally, engaging with the TreeSitter and Emacs communities helped identify best practices and potential pitfalls. For example, the TreeSitter community provided valuable feedback on my grammar files, ensuring they were accurate and efficient.
In conclusion, building TreeSitter-powered major modes for Emacs has been a rewarding experience, albeit with its share of challenges. The key takeaways include the importance of a solid understanding of the TreeSitter ecosystem, careful setup and integration, thorough testing, and leveraging community support. Whether you’re a seasoned developer or new to the world of Emacs and TreeSitter, these lessons can guide you in creating powerful and efficient major modes. As the landscape of programming languages continues to evolve, TreeSitter’s flexibility and Emacs’ extensibility make it a compelling choice for enhancing the editing experience.










