Documentation
¶
Overview ¶
Package command defines the command registry types for the editor
Index ¶
- Constants
- Variables
- func SplitCommandLine(input string) (string, string, bool)
- type Action
- type Args
- func (a *Args) CompletionState() CompletionState
- func (a *Args) Empty() bool
- func (a *Args) Finish() error
- func (a *Args) First() (string, bool)
- func (a *Args) Flag(name string) (string, bool)
- func (a *Args) Get(i int) (string, bool)
- func (a *Args) HasFlag(name string) bool
- func (a *Args) Join(sep string) string
- func (a *Args) Len() int
- func (a *Args) Positionals() []string
- func (a *Args) Push(arg string) error
- type Command
- type Completer
- type Completion
- type CompletionFunc
- type CompletionState
- type CompletionStateKind
- type Continuation
- type ExpansionKind
- type Flag
- type KeyAction
- type KeyBinding
- type KeyCode
- type KeyEvent
- type KeyHint
- type KeyModifiers
- type KeyResultAction
- type Keymaps
- func (k *Keymaps) Bind(mode view.Mode, name string, seqs ...[]KeyEvent)
- func (k *Keymaps) BindResultAction(modes []view.Mode, action KeyResultAction, label string, seqs ...[]KeyEvent) error
- func (k *Keymaps) Bindings(mode view.Mode, name string) KeyBinding
- func (k *Keymaps) CommandsIn(mode view.Mode) []*Command
- func (k *Keymaps) LabelNode(mode view.Mode, prefix KeyBinding, name string)
- func (k *Keymaps) Lookup(mode view.Mode, seq []KeyEvent) (LookupRes, bool)
- func (k *Keymaps) PendingHints(mode view.Mode, seq []KeyEvent) (string, []KeyHint)
- func (k *Keymaps) Register(name string, cmd Command) error
- func (k *Keymaps) ResolveCommand(name string) *Command
- func (k *Keymaps) ResolveCommandIn(mode view.Mode, name string) *Command
- type LookupRes
- type Module
- type Option
- type OptionGetter
- type OptionKeyGetter
- type OptionKeySetter
- type OptionSetter
- type ParseError
- type ParseErrorKind
- type Positionals
- type PrefixLabel
- type Quote
- type Registry
- func (r *Registry) ApplyOptionValues(e *view.Editor, values map[string]string) error
- func (r *Registry) ApplyTOML(e *view.Editor, raw map[string]any) error
- func (r *Registry) BoolOptionCompleter() CompletionFunc
- func (r *Registry) BoolOptionKeys() []string
- func (r *Registry) LookupOption(key string) *Option
- func (r *Registry) OptionCompleter() CompletionFunc
- func (r *Registry) OptionKeys() []string
- func (r *Registry) OptionValueCompleter() CompletionFunc
- func (r *Registry) OptionValues(e *view.Editor) (map[string]string, error)
- func (r *Registry) RegisterCommand(name string, c Command) error
- func (r *Registry) RegisterModule(m Module) error
- type Result
- type Run
- type Section
- type Signal
- type Signature
- type Special
- type SyntaxError
- type SyntaxErrorKind
- type Token
- type TokenExpander
- type TokenKind
- type Tokenizer
Constants ¶
const ( CompletionStateFlag = CompletionStateKind(argsCompletionFlag) CompletionStateFlagArgument = CompletionStateKind( argsCompletionFlagArgument, ) )
const ( // AllModes is every editing and pane mode AllModes = view.ModeNormal | view.ModeSelect | view.ModeInsert | view.ModeTerminal | view.ModeImage | view.ModeBinary // DocNormalModes is the non-insert document modes DocNormalModes = view.ModeNormal | view.ModeSelect // DocModes is every mode backed by an editable document view DocModes = view.ModeNormal | view.ModeSelect | view.ModeInsert // PaneModes is every mode for commands that apply to every pane kind PaneModes = view.ModeNormal | view.ModeSelect | view.ModeTerminal | view.ModeImage | view.ModeBinary // CmdKeyModes is every pane mode except terminal, where keystrokes // belong to the shell CmdKeyModes = view.ModeNormal | view.ModeSelect | view.ModeImage | view.ModeBinary )
Variables ¶
var ( ErrDuplicateCommand = errors.New("duplicate command registration") ErrNoModes = errors.New("command has no modes") ErrUnknownMode = errors.New("keys references mode not in modes") ErrBindingExists = i18n.NewError(i18n.ErrorBindingExists) )
var ( // ErrCommandLineParse is the sentinel error for command-line parse failures ErrCommandLineParse = errors.New("command line parse error") )
var ( // ErrInvalidKey reports malformed keycap notation ErrInvalidKey = i18n.NewError(i18n.ErrorInvalidKey) )
Functions ¶
Types ¶
type Args ¶
type Args struct {
// contains filtered or unexported fields
}
Args is command-line input interpreted by a command signature
func ParseArgs ¶
ParseArgs parses command input using the supplied signature; expand may be nil, in which case raw token content is used as-is
func (*Args) CompletionState ¶
func (a *Args) CompletionState() CompletionState
CompletionState returns what kind of argument the last token was
func (*Args) Positionals ¶
Positionals returns a copy of the positional arguments
type Command ¶
type Command struct {
Name string
Run Run
DocString string
Modes view.Mode
Keys map[view.Mode]KeyBinding
Aliases []string
Signature Signature
}
Command describes one registered command: its runner, key bindings, mode applicability, typeable aliases, and argument signature
type Completer ¶
type Completer struct {
Positionals []CompletionFunc
Raw CompletionFunc
}
Completer describes positional and raw argument completion
func PositionalCompleter ¶
func PositionalCompleter(c ...CompletionFunc) Completer
PositionalCompleter completes positionals by argument index
type Completion ¶
Completion replaces prompt input from Start to the end of the line
type CompletionFunc ¶
type CompletionFunc func(*view.Editor, *Args, string) []Completion
CompletionFunc returns completions for command-line input, given the arguments already parsed for the current command
func StaticCompleter ¶
func StaticCompleter[T ~string](items ...T) CompletionFunc
StaticCompleter completes from a fixed string set
type CompletionState ¶
type CompletionState struct {
Kind CompletionStateKind
Flag *Flag
}
CompletionState describes what kind of argument is being typed
type CompletionStateKind ¶
type CompletionStateKind int
CompletionStateKind identifies the kind of the last parsed argument
type Continuation ¶
type Continuation func(*view.Editor, KeyEvent) Continuation
Continuation is called with subsequent keys while an action is in progress. Returns nil to signal completion, or another Continuation to consume more keys
type ExpansionKind ¶
type ExpansionKind int
ExpansionKind identifies a percent-token expansion kind
const ( ExpansionVariable ExpansionKind = iota ExpansionUnicode ExpansionShell ExpansionRegister )
type KeyAction ¶
type KeyAction func(*view.Editor) Continuation
KeyAction handles a key sequence and may return a continuation
type KeyBinding ¶
type KeyBinding [][]KeyEvent
KeyBinding describes default key sequences for a command
type KeyCode ¶
type KeyCode struct {
// Char holds the rune for printable characters; 0 for special keys
Char rune
// Special names the key when Char is 0
Special Special
}
KeyCode represents a single keyboard key
type KeyEvent ¶
type KeyEvent struct {
Code KeyCode
Mods KeyModifiers
}
KeyEvent is a key code combined with modifier state
func ParseKeySequence ¶ added in v0.1.28
ParseKeySequence parses a space-separated sequence of keycap names
func (KeyEvent) IsTypable ¶
IsTypable reports whether k is a printable character that should be accepted as literal text input — Char is set and neither Ctrl nor Alt is held; ModShift alone is fine; it is already reflected in the Char value
func (KeyEvent) WithMods ¶
func (k KeyEvent) WithMods(m KeyModifiers) KeyEvent
WithMods returns a copy of k with the given modifiers added
type KeyModifiers ¶
type KeyModifiers uint8
KeyModifiers is a bitmask of modifier keys
const ( ModNone KeyModifiers = 0 ModShift KeyModifiers = 1 << iota ModCtrl ModAlt )
func (KeyModifiers) Has ¶
func (k KeyModifiers) Has(mod KeyModifiers) bool
type KeyResultAction ¶ added in v0.1.28
KeyResultAction handles a key sequence and returns its command result
type Keymaps ¶
type Keymaps struct {
// contains filtered or unexported fields
}
Keymaps is the combined command registry and key-event dispatch trie
func (*Keymaps) BindResultAction ¶ added in v0.1.28
func (k *Keymaps) BindResultAction( modes []view.Mode, action KeyResultAction, label string, seqs ...[]KeyEvent, ) error
BindResultAction adds key sequences for a result-returning action
func (*Keymaps) Bindings ¶
func (k *Keymaps) Bindings(mode view.Mode, name string) KeyBinding
Bindings returns key sequences bound to a command in a mode
func (*Keymaps) CommandsIn ¶
CommandsIn returns registered commands available in the named mode
func (*Keymaps) LabelNode ¶
func (k *Keymaps) LabelNode(mode view.Mode, prefix KeyBinding, name string)
LabelNode names the node reached by each alternative in prefix, so a shared menu (e.g. the Space and Ctrl-\ leaders) is labelled everywhere it is reached
func (*Keymaps) PendingHints ¶
PendingHints returns the title and (key, label) pairs for the node reached by seq in mode, used to populate the pending-key info popup
func (*Keymaps) Register ¶
Register adds a command entry and wires its key bindings. Returns ErrDuplicateCommand if name is already registered - each command must be fully declared once, in the module that owns it
func (*Keymaps) ResolveCommand ¶
ResolveCommand looks up a command by typeable alias
type LookupRes ¶ added in v0.1.28
type LookupRes struct {
Action KeyResultAction
Name string
Prefix bool
}
LookupRes is the result of traversing the key trie
type Module ¶
type Module struct {
Commands []Command
Options []Option
Section *Section
Labels []PrefixLabel
}
Module groups a set of commands, runtime options, and an optional config section. Options are registered into the editor option registry when the module is installed
type Option ¶
type Option struct {
Key string
Get OptionGetter
Set OptionSetter
KeyGet OptionKeyGetter
KeySet OptionKeySetter
Toggle OptionGetter
Complete CompletionFunc
}
Option describes a runtime editor option owned by a module. Toggle is nil for options that are not boolean-toggleable
type OptionGetter ¶
OptionGetter reads an option's current value from the editor
type OptionKeyGetter ¶
OptionKeyGetter reads concrete values owned by an option key prefix
type OptionKeySetter ¶
OptionKeySetter applies a concrete option key owned by a key prefix
type OptionSetter ¶
OptionSetter applies a new option value to the editor
type ParseError ¶
type ParseError struct {
Kind ParseErrorKind
Token Token
Flag string
Text string
Min int
Max int // 0 = no maximum
Actual int
}
ParseError reports a command-line parser validation failure
func (*ParseError) Error ¶
func (p *ParseError) Error() string
func (*ParseError) Is ¶
func (p *ParseError) Is(target error) bool
type ParseErrorKind ¶
type ParseErrorKind int
ParseErrorKind identifies a parser validation failure
const ( ParseErrorWrongPositionalCount ParseErrorKind = iota ParseErrorUnterminatedToken ParseErrorDuplicatedFlag ParseErrorUnknownFlag ParseErrorFlagMissingArgument ParseErrorMissingExpansionDelimiter ParseErrorUnknownExpansion )
type Positionals ¶
Positionals constrains the accepted positional argument count
type PrefixLabel ¶
type PrefixLabel struct {
Modes view.Mode
Seq KeyBinding
Label string
}
PrefixLabel names an intermediate key-sequence node for the pending-key hint popup, letting a module label the prefixes it owns
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry owns installed commands, runtime options, and config sections
func NewRegistry ¶
func (*Registry) ApplyOptionValues ¶
ApplyOptionValues applies a set of runtime option strings through the same handlers used by :set
func (*Registry) ApplyTOML ¶
ApplyTOML resets all sections to defaults, decodes the merged TOML map into each section, then calls each section's Apply to push typed values into editor Options. Pass an empty map when no config file is present
func (*Registry) BoolOptionCompleter ¶
func (r *Registry) BoolOptionCompleter() CompletionFunc
func (*Registry) BoolOptionKeys ¶
BoolOptionKeys returns registered option keys that support toggle
func (*Registry) LookupOption ¶
LookupOption returns the registered Option for the given key, if any
func (*Registry) OptionCompleter ¶
func (r *Registry) OptionCompleter() CompletionFunc
func (*Registry) OptionKeys ¶
OptionKeys returns all registered option keys in sorted order
func (*Registry) OptionValueCompleter ¶
func (r *Registry) OptionValueCompleter() CompletionFunc
OptionValueCompleter completes an option's value, dispatching to the completer registered against the option named by the already-parsed first positional argument (e.g. the key in "set <key> <value>")
func (*Registry) OptionValues ¶
OptionValues returns the current string value for every registered runtime option
func (*Registry) RegisterCommand ¶
RegisterCommand registers a command with its kebab-cased name as the first alias
func (*Registry) RegisterModule ¶
type Result ¶
type Result struct {
Signal Signal
Message string
Error error
Continuation Continuation
}
Result is returned by a Run function
type Section ¶
type Section struct {
Config any // *ConcreteConfig, pre-filled with defaults
Reset func()
Apply func(*view.Editor)
}
Section declares a module's live config pointer and Apply hook
type Signal ¶
type Signal int
Signal is a post-execution application-level effect
const ( SignalQuit Signal SignalClearScreen )
type Signature ¶
type Signature struct {
Positionals Positionals
RawAfter int // 0 = disabled; n = switch to raw after n positionals
Flags []Flag
Completer Completer
}
Signature describes positional, raw, and flag parsing for a command
func DefaultSignature ¶
func DefaultSignature() Signature
DefaultSignature returns a signature that accepts any number of positionals
type Special ¶
type Special uint8
Special enumerates the non-printable keys; SpecialNone means the key is a printable KeyCode.Char instead
type SyntaxError ¶
type SyntaxError struct {
Kind SyntaxErrorKind
Token Token
Text string
}
SyntaxError is a tokenizer-level parse error
func (*SyntaxError) Error ¶
func (s *SyntaxError) Error() string
func (*SyntaxError) Is ¶
func (s *SyntaxError) Is(target error) bool
type SyntaxErrorKind ¶
type SyntaxErrorKind int
SyntaxErrorKind identifies a tokenizer-level parse failure
const ( SyntaxErrorUnterminatedToken SyntaxErrorKind = iota SyntaxErrorMissingExpansionDelimiter SyntaxErrorUnknownExpansion )
type Token ¶
type Token struct {
Kind TokenKind
Expansion ExpansionKind
Quote Quote
ContentStart int
Content string
Terminated bool
}
Token is a token from command-line input
type TokenExpander ¶
TokenExpander maps a raw token to its expanded string value; a nil expander uses the token content verbatim
type Tokenizer ¶
type Tokenizer struct {
// contains filtered or unexported fields
}
Tokenizer tokenizes command-line input
func NewTokenizer ¶
NewTokenizer returns a tokenizer for command-line input