micro icon indicating copy to clipboard operation
micro copied to clipboard

Typst syntax

Open ghost opened this issue 2 years ago • 7 comments

Description of the problem or steps to reproduce

I think it would be help to add Typst syntax:

https://github.com/zyedidia/micro/tree/master/runtime/syntax

maybe not the full language, but at least the basics:

https://typst.app/docs/reference/syntax https://github.com/typst/typst

ghost avatar Mar 22 '23 23:03 ghost

Big +1 on adding typst syntax highlighting. To me, typst feels like a word and latex killer, and is rapidly growing and developing. Support would be lovely!

KronosTheLate avatar Sep 14 '23 08:09 KronosTheLate

I'm hoping to implement just such a thing. It doesn't seem like we can have our own "plug in" syntax highlighting files (the way we can with e.g. color schemes), so I presumably will have to create a pull request to have it merged into the main codebase, unfortunately.

dei-layborer avatar Nov 15 '23 18:11 dei-layborer

It doesn't seem like we can have our own "plug in" syntax highlighting files (the way we can with e.g. color schemes)

I think we actually can. You can put your own syntax *.yaml or *.hdr files to ~/.config/micro/syntax directory.

Also if you want to do it via a plugin, you should be able to do it via something like this in your plugin:

config.AddRuntimeFile("typst", config.RTSyntax, "typst.yaml")

That said, although the above should work, why not add it to the main micro codebase instead? FWIW it's the first time I hear about typst, but if it's useful, why not have its syntax support available out of the box?

dmaluka avatar Nov 17 '23 10:11 dmaluka

I tried to create a syntax for typst but micro's syntax highlighting system can't really handle it. The problem is that there are three different modes (code, math, markup) with different syntax rules and you can switch between them at any time. For example it's not uncommon to have code inside markup inside code inside markup. Here's a small (but tricky) test case in case anyone wants to try to implement this in a halfway sensible way:

#set page("us-letter")
= Heading
let's go to code mode so we can set x to none
#{
    let x = none
    let y = 1 + 2
    set text(20pt)
}

// but it's not always so easy to see where code mode begins and ends:
#let z = (
    "first", // this is all in code mode!
    none,
    [
        this is in markup mode again, so there are no
        "strings" and let is not a keyword -- but we can
        go even deeper, here's a math mode equation that
        switches to code mode to calculate 1 + 2:
        $1 + 2 = #(1 + 2)$
    ],
)
this is in markup mode again
#z.at(2)

Andriamanitra avatar Aug 18 '24 09:08 Andriamanitra

This would be a killer feature for me. I don't necessarily need live preview like some other editors have, but decent syntax highlighting would make it a lot easier to work with typst source.

micro has been my preferred text editor for some time, but not having typst syntax makes homework and publications a lot harder to work with at a glance.

infinitymdm avatar Aug 28 '24 14:08 infinitymdm

@infinitymdm here's my attempt, it at least highlights something but is often very wrong for more complicated documents because it doesn't understand toggling between the different modes (for example literals and keywords from code mode are highlighted even in markup mode). My typst documents tend to use a lot of code mode so I focused on that and omitted most of the markup mode syntax that would get highlighted wrong when in code mode. You can probably modify it slightly to better fit whatever kinds of documents you're writing.

~/.config/micro/syntax/typst.yaml

filetype: typst

detect:
    filename: "\\.typ"

rules:
    - underlined: "^\\s*=+ .*$"
    - statement: "#[A-Za-z_0-9\\.]+"
    - identifier: "\\B@[A-Za-z_0-9]+\\b"
    - identifier: "<[A-Za-z_0-9]+>"
    - special: "https?://\\S+"
    - statement: "\\b(let|set|show|context|if|else|for|in|while|break|continue|return|include|import)\\b"
    - symbol.brackets: "[\\[\\]\\{\\}\\(\\)]"
    - constant: "\\b(none|auto)\\b"
    - constant.bool.true: "\\btrue\\b"
    - constant.bool.false: "\\bfalse\\b"
    - constant.number: "\\b[0-9](_?[0-9])*(\\.([0-9](_?[0-9])*)?)?(e[0-9](_?[0-9])*)?\\b"
    - constant.number: "\\b0x(_?[0-9a-fA-F])+\\b"

    # math mode
    - special:
        start: "\\$"
        end: "\\$"
        rules: []

    # raw text
    - constant.string:
        start: "`"
        end: "`"
        rules: []

    - constant.string:
        start: "'"
        end: "'"
        skip: "\\\\."
        rules: []
    - constant.string:
        start: "\""
        end: "\""
        skip: "\\\\."
        rules: []

    - comment:
        start: "//"
        end: "$"
        rules: []
    - comment:
        start: "/\\*"
        end: "\\*/"
        rules: []

Andriamanitra avatar Aug 29 '24 01:08 Andriamanitra

Can there be a “best effort” mode that just highlights things starting with # and symbols that are immediately followed by an opening (?

I mean you can’t parse this without a general purpose recursive parser, but syntax highlighting in micro does not have to be perfect, just good enough imo.

Profpatsch avatar Sep 17 '25 12:09 Profpatsch