Documentation
¶
Overview ¶
Package keymap manages centralized keyboard shortcut registration, multi-key sequence handling, context-aware binding resolution, and user-configurable key overrides.
Index ¶
- Variables
- func ApplyConfig(r *Registry, cfg *Config)
- func RegisterDefaults(r *Registry)
- type Binding
- type Category
- type Command
- type Config
- type Registry
- func (r *Registry) AllContexts() []string
- func (r *Registry) BindingsForContext(context string) []Binding
- func (r *Registry) CommandForContextKey(context, key string) (string, bool)
- func (r *Registry) GetCommand(id string) (Command, bool)
- func (r *Registry) Handle(key tea.KeyMsg, activeContext string) tea.Cmd
- func (r *Registry) HasPending() bool
- func (r *Registry) RegisterBinding(b Binding)
- func (r *Registry) RegisterCommand(cmd Command)
- func (r *Registry) RegisterPluginBinding(key, command, context string)
- func (r *Registry) ResetPending()
- func (r *Registry) SetUserOverride(key, commandID string)
- func (r *Registry) UserOverride(key tea.KeyMsg) (tea.Cmd, bool)
Constants ¶
This section is empty.
Variables ¶
var BracketTabCycleKeys = map[string]bool{ "[": true, "]": true, }
BracketTabCycleKeys are the keys a context may bind to prev-plugin or next-plugin to opt in to sidecar tab cycling.
Brackets are not a global. Several tabs already give them a local meaning (`[`/`]` step between file-browser tabs, workspace preview tabs, and files in a diff), and taking those away would trade one broken habit for another. So the host cycles tabs on a bracket only where the keymap says this context asked for it, which keeps the decision in the binding table next to every other key instead of in a context name hard-coded into the host.
internal/app's handleKeyMsg is the only consumer; the commands are the same ones backtick and tilde run.
var GlobalKeys = map[string]bool{ "`": true, "~": true, "1": true, "2": true, "3": true, "4": true, "5": true, "6": true, "7": true, "8": true, "9": true, "?": true, "!": true, "@": true, "K": true, "W": true, "#": true, "^": true, "i": true, "q": true, "ctrl+c": true, }
GlobalKeys are the keys sidecar's own root key handler acts on while an ordinary plugin tab is focused: tab cycling and direct tab selection, the palette, diagnostics, the project/worktree/theme switchers, Open In, the issue input, and the quit flow.
It exists so a plugin can tell "this key collides with sidecar" from "this key is mine alone" without duplicating the host's switch statement, and so that collision policy can be stated as a rule instead of a list of accidents.
`r` is deliberately absent: sidecar's refresh yields to the plugin in any context isGlobalRefreshContext does not name, which is every plugin context that binds `r` itself.
internal/app's TestGlobalKeysAreTheOnesTheHostActuallyHandles pins this against the real handler, so it cannot drift from the switch it describes.
HostReservedKeys are keys no plugin may take from sidecar's global handling, whatever its key router claims.
These are the host's non-negotiables. `ctrl+c` and `q` are the only ways out of sidecar, and an embedded plugin must never be able to swallow the exit. `?` opens the merged contextual help, which is the surface that lists every plugin command — including the ones the plugin would rather bind `?` to.
A plugin still gets these keys where it genuinely owns the keyboard: an overlay or text-input context (precedence level 2) is forwarded everything except ctrl+c, and that runs before any claim is consulted.
Functions ¶
func ApplyConfig ¶
ApplyConfig applies user configuration overrides to the registry.
func RegisterDefaults ¶
func RegisterDefaults(r *Registry)
RegisterDefaults registers all default bindings with the given registry.
Types ¶
type Binding ¶
type Binding struct {
Key string // e.g., "tab", "ctrl+s", "g g"
Command string // Command ID
Context string // "global", plugin ID, etc.
}
Binding maps a key or key sequence to a command.
type Config ¶
Config represents user key binding configuration.
func LoadConfig ¶
LoadConfig loads key binding overrides from a JSON file.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry manages key bindings and command dispatch.
func (*Registry) AllContexts ¶
AllContexts returns all contexts that have bindings.
func (*Registry) BindingsForContext ¶
BindingsForContext returns all bindings for a given context.
func (*Registry) CommandForContextKey ¶ added in v0.95.0
CommandForContextKey returns the command a key is bound to in exactly this context. It does not fall back to the global context and does not consult user overrides: callers use it to ask "did this context opt in to this key?", which a global binding or an override would answer for a different question.
func (*Registry) GetCommand ¶
GetCommand retrieves a command by ID. Returns the command and true if found, or zero value and false otherwise.
func (*Registry) Handle ¶
Handle dispatches a key event to the appropriate command handler. Returns nil if no matching binding is found.
func (*Registry) HasPending ¶
HasPending returns true if there's a pending key sequence.
func (*Registry) RegisterBinding ¶
RegisterBinding adds a key binding. Registering the same binding again is a no-op.
Idempotence matters because the registry lives for the process lifetime while plugins are re-initialized on every project switch: a plugin that publishes its table on adoption would otherwise grow the context's slice without bound. Nothing downstream wants duplicates either — the palette already has to de-duplicate, and lookup takes the first match.
func (*Registry) RegisterCommand ¶
RegisterCommand adds a command to the registry.
func (*Registry) RegisterPluginBinding ¶
RegisterPluginBinding satisfies plugin.BindingRegistrar interface. It converts plugin.Binding to keymap.Binding and registers it.
func (*Registry) ResetPending ¶
func (r *Registry) ResetPending()
ResetPending clears any pending key sequence.
func (*Registry) SetUserOverride ¶
SetUserOverride sets a user-configured key override.
func (*Registry) UserOverride ¶ added in v0.95.0
UserOverride returns the command a user override binds to this key, if any.
It exists so a caller that runs *before* Handle in the key ladder can still honour the user's mapping. Handle consults the same overrides first (findCommand step 1); this exposes only that step.
The bool reports that an override exists and resolved to a runnable command. An override naming an unknown command is not a claim on the key: the caller should carry on rather than swallow the keystroke.