codehl 实验性
实验性
本包属于实验层:任何 0.x 版本都可能有不兼容变化(docs/release.md「API 稳定性」)。
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
func Install()Install sets a new Highlighter as ui's default (ui.SetCodeHighlighter).
func Kind
func Kind(tokenType string) ui.CodeTokenKindKind maps a Monarch token type ("keyword.go", "string.key.json", "type.identifier.ts" …) to a ui.CodeTokenKind.
func LanguageID
func LanguageID(lang string) stringLanguageID 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
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() *HighlighterNew 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) *HighlighterNewWithRegistry 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.CodeDocumentDocument implements ui.CodeHighlighter: nil for an unknown language or one without a grammar.