/tree-sitter-typst

Tree Sitter grammar for Typst

Primary LanguageCMIT LicenseMIT

A TreeSitter grammar for the Typst language

Typst official page https://typst.app

TreeSitter documentation page https://tree-sitter.github.io

Typst doesn't have yet an official TreeSitter grammar. This grammar is complete but may contains bug as it is very recent.

Get involved

Your help is welcome. You don't have to know anything about tree-sitter to help. We need you to find bugs!

Find a bug by using this grammar. If you find an incorrect highlighting of your Typst files, then you found a bug. You can open an issue or simply send me by mail the Typst code causing the bug. See below installation instructions for this grammar. Even if your not sure it is a bug, it doesn't hurt to repport it.

Any inconsistancy between this syntax and Typst's one is considered a bug. In Helix editor, the syntax tree of selected text can be displayed with :tree-sitter-subtree command.

Don't hesitate to contact me: eddie.gerbais-nief@proton.me

The documentation of this implementation is available in DOC. Because parsing Typst is really tricky. It would also be great to have a competitor parser, to see if the other finds better ways, and for redunduncy, because as of today, only one person understand how this parser works (me), and if I'm out, this parser won't be maintained (I guess). If your interresting in implementing a competitor Typst tree-sitter parser, I'd be happy to help you.

TODO

  • Update 0.10 (complete grammar)
  • More tests, objectif 1000, current 409
  • Installation
    • Helix
    • Emacs
    • NeoVim (work in progress)
  • Simplification
    • Implement a type 3/4 parser generator (work in progress)
    • Migrate all lexing to external scanner
    • Remove dependency on get_column

Features

  • Markup mode
  • Code mode
  • Math mode
  • Indentation
  • Foldable sections
  • Unicode characters

Installation

Having syntax highlighting is great but having syntax highlighting and the language server is even greater. If you have cargo installed (Rust's package manager), you can install Typst language server with the following command (you don't have to be in any particular directory):

cargo install --git https://github.com/nvarner/typst-lsp typst-lsp

Neovim

The following fork adds Typst to the supported language of Neovim TreeSitter plugin:

https://github.com/Nagefire/nvim-treesitter/tree/feat/typst

It will lead to a future pull-request. At the moment, you can have a look to the diff and apply them to your config.

Emacs

Typst support for Emacs is available through the following package:

typst-ts-mode

(use-package typst-ts-mode
  :elpaca (:type git :host sourcehut :repo "meow_king/typst-ts-mode")
  :custom
  (typst-ts-mode-watch-options "--open"))

Helix

  1. Locate the configuration directory:
  • Global /usr/share/helix
  • Local ~/.config/helix
  1. Append the following configuration to the languages.toml file.
[[language]]
name = "typst"
scope = "source.typst"
file-types = ["typst", "typ"]
indent = { tab-width = 2, unit = "  " }
comment-token = "//"
injection-regex = "typ(st)?"
roots = ["typst.toml"]

[language.auto-pairs]
'(' = ')'
'{' = '}'
'[' = ']'
'$' = '$'
'"' = '"'

[[grammar]]
name = "typst"
source.git = "https://github.com/uben0/tree-sitter-typst"
source.rev = "master"
  1. Copy the content of the queries directory (highlights.scm and injections.scm) inside your corresponding runtime/queries/typst directory. You should have the following structure:
runtime
└── queries
    └── typst
        ├── highlights.scm
        └── injections.scm
  1. And finally execute (you don't have to be in any particular directory):
hx --grammar fetch
hx --grammar build

The fetch command will clone the git repository, and the build command will compile the grammar.