Skip to content

syntax: add Typst - #4243

Open
Londopy wants to merge 3 commits into
micro-editor:masterfrom
Londopy:syntax-typst
Open

Londopy wants to merge 3 commits into
micro-editor:masterfrom
Londopy:syntax-typst

Conversation

@Londopy

@Londopy Londopy commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Adds typst.yaml for .typ files, following the subset @Andriamanitra outlined in #4064 (thanks to @TinCan2 for that PR). Typst switches between markup, code and math mode at any point, which micro's rules can't track, so this only highlights what looks the same in every mode:

  • // and /* */ comments
  • double-quoted strings
  • raw text in backticks, including fenced blocks
  • headings
  • escapes, including \u{...}
  • math ($...$) and the name that starts a code expression (#set, #emph, #z.at)

No keywords and no single-quoted strings. The heading and math groups follow the attempt in #2780.

Three things needed care:

  • Escapes are a region, so \$ and \# don't start math or a code expression (micro picks the region that starts first, and the escape starts at the backslash). That's what makes math and #... safe to include. It also ends at the end of the line, since a trailing \ is a line break.
  • // after : isn't a comment, so URLs aren't commented out. Same idiom tex.yaml uses for \%.
  • Headings need = in column 0. Rules also run on the rest of a line after inline math or raw text, so $x$ = 5 would otherwise be highlighted as a heading.

Known limitations: indented headings aren't highlighted, text after inline math in a heading loses the heading color, and nested block comments end at the first */.

Tested with the example from #2780 (nothing in it is highlighted incorrectly) and a file of edge cases, through both HighlightString and the editor's incremental path, with and without #4022, and in the built editor. Editing lines that open or close math, raw blocks, strings and comments re-highlights the same as highlighting the file fresh.

Closes #2780

@Andriamanitra Andriamanitra left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good approach and I didn't notice any issues with the syntax. It only highlights the basics but it still helps, and having it allows filetype-specific configuration -- speaking of which we should add typst to the built-in comment plugin as well.

Comment thread runtime/syntax/typst.yaml Outdated
Comment on lines +12 to +14
# headings in markup mode. No leading whitespace is allowed: rules are
# also matched against the rest of a line after inline math, raw text
# etc., so "$x$ = 5" would otherwise look like a heading.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex explains itself better than this comment does.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, shortened it in bd5fdfa.

@Londopy

Londopy commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! Added typst (// %s) to the comment plugin and its help list in fced4b2. Without it the plugin fell back to # %s, which starts a code expression in typst.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Typst syntax

2 participants