Documentation
¶
Overview ¶
Package codehl implements ui.CodeHighlighter with the code editor's language support: the Monarch grammars of ui/editor/langs and the incremental tokenizer of ui/editor/highlight. Install it once to color ui.CodeBlock and the fenced code blocks of ui.Markdown:
ui.SetCodeHighlighter(codehl.New())
Token types are mapped to ui.CodeTokenKind (see Kind); the colors come from the CodeBlock component token of the current ui theme, so code follows the light, dark and high-contrast themes of every design system.
Experimental: this package's API may change in any 0.x release (see docs/release.md, "API 稳定性").
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Install ¶
func Install()
Install sets a new Highlighter as ui's default (ui.SetCodeHighlighter).
func Kind ¶
func Kind(tokenType string) ui.CodeTokenKind
Kind maps a Monarch token type ("keyword.go", "string.key.json", "type.identifier.ts" …) to a ui.CodeTokenKind.
func LanguageID ¶
LanguageID resolves lang (an id, an alias such as "golang" or "js", or a file extension with or without the dot) to a built-in language id, "" when none matches.
Types ¶
type Highlighter ¶
type Highlighter struct {
// contains filtered or unexported fields
}
Highlighter is a ui.CodeHighlighter over a Monarch registry. It keeps the most recently used documents, so asking again for the same code (a Markdown block re-rendered every frame) continues where it stopped. It is safe for concurrent use.
func New ¶
func New() *Highlighter
New returns a Highlighter over the code editor's shared registry (editor.DefaultRegistry), so grammars compiled for editors are reused.
func NewWithRegistry ¶
func NewWithRegistry(reg *monarch.Registry) *Highlighter
NewWithRegistry returns a Highlighter over reg (grammars added to reg with monarch.Registry.Add are available by their id).
func (*Highlighter) Document ¶
func (h *Highlighter) Document(lang, code string) ui.CodeDocument
Document implements ui.CodeHighlighter: nil for an unknown language or one without a grammar.