keymap

package
v1.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 7 Imported by: 0 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 keymap manages centralized keyboard shortcut registration, multi-key sequence handling, context-aware binding resolution, and user-configurable key overrides.

Index

Constants

This section is empty.

Variables

View Source
var GlobalKeys = map[string]bool{
	"`": true, "~": true, "[": true, "]": true,

	"1": true, "2": true, "3": true, "4": true, "5": true, "6": true,
	"7": true, "8": true, "9": true, "0": 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.

View Source
var HostReservedKeys = map[string]bool{
	"ctrl+c": true,
	"q":      true,
	"?":      true,
}

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

func ApplyConfig(r *Registry, cfg *Config)

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.
	// Feature names the optional runtime gate for this binding. Keeping the gate
	// on the binding makes dispatch, footer/help discovery, and live config
	// changes agree about whether the key exists.
	Feature string
}

Binding maps a key or key sequence to a command.

func DefaultBindings

func DefaultBindings() []Binding

DefaultBindings returns the default keymap.

type Category

type Category string

Category represents a command category.

const (
	CategoryNavigation Category = "Navigation"
	CategoryActions    Category = "Actions"
	CategoryView       Category = "View"
	CategorySearch     Category = "Search"
	CategorySystem     Category = "System"
)

type Command

type Command struct {
	ID      string
	Name    string
	Handler func() tea.Cmd
	Context string
}

Command represents a registered command handler.

type Config

type Config struct {
	Bindings map[string]string `json:"bindings"` // key -> command ID
}

Config represents user key binding configuration.

func LoadConfig

func LoadConfig(path string) (*Config, error)

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 NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new keymap registry.

func (*Registry) AllContexts

func (r *Registry) AllContexts() []string

AllContexts returns all contexts that have bindings.

func (*Registry) BindingsForContext

func (r *Registry) BindingsForContext(context string) []Binding

BindingsForContext returns all bindings for a given context.

func (*Registry) CommandForContextKey added in v0.95.0

func (r *Registry) CommandForContextKey(context, key string) (string, bool)

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

func (r *Registry) GetCommand(id string) (Command, bool)

GetCommand retrieves a command by ID. Returns the command and true if found, or zero value and false otherwise.

func (*Registry) Handle

func (r *Registry) Handle(key tea.KeyMsg, activeContext string) tea.Cmd

Handle dispatches a key event to the appropriate command handler. Returns nil if no matching binding is found.

func (*Registry) HasPending

func (r *Registry) HasPending() bool

HasPending returns true if there's a pending key sequence.

func (*Registry) RegisterBinding

func (r *Registry) RegisterBinding(b Binding)

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

func (r *Registry) RegisterCommand(cmd Command)

RegisterCommand adds a command to the registry.

func (*Registry) RegisterPluginBinding

func (r *Registry) RegisterPluginBinding(key, command, context string)

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

func (r *Registry) SetUserOverride(key, commandID string)

SetUserOverride sets a user-configured key override.

func (*Registry) UserOverride added in v0.95.0

func (r *Registry) UserOverride(key tea.KeyMsg) (tea.Cmd, bool)

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.

Jump to

Keyboard shortcuts

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