highlighter

Themes

A Theme maps each of the nine highlight categories to a colour. Themes are used by InlineMode to emit inline style colours, and you can read a theme yourself when…

A Theme maps each of the nine highlight categories to a colour. Themes are used by InlineMode to emit inline style colours, and you can read a theme yourself when rendering tokens.

Built-in themes

Three presets ship with the library:

InlineMode(Theme.OneDark)   // dark
InlineMode(Theme.OneLight)  // light
InlineMode(Theme.Monokai)   // dark

The same Scala snippet under each — and every one of these blocks was produced by highlighter itself, in inline mode:

OneDark

// quicksort
def sort(xs: List[Int]): List[Int] = xs match
  case Nil => Nil
  case pivot :: rest =>
    val (less, more) = rest.partition(_ < pivot)
    sort(less) ++ (pivot :: sort(more))

val nums = List(5, 2, 8, 1, 9)
println(sort(nums))  // List(1, 2, 5, 8, 9)

OneLight

// quicksort
def sort(xs: List[Int]): List[Int] = xs match
  case Nil => Nil
  case pivot :: rest =>
    val (less, more) = rest.partition(_ < pivot)
    sort(less) ++ (pivot :: sort(more))

val nums = List(5, 2, 8, 1, 9)
println(sort(nums))  // List(1, 2, 5, 8, 9)

Monokai

// quicksort
def sort(xs: List[Int]): List[Int] = xs match
  case Nil => Nil
  case pivot :: rest =>
    val (less, more) = rest.partition(_ < pivot)
    sort(less) ++ (pivot :: sort(more))

val nums = List(5, 2, 8, 1, 9)
println(sort(nums))  // List(1, 2, 5, 8, 9)

Custom themes

Theme is a plain case class with a default colour per category, so override only what you want:

val mine = Theme(
  keyword     = "#ff0000",
  string      = "#00aa00",
  comment     = "#888888",
  number      = "#d19a66",
  `type`      = "#e5c07b",
  function    = "#61afef",
  variable    = "#e06c75",
  operator    = "#56b6c2",
  punctuation = "#abb2bf",
  default     = "#abb2bf",   // anything with no category of its own
)

val Right(hl) = Highlighter.fromJson(grammar, InlineMode(mine)): @unchecked

keyword covers control words and storage modifiers; default is used for text that carries a scope highlighter doesn’t map to a category. (type is a Scala keyword, so it is written `type` in source.)

Search

Esc
to navigate to open Esc to close