Skip to content

theme 实验性 ​

实验性

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

go
import "github.com/tangoui/tango/ui/editor/theme"

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

Package theme parses Monaco-format editor themes (IStandaloneThemeData: {base, inherit, rules, colors}) and resolves Monarch token types such as "keyword.go" or "string.escape.ts" to display styles.

Token resolution follows Monaco's TokenTheme: rules form a trie keyed by dot-separated segments and a token takes the style of the deepest matching node. A node starts as a copy of its parent's style when it is created and each rule only overwrites the fields it sets, so a rule that omits foreground, background or fontStyle inherits them from its nearest ancestor rule, while fontStyle "" explicitly resets to plain.

Parsing is lenient about individual values, unlike Monaco in places: an invalid color in the colors table (Monaco shows it as red) or in a rule (Monaco throws and the theme fails to apply) is ignored and recorded in Theme.Warnings; non-string rule fields are ignored field by field (as Monaco's typeof checks do) and non-object rules are skipped. Only malformed JSON or a missing/unknown base fails Parse.

The structure follows Monaco's TokenTheme (vs/editor/common/languages/ supports/tokenization.ts) and StandaloneTheme (vs/editor/standalone/browser/ standaloneThemeService.ts), Copyright (c) Microsoft Corporation, MIT License. See NOTICE in this directory.

Experimental: this package's API may change in any 0.x release (see docs/release.md, "API 稳定性").

类型 ​

Style ​

go
type Style struct {
	Foreground, Background                 color.NRGBA
	Bold, Italic, Underline, Strikethrough bool
}

Style is the resolved display style of a token type.

Theme ​

go
type Theme struct {
	// contains filtered or unexported fields
}

Theme is a parsed, immutable editor theme. It is safe for concurrent use.

func Base ​

go
func Base(name string) *Theme

Base returns Monaco's built-in base theme "vs", "vs-dark", "hc-black" or "hc-light", or nil for any other name. The result is shared.

func Parse ​

go
func Parse(data []byte) (*Theme, error)

Parse parses a Monaco theme. "base" must be one of the four base themes; when "inherit" is true the base theme's rules are applied first and its colors fill in keys the theme does not define. Colors are hex RGB, RGBA, RRGGBB or RRGGBBAA with an optional leading '#'; token rule colors are opaque, so any alpha digits in them are ignored, as in Monaco. Invalid individual values are ignored and reported by Warnings (see the package doc).

func (*Theme) BaseName ​

go
func (t *Theme) BaseName() string

BaseName returns the theme's base: "vs", "vs-dark", "hc-black" or "hc-light" (the theme type that selects default colors).

func (*Theme) Color ​

go
func (t *Theme) Color(key string) (color.NRGBA, bool)

Color returns the theme color for a workbench color key such as "editor.lineHighlightBackground": the theme's own value (with inherit, its base theme's values for keys it does not set), else Monaco 0.56's registered default for the theme's base type, resolved against the theme's colors (colors.go). It reports false for a key without a color: one Monaco has no default for there (editor.lineHighlightBackground), or that is not a registered color id.

func (*Theme) OwnColor ​

go
func (t *Theme) OwnColor(key string) (color.NRGBA, bool)

OwnColor returns the theme's own value of key (with inherit, including its base theme's colors), without the registry defaults: Monaco's theme.defines(key) is whether it reports true.

func (*Theme) Style ​

go
func (t *Theme) Style(tokenType string) Style

Style returns the style for a Monarch token type. Results are cached per token type; Style is safe to call from multiple goroutines.

func (*Theme) Warnings ​

go
func (t *Theme) Warnings() []string

Warnings returns the problems Parse ignored: invalid color values, non-string rule fields and non-object rules. It returns a new slice on each call.

以 MIT 许可发布