spruce/highlight

Syntax highlighting for source code using spruce styles.

Use language to resolve a canonical name or alias, language_name to inspect the result, and languages to discover all supported names.

Types

A resolved syntax language backed by a smalto grammar.

pub opaque type Language

A syntax highlighting theme for styled smalto token kinds.

Use a built-in theme as a base and replace selected fields:

highlight.Theme(
  ..highlight.dark_theme(),
  keyword: style.new() |> style.bold |> style.fg(style.Hex(0xff6b6b)),
)

The public constructor also supports defining every style from scratch.

pub type Theme {
  Theme(
    keyword: style.Style,
    string: style.Style,
    number: style.Style,
    comment: style.Style,
    function: style.Style,
    operator: style.Style,
    punctuation: style.Style,
    type_: style.Style,
    module_: style.Style,
    variable: style.Style,
    constant: style.Style,
    builtin: style.Style,
    tag: style.Style,
    attribute: style.Style,
    selector: style.Style,
    property: style.Style,
    regex: style.Style,
  )
}

Constructors

Values

pub fn adaptive_theme() -> Theme

Build the default syntax highlighting theme with adaptive light/dark colors.

pub fn dark_theme() -> Theme

Build a syntax highlighting theme for dark terminal backgrounds.

pub fn highlight(
  context: spruce.Spruce,
  code code: String,
  name name: String,
) -> String

Highlight code with the default adaptive theme, or return code unchanged for unknown languages.

pub fn highlight_named_with(
  context: spruce.Spruce,
  code code: String,
  name name: String,
  theme theme: Theme,
) -> String

Highlight code with a string language name and explicit theme.

pub fn highlight_with(
  context: spruce.Spruce,
  code: String,
  language: Language,
  theme: Theme,
) -> String

Highlight code with a resolved language and explicit theme.

pub fn language(name: String) -> Result(Language, Nil)

Resolve a language name or alias to a smalto-backed language.

pub fn language_name(language: Language) -> String

Return the canonical name for a resolved language.

pub fn languages() -> List(#(String, List(String)))

Return all supported canonical language names and their aliases.

Canonical names and aliases are in deterministic alphabetical order.

pub fn light_theme() -> Theme

Build a syntax highlighting theme for light terminal backgrounds.

✨ Search Document