Log inGet started
World

Builtin Library

Branchmain
Branchesmainpublish
Commit3bb1da2647

README.md

highlight

Syntax-highlighting dispatch + per-language modules. Each language module exports (text: string) -> { Segment } where Segment = { text, color, monospace?, italic? }. Z.codeEditor calls into dispatch at update time and passes the resulting segments to TextInput, which builds the egui LayoutJob in pure Luau. Library authors can ship their own @author/zui_highlight_<lang> modules and slot them into Z.codeEditor via the same language opt.

Exports

  • M.dispatch(language: string, text: string) -> { Segment } — pick a per-language highlighter (with yml / md aliases) and tokenize; unknown languages fall through to a single plain segment.
  • M.lua: Highlighter — Lua highlighter.
  • M.json: Highlighter — JSON highlighter.
  • M.yaml: Highlighter — YAML highlighter.
  • M.wgsl: Highlighter — WGSL highlighter.
  • M.markdown: Highlighter — Markdown highlighter.

Types:

  • Segment = { text: string, color: string, monospace: boolean?, italic: boolean? }
  • Highlighter = (string) -> { Segment }

Usage

local Highlight = require("@builtin::modules.zui.highlight")

local segs = Highlight.dispatch("lua", source)
-- segs = { { text = "local", color = "#...", monospace = true }, ... }

-- Or call a specific language directly:
local segs2 = Highlight.json(jsonText)

Notes

  • Per-language colors come from the active theme's code.* tokens (ui.getToken("code.keyword") etc.). Switching themes (ui.setTheme("light")) automatically re-colors on the next call.
  • A single-slot LRU cache keyed by (language, text, theme) makes same-text re-emits free (focus-loss redraws, no-op editor pushes). Cache returns a shared array reference — callers must not mutate it.
  • Aliases: "yml" → yaml, "md" → markdown. Unknown languages return a single default-colored monospace segment so user input never crashes the dispatcher.

Roles the Lua highlighter reads

code.keyword, code.string, code.number, code.comment, code.bool (true / false / nil), code.attribute (--! directives), code.type (a name after :, ->, :: or type), code.function (a name a call is made through, or one function declares), code.property (a member reached by . that is not called), code.builtin (the standard library's globals), and code.default for the rest.