Skip to content

uben0/tree-sitter-typst

Repository files navigation

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.11
    • Hangul script doesn't form words
    • Context operator
    • No line comment inside block comment
  • More tests, objectif 1000, current 419
  • Installation
    • Helix
    • Emacs
    • NeoVim
  • 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 https://github.com/nvim-treesitter/nvim-treesitter provides this grammar.

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

The next release of Helix will support Typst. Waiting until then, you can add support for Typst with the following instructions:

  1. Locate the configuration directory:
  • Global /usr/share/helix
  • Local ~/.config/helix
  1. Append the following configuration to the languages.toml file (create it if it doesn't exist).
[language-server.typst-lsp]
command = "typst-lsp"

[[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-servers = [ "typst-lsp" ]

[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:
config.toml
languages.toml
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.