Lutra/crates/engine/src/registry.rs
danny 184b8a3468 M1.8: command palette, global keymap, UI commands
- Engine: ui.registerCommands (validated, replaces the previous set),
  UI commands in listCommands with ui: true and labelKey, running one
  through the engine emits uiCommand {id, params}.
- Frontend: ui/src/commands/ (keymap by key code, fuzzy ranking, Pinia
  commands store with handlers, recents and error toasts), the command
  palette (Mod+Shift+P), UI commands palette.open, settings.open (Mod+,,
  disabled until M1.9b) and view.toggleProjectTree (Mod+1). The editor's
  shortcuts.ts moved into the global keymap.
- Tests: engine and control-channel tests for registration and
  uiCommand, Vitest for keymap, ranking, store, palette and App wiring.
- Docs: architecture, ui-parity, target-app-parity, gaps, roadmap,
  ROADMAP, README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 16:59:43 +02:00

55 lines
1.5 KiB
Rust

//! The command registry, keyed by command id.
use std::collections::BTreeMap;
use crate::{Command, EngineError};
/// All registered commands, in id order.
#[derive(Default)]
pub struct Registry {
/// Commands by id.
commands: BTreeMap<&'static str, Box<dyn Command>>,
}
impl Registry {
/// Adds a command.
///
/// # Errors
/// Returns [`EngineError::Internal`] if the id is already taken; two
/// commands with one id is a programming error.
pub fn register(&mut self, command: Box<dyn Command>) -> Result<(), EngineError> {
let id = command.spec().id;
if self.commands.contains_key(id) {
return Err(EngineError::Internal(format!(
"command registered twice: {id}"
)));
}
self.commands.insert(id, command);
Ok(())
}
/// Looks a command up by id.
pub fn get(&self, id: &str) -> Option<&dyn Command> {
self.commands.get(id).map(Box::as_ref)
}
/// The ids of every command.
pub fn ids(&self) -> impl Iterator<Item = &'static str> + '_ {
self.commands.keys().copied()
}
/// Iterates over the commands in id order.
pub fn iter(&self) -> impl Iterator<Item = &dyn Command> {
self.commands.values().map(Box::as_ref)
}
/// Number of registered commands.
pub fn len(&self) -> usize {
self.commands.len()
}
/// Whether no command is registered.
pub fn is_empty(&self) -> bool {
self.commands.is_empty()
}
}