Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
# Typst Extension for Zed

## Usage
To register the LSP and enable certain features such as compile on save, add the
following your `settings.json` (or `.zed/settings.json` in the project root for
project-specific config),

```json
"lsp": {
"tinymist": {
"settings": {
"exportPdf": "onSave",
"outputPath": "$root/$name"
}
}

To register the LSP and enable certain features (such as compile on save), add the following to your local (resp. server) configuration file (`~/.zed/settings.json`):

```jsonc
// In settings.json
{
"lsp": {
"tinymist": {
"initialization_options": {
// Enable background preview
// Server will be running on 127.0.0.1:23635
"preview": {
"background": {
"enabled": true,
},
},
},

"settings": {
// Compile on save
// This will compile a PDF for the `main.typ` file in the project root.
"exportPdf": "onSave",
"outputPath": "$root/$name"

// Enable formatter
"formatterMode": "typstyle",
}
}
},

"languages": {
"Typst": {
// Disable soft wrap
"soft_wrap": "none",
},
},
}
```

This will compile a PDF for the `main.typ` file in the project root.

To see all available options refer to [the tinymist documentation](https://github.com/Myriad-Dreamin/tinymist/blob/main/editors/neovim/Configuration.md).
Beware that the configuration options displayed there apply to **NeoVim**, not Zed, so some might be incorrect or misleading

## Components

- Tree Sitter: [tree-sitter-typst](https://github.com/uben0/tree-sitter-typst/)
- Language Server: [tinymist](https://github.com/Myriad-Dreamin/tinymist/)
14 changes: 12 additions & 2 deletions languages/typst/brackets.scm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@
"}" @close)

("\"" @open
"\"" @close)
"\"" @close
(#set! rainbow.exclude))

("`" @open
"`" @close
(#set! rainbow.exclude))

("```" @open
"```" @close
(#set! rainbow.exclude))

("$" @open
"$" @close)
"$" @close
(#set! rainbow.exclude))
19 changes: 9 additions & 10 deletions languages/typst/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ name = "Typst"
grammar = "typst"
path_suffixes = ["typ", "typst"]
line_comments = ["// "]
documentation_comment = { start = "/*", prefix = "* ", end = "*/", tab_size = 1 }
tab_size = 2
soft_wrap = "editor_width"
brackets = [
{ start = "/*", end = " */", close = true, newline = true, not_in = ["string", "comment"] },
{ start = "(", end = ")", close = true, newline = true },
{ start = "{", end = "}", close = true, newline = true },
{ start = "[", end = "]", close = true, newline = true },
{ start = "$", end = "$", close = true, newline = true },
{ start = "\"", end = "\"", close = true, newline = false, not_in = [
"string",
] },
{ start = "*", end = "*", close = false, newline = false, surround = true, not_in = [
"math"
] },
{ start = "_", end = "_", close = false, newline = false, surround = true, not_in = [
"math"
] },
{ start = "`", end = "`", close = false, newline = false, surround = true },
{ start = "`", end = "`", close = true, newline = false, surround = true },
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["string"] },
{ start = "*", end = "*", close = true, newline = false, surround = true, not_in = ["string", "math"] },
{ start = "_", end = "_", close = true, newline = false, surround = true, not_in = ["string", "math"] },
]
unordered_list = ["- ", "+ "]
ordered_list = [{ pattern = "(\\d+)\\. ", format = "{1}. " }]
120 changes: 64 additions & 56 deletions languages/typst/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
; Taken from https://github.com/uben0/tree-sitter-typst/blob/f457c77edffd4b93190794355ff5acf7acfb99c6/editors/helix/queries/highlights.scm#L4
; Improved by @Gaspartcho
; CONTROL
(let
"let" @keyword.storage.type)

(branch
[
"if"
Expand All @@ -19,9 +16,21 @@
"in"
] @keyword.control.repeat)

(flow
[
"break"
"continue"
] @keyword.control)

(return
"return" @keyword.control)

; DIRECTIVES
(import
"import" @keyword.control.import)

(wildcard) @operator

(as
"as" @keyword.operator)

Expand All @@ -34,14 +43,8 @@
(set
"set" @keyword.control)

(return
"return" @keyword.control)

(flow
[
"break"
"continue"
] @keyword.control)
(let
"let" @keyword.storage.type)

; OPERATOR
(in
Expand Down Expand Up @@ -90,77 +93,78 @@
">"
] @operator)

(fraction
"/" @operator)
; VALUE
(ident) @variable

(fac
"!" @operator)
(tagged
field: (ident) @variable.parameter)

(attach
[
"^"
"_"
] @operator)
(field
field: (_) @property)

(wildcard) @operator
(call
item: (ident) @function)

; VALUE
(ident) @variable
(call
item: (field
field: (ident) @function.method))

; RAW
(raw_blck
"```" @punctuation.delimiter
"```" @punctuation.delimiter @embedded
(blob) @text.literal)

(raw_blck
lang: (ident) @tag)
lang: (ident) @tag @embedded)

(raw_span
"`" @punctuation.delimiter
(blob) @text.literal)

(label) @tag

(ref) @tag
; MATH
[
(label)
(ref)
] @tag @label

(number) @number

(string) @string

(content
[
"["
"]"
] @operator)

(bool) @boolean

(none) @constant.builtin

(auto) @constant.builtin

; Functions
(formula
(ident) @function.method)
(ident) @constant)

(formula
(field
(ident) @constant))

(attach
(ident) @function.method)
(ident) @constant)

(formula
(attach
(field
(ident) @function.method))
(ident) @constant))

(tagged
field: (ident) @tag)
(fraction
(ident) @constant)

(field
field: (ident) @tag)
(attach
[
"^"
"_"
] @operator)

(call
item: (ident) @function)
(fraction
"/" @operator)

(call
item: (field
field: (ident) @function.method))
(fac
"!" @operator)

; MARKUP
(item
Expand All @@ -174,36 +178,40 @@

(heading) @title

(url) @tag
(url) @tag @link_uri

(emph) @emphasis

(strong) @emphasis.strong

(symbol) @operator

(shorthand) @constant.builtin
(shorthand) @constant.builtin @operator

(quote) @markup.quote

(align) @operator

(linebreak) @constant.builtin
(code
"#" @punctuation.special)

(math
"$" @operator)
"$" @punctuation.special)

"#" @operator
[
(align)
(linebreak)
] @punctuation.special

"end" @operator

(escape) @constant.character.escape
(escape) @string.escape

[
"("
")"
"{"
"}"
"["
"]"
] @punctuation.bracket

[
Expand Down
3 changes: 3 additions & 0 deletions languages/typst/indents.scm
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
(_
"$"
"$" @end) @indent

((comment) @indent
(#match? @indent "^/\\*"))
16 changes: 6 additions & 10 deletions languages/typst/outline.scm
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
(heading
[
"="
"=="
"==="
"===="
"====="
"======"
] @context
(text) @name) @item
(section
(heading
.
_ @context
.
(text) @name)) @item
2 changes: 2 additions & 0 deletions languages/typst/overrides.scm
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
(string) @string

(math) @math

(comment) @comment.inclusive
12 changes: 10 additions & 2 deletions languages/typst/tasks.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
[
{
"label": "Typst: export file",
"label": "Typst: Export File",
"command": "typst",
"args": ["compile", "\"$ZED_FILE\""],
"hide": "on_success"
},
{
"label": "Typst: watch file",
"label": "Typst: Watch File",
"command": "typst",
"args": ["watch", "\"$ZED_FILE\""],
"reveal": "never",
"hide": "always"
},
{
"label": "Typst: Open Preview",
"command": "xdg-open",
"args": ["http://127.0.0.1:23635/"],
"reveal": "never",
"hide": "always"
}
]
Loading