Sorixelle / astro-ts-mode

Emacs major mode for Astro templates
GNU General Public License v3.0
13 stars 1 forks source link
astro emacs major-mode

A major mode for editing [[https://astro.build/][Astro]] templates. Requires Emacs 29.

Installation ** MELPA Available as =astro-ts-mode=. Install with your preferred package manager, such as [[https://github.com/radian-software/straight.el][straight.el]]. Manual Clone this repo anywhere you want, probably somewhere in =~/.emacs.d= . Then, add the directory to your =load-path=.

For example:

+begin_example

cd ~/.emacs.d git clone https://github.com/Sorixelle/astro-ts-mode.git

+end_example

Then, in =init.el=:

+begin_src emacs-lisp

(add-to-list 'load-path (concat user-emacs-directory "astro-ts-mode")) (require 'astro-ts-mode)

+end_src

** Setup The first time you open a =.astro= file, or =(require 'astro-ts-mode)=, you'll likely get a warning such as the following:

: Cannot activate tree-sitter, because language grammar for astro is unavailable

Because this major mode is powered by [[https://tree-sitter.github.io/tree-sitter/][Tree-sitter]], it depends on an external grammar to provide a syntax tree for Astro templates. To set it up, you'll need to set =treesit-language-source-alist= to point to the correct repositories for each language. At a minimum, you'll need to specify =astro=, =tsx= and =css=, like so:

+begin_src emacs-lisp

(setq treesit-language-source-alist '((astro "https://github.com/virchau13/tree-sitter-astro") (css "https://github.com/tree-sitter/tree-sitter-css") (tsx "https://github.com/tree-sitter/tree-sitter-typescript" "master" "tsx/src")))

+end_src

Once that's been setup, you'll need to run =treesit-install-language-grammar= once for each language in the list. You can do this interactively (=M-x=), or by evaluating this snippet:

+begin_src emacs-lisp

(mapc #'treesit-install-language-grammar '(astro css tsx))

+end_src

Alternatively, if you're using [[https://github.com/renzmann/treesit-auto][treesit-auto]] to manage tree-sitter grammars for you, you can specify a recipe for it:

+begin_src emacs-lisp

(let ((astro-recipe (make-treesit-auto-recipe :lang 'astro :ts-mode 'astro-ts-mode :url "https://github.com/virchau13/tree-sitter-astro" :revision "master" :source-dir "src"))) (add-to-list 'treesit-auto-recipe-list astro-recipe))

+end_src

Make sure you have a working C compiler as =cc= in your PATH, since this needs to compile the grammars. ** Customization You can use these variables to customize the behaviour of astro-ts-mode: