codehl

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func LanguageID(lang string) string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL