when 实验性
实验性
本包属于实验层:任何 0.x 版本都可能有不兼容变化(docs/release.md「API 稳定性」)。
import "github.com/tangoui/tango/ui/editor/when"本页由
site/tools/apigen从源码注释生成(按 darwin 构建标签选文件),请勿手改。
Package when parses and evaluates VS Code "when" clause expressions, the conditions keybindings and commands use to decide whether they apply ("editorTextFocus && !editorReadonly").
Grammar (VS Code's, lowest precedence first):
expr = and { "||" and }
and = unary { "&&" unary }
unary = "!" term | primary
term = key | "true" | "false" | "(" expr ")"
primary = term
| key op value
| key "=~" /regexp/flags
| key [ "not" ] "in" key
op = "==" | "!=" | "===" | "!==" | "<" | "<=" | ">" | ">="
As in VS Code's parser (_term), "!" applies only to a key, true, false or a parenthesized expression: "!a == 'b'" and "!!a" are errors (write "a != 'b'", "!(a == 'b')", "a").
Keys and unquoted values are runs of characters other than blanks and ( ) ! = < > & | ' ". Values are single-quoted strings ('a b'), double-quoted strings ("a b", MinCode's plugin dialect) or bare words.
Semantics follow VS Code (Monaco's contextkey):
- A bare key is true when its value is truthy (not missing, nil, false, 0, NaN or ""); an undefined key is false.
- Only the bare words true and false are rewritten: "key == true" means the key is truthy, "key == false" that it is not ("!=" the opposite). A quoted 'true' or "true" is a string and compares as one, so it never equals a boolean value.
- == and != compare a string value with the literal; a numeric value is equal when the literal parses as the same number (JavaScript's loose 3 == '3'); booleans and nil equal no literal.
- < <= > >= need a numeric literal (else the comparison is false); the value is a Go number, or a string that strconv.ParseFloat accepts in full; anything else is false.
- =~ tests the key's value (a string, or other values formatted with fmt.Sprint) with a JavaScript regular expression compiled through package editor/regex: a search, not anchored at the start; flags i, s, m, u, g, y.
- "a in b" is true when b's value is a slice or array containing a's value, or a map with a's string value as a key; "not in" negates it.
Differences from VS Code:
- Double-quoted strings are accepted as literals (MinCode's dialect); VS Code's scanner would take the quotes as part of a bare word.
- Expressions longer than MaxLength (512, MinCode's limit) are refused.
- < <= > >= parse strings strictly ("12px" is not 12); JavaScript's parseFloat would read a numeric prefix.
- =~ on an undefined key is false (JavaScript tests the string "undefined"); the regular expression engines differ in corner cases (see package editor/regex).
- "in" works on Go slices, arrays and maps with string keys in place of JavaScript arrays and objects.
- Monaco's normalizations (constant folding, distributing && over ||) are not applied; Implies compares flattened, order-independent && / || terms instead.
Experimental: this package's API may change in any 0.x release (see docs/release.md, "API 稳定性").
常量
const MaxLength = 512MaxLength is the longest expression Parse accepts (MinCode's WHEN_EXPRESSION_MAX_LENGTH).
函数
func Eval
func Eval(expr string, ctx Context) boolEval parses and evaluates expr: a blank expression is true, a malformed one false (a bad when hides the command, as MinCode's evaluateWhen).
func Implies
func Implies(p, q Expr) boolImplies reports whether p provably implies q, Monaco's implies (used by keybinding removal rules, KeybindingResolver.whenIsEntirelyIncluded): a nil q (no when, always true) is implied by everything; a nil p implies only a nil q. Expressions are compared after flattening parentheses and nested && / || and ignoring term order; each term (a key, a comparison, a negation) compares by its String. Monaco's further normalizations (constant folding, distributing && over ||) are not applied.
Implies("a && b", "a") true
Implies("b && a", "(a && b)") true
Implies("a", "a && b") false
Implies("a", "a || b") true
Implies("a || b", "a") false
类型
Context
type Context interface {
Value(key string) (any, bool)
}Context supplies context key values.
ContextFunc
type ContextFunc func(key string) (any, bool)ContextFunc adapts a function to Context.
func (ContextFunc) Value
func (f ContextFunc) Value(key string) (any, bool)Value implements Context.
Expr
type Expr interface {
// Eval evaluates the expression; a nil ctx has no keys.
Eval(ctx Context) bool
// String returns the expression in a canonical form: parsing it again
// gives an equal expression (the same String).
String() string
}Expr is a parsed expression.
func Parse
func Parse(expr string) (Expr, error)Parse parses expr. An empty or blank expression is an error (callers treat an absent when as "always"; see Eval).
MapContext
type MapContext map[string]anyMapContext is a Context backed by a map.
func (MapContext) Value
func (m MapContext) Value(key string) (any, bool)Value implements Context.