when

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

Index

Constants

View Source
const MaxLength = 512

MaxLength is the longest expression Parse accepts (MinCode's WHEN_EXPRESSION_MAX_LENGTH).

Variables

This section is empty.

Functions

func Eval

func Eval(expr string, ctx Context) bool

Eval 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) bool

Implies 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

Types

type Context

type Context interface {
	Value(key string) (any, bool)
}

Context supplies context key values.

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

type 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).

type MapContext

type MapContext map[string]any

MapContext is a Context backed by a map.

func (MapContext) Value

func (m MapContext) Value(key string) (any, bool)

Value implements Context.

Jump to

Keyboard shortcuts

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