codemirror-lang

    Portable MoonBit syntax highlighting with per-line UTF-16 spans

    codemirror
    syntax-highlighting
    Download zip
    Author
    Version
    0.1.1
    License
    MIT
    Last updated
    5 days ago
    Downloads
    332

    #CodeMirror language modes for MoonBit

    This module highlights source in 121 language modes on JavaScript, Native, and Wasm-GC. To select a mode by name, import the root package in moon.pkg.

    import {
    "bobzhang/codemirror-lang" @highlight,
    }

    let lines = @highlight.tokenize("Go", "package main\r\n").unwrap()
    let first = lines[0][0] // offsets 0..7, style Some("keyword")

    Use any directory name under modes/ or one of the language aliases in the lookup table. For example, ts selects TypeScript, erb selects HTML with Ruby, and rpm-changes selects the RPM changelog format. Names are ASCII case-insensitive. An unknown name returns None. Each line has an array of spans with a style and half-open UTF-16 offsets. A trailing line break produces a final empty array.

    For a specific mode, import its package directly. For example, import bobzhang/codemirror-lang/modes/diff for unified diffs.

    let lines = @diff.tokenize("+hello\r\n")
    let first = lines[0][0] // offsets 0..6, style Some("positive")

    The Go mode provides incremental highlighting and indentation through bobzhang/codemirror-lang/modes/go. Its tokenize_spans function returns the same span type as the diff mode.

    #Enum constructors in existing editor packages

    A re-exported enum keeps its type identity. When using an existing editor package, include the enum name in expressions and patterns. For example, change @javascript.TypeScript to @javascript.Dialect::TypeScript. Values can be passed directly to the corresponding language-library API. The same rule applies to @css.KeywordGroup.

    tokenize

    fn tokenize(language : String, source : String) -> Array[Array[
    Span
    ]]?

    Highlight well-formed UTF-16 source by portable package name or language alias. Names are ASCII case-insensitive. Unknown names return None. Each line has its own array of half-open UTF-16 spans, including an empty array for the final line after a line break. Modes use their standalone defaults.

    test {
    let lines = @highlight.tokenize("Go", "package main\r\n").unwrap()
    assert_eq(lines[0][0], { start: 0, end: 7, style: Some("keyword"), })
    assert_eq(lines[1], [])
    }

    Source Files