Minga.Command (Minga v0.1.0)

Copy Markdown View Source

A named editor command.

Commands are the bridge between key sequences (via the keymap trie) and editor state mutations. Each command has a name (for registry lookup), a human-readable description (for which-key display), and an execute function that maps editor state → new editor state.

Scopeable commands

Commands that toggle buffer-local options can declare an option_toggle. When invoked from a keybinding, scopeable commands apply to the current buffer (fast path). When invoked from the command palette, the palette presents a scope picker ("This Buffer" / "All Buffers") so the user can choose where the change applies.

The option_toggle field is either:

  • an atom (e.g. :wrap) for boolean toggles
  • a {atom, function} tuple (e.g. {:line_numbers, fn :hybrid -> :absolute end}) for non-boolean cycling

Buffer requirement

Commands that only make sense with an active buffer set requires_buffer: true. The dispatch layer skips these commands (returning state unchanged) when no buffer is active.

Example

%Minga.Command{
  name: :save,
  description: "Save the current file",
  execute: fn state -> MingaEditor.Commands.BufferManagement.execute(state, :save) end,
  requires_buffer: true
}

# Scopeable toggle:
%Minga.Command{
  name: :toggle_wrap,
  description: "Toggle word wrap",
  execute: fn state -> MingaEditor.Commands.BufferManagement.execute(state, :toggle_wrap) end,
  requires_buffer: true,
  option_toggle: :wrap
}

Summary

Types

Descriptor for commands that toggle buffer-local options.

t()

An editor command struct.

Functions

Returns all registered commands as a list.

Computes the new value for a scopeable command given the current value.

Looks up a command by name. Returns {:ok, command} or :error.

Extracts the option name from an option_toggle field.

Parses a command-line string into a structured command invocation.

Registers a command with name, description, and execute function.

Resets the registry to built-in commands (discards user/extension commands).

Returns true if this command toggles a buffer-local option.

Types

option_toggle()

@type option_toggle() :: atom() | {atom(), (term() -> term())}

Descriptor for commands that toggle buffer-local options.

An atom for boolean toggles, or {atom, function} for custom cycling.

t()

@type t() :: %Minga.Command{
  description: String.t(),
  execute: function(),
  name: atom(),
  option_toggle: option_toggle() | nil,
  requires_buffer: boolean(),
  scope: atom() | nil
}

An editor command struct.

  • name — atom identifier used for registry lookup and keymap binding
  • description — human-readable label shown in which-key popups
  • execute — function applied to the editor state, returns new state
  • requires_buffer — when true, command is skipped if no buffer is active
  • option_toggle — option to toggle: atom for boolean flip, {atom, fun} for custom cycling
  • scope — keymap scope required for this command to run (e.g. :agent); nil means any scope

Functions

all_commands()

@spec all_commands() :: [t()]

Returns all registered commands as a list.

compute_new_value(command, current)

@spec compute_new_value(t(), term()) :: term()

Computes the new value for a scopeable command given the current value.

Atom toggles negate the boolean. Tuple toggles call the cycling function.

lookup(name)

@spec lookup(atom()) :: {:ok, t()} | :error

Looks up a command by name. Returns {:ok, command} or :error.

option_name(command)

@spec option_name(t()) :: atom() | nil

Extracts the option name from an option_toggle field.

parse(input)

Parses a command-line string into a structured command invocation.

register(name, description, execute)

@spec register(atom(), String.t(), (term() -> term())) :: :ok

Registers a command with name, description, and execute function.

reset_registry()

@spec reset_registry() :: :ok

Resets the registry to built-in commands (discards user/extension commands).

scopeable?(command)

@spec scopeable?(t()) :: boolean()

Returns true if this command toggles a buffer-local option.