Skip to content

codehl 实验性 ​

实验性

本包属于实验层:任何 0.x 版本都可能有不兼容变化(docs/release.md「API 稳定性」)。

go
import "github.com/tangoui/tango/ui/codehl"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

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 稳定性").

函数 ​

func Install ​

go
func Install()

Install sets a new Highlighter as ui's default (ui.SetCodeHighlighter).

func Kind ​

go
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 ​

go
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.

类型 ​

Highlighter ​

go
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 ​

go
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 ​

go
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 ​

go
func (h *Highlighter) Document(lang, code string) ui.CodeDocument

Document implements ui.CodeHighlighter: nil for an unknown language or one without a grammar.

以 MIT 许可发布