canvas library config is now required
This commit is contained in:
@@ -18,6 +18,7 @@ tokio = { workspace = true }
|
||||
toml = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
unicode-width.workspace = true
|
||||
thiserror = { workspace = true }
|
||||
|
||||
tracing = "0.1.41"
|
||||
tracing-subscriber = "0.3.19"
|
||||
|
||||
50
canvas/canvas_config_clean.toml
Normal file
50
canvas/canvas_config_clean.toml
Normal file
@@ -0,0 +1,50 @@
|
||||
[keybindings.edit]
|
||||
# Required
|
||||
prev_field = ["Shift+Tab"]
|
||||
move_left = ["Left", "h"]
|
||||
move_up = ["Up", "k"]
|
||||
move_right = ["Right", "l"]
|
||||
move_down = ["Down", "j"]
|
||||
delete_char_backward = ["Backspace"]
|
||||
next_field = ["Tab", "Enter"]
|
||||
# Optional
|
||||
delete_char_forward = ["Delete"]
|
||||
move_line_start = ["Home", "0"]
|
||||
move_first_line = ["Ctrl+Home", "gg"]
|
||||
move_word_end_prev = ["ge"]
|
||||
move_word_prev = ["Ctrl+Left", "b"]
|
||||
move_word_next = ["Ctrl+Right", "w"]
|
||||
move_word_end = ["e"]
|
||||
move_line_end = ["End", "$"]
|
||||
move_last_line = ["Ctrl+End", "G"]
|
||||
|
||||
[keybindings.read_only]
|
||||
# Required
|
||||
move_right = ["l", "Right"]
|
||||
move_left = ["h", "Left"]
|
||||
move_down = ["j", "Down"]
|
||||
move_up = ["k", "Up"]
|
||||
# Optional
|
||||
move_line_end = ["$"]
|
||||
move_line_start = ["0"]
|
||||
move_word_end_prev = ["ge"]
|
||||
move_first_line = ["gg"]
|
||||
move_word_next = ["w"]
|
||||
prev_field = ["Shift+Tab"]
|
||||
move_word_prev = ["b"]
|
||||
move_word_end = ["e"]
|
||||
next_field = ["Tab"]
|
||||
move_last_line = ["G"]
|
||||
|
||||
[keybindings.suggestions]
|
||||
# Required
|
||||
suggestion_down = ["Down", "Ctrl+n"]
|
||||
suggestion_up = ["Up", "Ctrl+p"]
|
||||
select_suggestion = ["Enter", "Tab"]
|
||||
exit_suggestions = ["Esc"]
|
||||
|
||||
[keybindings.global]
|
||||
# Optional
|
||||
move_up = ["Up"]
|
||||
move_down = ["Down"]
|
||||
|
||||
114
canvas/canvas_config_template2.toml
Normal file
114
canvas/canvas_config_template2.toml
Normal file
@@ -0,0 +1,114 @@
|
||||
# Canvas Library Configuration Template
|
||||
# Generated automatically - customize as needed
|
||||
|
||||
[keybindings.edit]
|
||||
# REQUIRED ACTIONS - These must be configured
|
||||
# Move to next field or line
|
||||
move_down = ["Down", "j"]
|
||||
|
||||
# Delete character before cursor
|
||||
delete_char_backward = ["Backspace"]
|
||||
|
||||
# Move cursor one position to the left
|
||||
move_left = ["Left", "h"]
|
||||
|
||||
# Move to previous input field
|
||||
prev_field = ["Shift+Tab"]
|
||||
|
||||
# Move to next input field
|
||||
next_field = ["Tab", "Enter"]
|
||||
|
||||
# Move to previous field or line
|
||||
move_up = ["Up", "k"]
|
||||
|
||||
# Move cursor one position to the right
|
||||
move_right = ["Right", "l"]
|
||||
|
||||
# OPTIONAL ACTIONS - Configure these if you want them enabled
|
||||
# Delete character after cursor
|
||||
# delete_char_forward = ["Delete"]
|
||||
|
||||
# Move cursor to start of next word
|
||||
# move_word_next = ["Ctrl+Right", "w"]
|
||||
|
||||
# Move cursor to end of line
|
||||
# move_line_end = ["End", "$"]
|
||||
|
||||
# Move cursor to end of current/next word
|
||||
# move_word_end = ["e"]
|
||||
|
||||
# Move cursor to end of previous word
|
||||
# move_word_end_prev = ["ge"]
|
||||
|
||||
# Move cursor to beginning of line
|
||||
# move_line_start = ["Home", "0"]
|
||||
|
||||
# Move to first field
|
||||
# move_first_line = ["Ctrl+Home", "gg"]
|
||||
|
||||
# Move cursor to start of previous word
|
||||
# move_word_prev = ["Ctrl+Left", "b"]
|
||||
|
||||
# Move to last field
|
||||
# move_last_line = ["Ctrl+End", "G"]
|
||||
|
||||
[keybindings.read_only]
|
||||
# REQUIRED ACTIONS - These must be configured
|
||||
# Move cursor one position to the right
|
||||
move_right = ["l", "Right"]
|
||||
|
||||
# Move to previous field
|
||||
move_up = ["k", "Up"]
|
||||
|
||||
# Move to next field
|
||||
move_down = ["j", "Down"]
|
||||
|
||||
# Move cursor one position to the left
|
||||
move_left = ["h", "Left"]
|
||||
|
||||
# OPTIONAL ACTIONS - Configure these if you want them enabled
|
||||
# Move to next input field
|
||||
# next_field = ["Tab"]
|
||||
|
||||
# Move to last field
|
||||
# move_last_line = ["G"]
|
||||
|
||||
# Move to first field
|
||||
# move_first_line = ["gg"]
|
||||
|
||||
# Move cursor to start of previous word
|
||||
# move_word_prev = ["b"]
|
||||
|
||||
# Move cursor to start of next word
|
||||
# move_word_next = ["w"]
|
||||
|
||||
# Move cursor to end of current/next word
|
||||
# move_word_end = ["e"]
|
||||
|
||||
# Move cursor to beginning of line
|
||||
# move_line_start = ["0"]
|
||||
|
||||
# Move cursor to end of line
|
||||
# move_line_end = ["$"]
|
||||
|
||||
# Move to previous input field
|
||||
# prev_field = ["Shift+Tab"]
|
||||
|
||||
# Move cursor to end of previous word
|
||||
# move_word_end_prev = ["ge"]
|
||||
|
||||
[keybindings.suggestions]
|
||||
# REQUIRED ACTIONS - These must be configured
|
||||
# Move selection to previous suggestion
|
||||
suggestion_up = ["Up", "Ctrl+p"]
|
||||
|
||||
# Close suggestions without selecting
|
||||
exit_suggestions = ["Esc"]
|
||||
|
||||
# Select the currently highlighted suggestion
|
||||
select_suggestion = ["Enter", "Tab"]
|
||||
|
||||
# Move selection to next suggestion
|
||||
suggestion_down = ["Down", "Ctrl+n"]
|
||||
|
||||
|
||||
21
canvas/examples/generate_template.rs
Normal file
21
canvas/examples/generate_template.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
// examples/generate_template.rs
|
||||
use canvas::config::CanvasConfig;
|
||||
use std::env;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
if args.len() > 1 && args[1] == "clean" {
|
||||
// Generate clean template with 80% active code
|
||||
let template = CanvasConfig::generate_clean_template();
|
||||
println!("{}", template);
|
||||
} else {
|
||||
// Generate verbose template with descriptions (default)
|
||||
let template = CanvasConfig::generate_template();
|
||||
println!("{}", template);
|
||||
}
|
||||
}
|
||||
|
||||
// Usage:
|
||||
// cargo run --example generate_template > canvas_config.toml
|
||||
// cargo run --example generate_template clean > canvas_config_clean.toml
|
||||
@@ -1,494 +0,0 @@
|
||||
// canvas/src/config.rs
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use crossterm::event::{KeyCode, KeyModifiers};
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CanvasConfig {
|
||||
#[serde(default)]
|
||||
pub keybindings: CanvasKeybindings,
|
||||
#[serde(default)]
|
||||
pub behavior: CanvasBehavior,
|
||||
#[serde(default)]
|
||||
pub appearance: CanvasAppearance,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct CanvasKeybindings {
|
||||
#[serde(default)]
|
||||
pub read_only: HashMap<String, Vec<String>>,
|
||||
#[serde(default)]
|
||||
pub edit: HashMap<String, Vec<String>>,
|
||||
#[serde(default)]
|
||||
pub suggestions: HashMap<String, Vec<String>>,
|
||||
#[serde(default)]
|
||||
pub global: HashMap<String, Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CanvasBehavior {
|
||||
#[serde(default = "default_wrap_around")]
|
||||
pub wrap_around_fields: bool,
|
||||
#[serde(default = "default_auto_save")]
|
||||
pub auto_save_on_field_change: bool,
|
||||
#[serde(default = "default_word_chars")]
|
||||
pub word_chars: String,
|
||||
#[serde(default = "default_suggestion_limit")]
|
||||
pub max_suggestions: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CanvasAppearance {
|
||||
#[serde(default = "default_cursor_style")]
|
||||
pub cursor_style: String, // "block", "bar", "underline"
|
||||
#[serde(default = "default_show_field_numbers")]
|
||||
pub show_field_numbers: bool,
|
||||
#[serde(default = "default_highlight_current_field")]
|
||||
pub highlight_current_field: bool,
|
||||
}
|
||||
|
||||
// Default values
|
||||
fn default_wrap_around() -> bool { true }
|
||||
fn default_auto_save() -> bool { false }
|
||||
fn default_word_chars() -> String { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_".to_string() }
|
||||
fn default_suggestion_limit() -> usize { 10 }
|
||||
fn default_cursor_style() -> String { "block".to_string() }
|
||||
fn default_show_field_numbers() -> bool { false }
|
||||
fn default_highlight_current_field() -> bool { true }
|
||||
|
||||
impl Default for CanvasBehavior {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
wrap_around_fields: default_wrap_around(),
|
||||
auto_save_on_field_change: default_auto_save(),
|
||||
word_chars: default_word_chars(),
|
||||
max_suggestions: default_suggestion_limit(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CanvasAppearance {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
cursor_style: default_cursor_style(),
|
||||
show_field_numbers: default_show_field_numbers(),
|
||||
highlight_current_field: default_highlight_current_field(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CanvasConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
keybindings: CanvasKeybindings::with_vim_defaults(),
|
||||
behavior: CanvasBehavior::default(),
|
||||
appearance: CanvasAppearance::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CanvasKeybindings {
|
||||
pub fn with_vim_defaults() -> Self {
|
||||
let mut keybindings = Self::default();
|
||||
|
||||
// Read-only mode (vim-style navigation)
|
||||
keybindings.read_only.insert("move_left".to_string(), vec!["h".to_string()]);
|
||||
keybindings.read_only.insert("move_right".to_string(), vec!["l".to_string()]);
|
||||
keybindings.read_only.insert("move_up".to_string(), vec!["k".to_string()]);
|
||||
keybindings.read_only.insert("move_down".to_string(), vec!["j".to_string()]);
|
||||
keybindings.read_only.insert("move_word_next".to_string(), vec!["w".to_string()]);
|
||||
keybindings.read_only.insert("move_word_end".to_string(), vec!["e".to_string()]);
|
||||
keybindings.read_only.insert("move_word_prev".to_string(), vec!["b".to_string()]);
|
||||
keybindings.read_only.insert("move_word_end_prev".to_string(), vec!["ge".to_string()]);
|
||||
keybindings.read_only.insert("move_line_start".to_string(), vec!["0".to_string()]);
|
||||
keybindings.read_only.insert("move_line_end".to_string(), vec!["$".to_string()]);
|
||||
keybindings.read_only.insert("move_first_line".to_string(), vec!["gg".to_string()]);
|
||||
keybindings.read_only.insert("move_last_line".to_string(), vec!["G".to_string()]);
|
||||
keybindings.read_only.insert("next_field".to_string(), vec!["Tab".to_string()]);
|
||||
keybindings.read_only.insert("prev_field".to_string(), vec!["Shift+Tab".to_string()]);
|
||||
|
||||
// Edit mode
|
||||
keybindings.edit.insert("delete_char_backward".to_string(), vec!["Backspace".to_string()]);
|
||||
keybindings.edit.insert("delete_char_forward".to_string(), vec!["Delete".to_string()]);
|
||||
keybindings.edit.insert("move_left".to_string(), vec!["Left".to_string()]);
|
||||
keybindings.edit.insert("move_right".to_string(), vec!["Right".to_string()]);
|
||||
keybindings.edit.insert("move_up".to_string(), vec!["Up".to_string()]);
|
||||
keybindings.edit.insert("move_down".to_string(), vec!["Down".to_string()]);
|
||||
keybindings.edit.insert("move_line_start".to_string(), vec!["Home".to_string()]);
|
||||
keybindings.edit.insert("move_line_end".to_string(), vec!["End".to_string()]);
|
||||
keybindings.edit.insert("move_word_next".to_string(), vec!["Ctrl+Right".to_string()]);
|
||||
keybindings.edit.insert("move_word_prev".to_string(), vec!["Ctrl+Left".to_string()]);
|
||||
keybindings.edit.insert("next_field".to_string(), vec!["Tab".to_string()]);
|
||||
keybindings.edit.insert("prev_field".to_string(), vec!["Shift+Tab".to_string()]);
|
||||
|
||||
// Suggestions
|
||||
keybindings.suggestions.insert("suggestion_up".to_string(), vec!["Up".to_string(), "Ctrl+p".to_string()]);
|
||||
keybindings.suggestions.insert("suggestion_down".to_string(), vec!["Down".to_string(), "Ctrl+n".to_string()]);
|
||||
keybindings.suggestions.insert("select_suggestion".to_string(), vec!["Enter".to_string(), "Tab".to_string()]);
|
||||
keybindings.suggestions.insert("exit_suggestions".to_string(), vec!["Esc".to_string()]);
|
||||
|
||||
// Global (works in both modes)
|
||||
keybindings.global.insert("move_up".to_string(), vec!["Up".to_string()]);
|
||||
keybindings.global.insert("move_down".to_string(), vec!["Down".to_string()]);
|
||||
|
||||
keybindings
|
||||
}
|
||||
|
||||
pub fn with_emacs_defaults() -> Self {
|
||||
let mut keybindings = Self::default();
|
||||
|
||||
// Emacs-style bindings
|
||||
keybindings.read_only.insert("move_left".to_string(), vec!["Ctrl+b".to_string()]);
|
||||
keybindings.read_only.insert("move_right".to_string(), vec!["Ctrl+f".to_string()]);
|
||||
keybindings.read_only.insert("move_up".to_string(), vec!["Ctrl+p".to_string()]);
|
||||
keybindings.read_only.insert("move_down".to_string(), vec!["Ctrl+n".to_string()]);
|
||||
keybindings.read_only.insert("move_word_next".to_string(), vec!["Alt+f".to_string()]);
|
||||
keybindings.read_only.insert("move_word_prev".to_string(), vec!["Alt+b".to_string()]);
|
||||
keybindings.read_only.insert("move_line_start".to_string(), vec!["Ctrl+a".to_string()]);
|
||||
keybindings.read_only.insert("move_line_end".to_string(), vec!["Ctrl+e".to_string()]);
|
||||
|
||||
keybindings.edit.insert("delete_char_backward".to_string(), vec!["Ctrl+h".to_string(), "Backspace".to_string()]);
|
||||
keybindings.edit.insert("delete_char_forward".to_string(), vec!["Ctrl+d".to_string(), "Delete".to_string()]);
|
||||
|
||||
keybindings
|
||||
}
|
||||
}
|
||||
|
||||
impl CanvasConfig {
|
||||
/// Load from canvas_config.toml or fallback to vim defaults
|
||||
pub fn load() -> Self {
|
||||
// Try to load canvas_config.toml from current directory
|
||||
if let Ok(config) = Self::from_file(std::path::Path::new("canvas_config.toml")) {
|
||||
return config;
|
||||
}
|
||||
|
||||
// Fallback to vim defaults
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Load from TOML string
|
||||
pub fn from_toml(toml_str: &str) -> Result<Self> {
|
||||
toml::from_str(toml_str)
|
||||
.with_context(|| "Failed to parse canvas config TOML")
|
||||
}
|
||||
|
||||
/// Load from file
|
||||
pub fn from_file(path: &std::path::Path) -> Result<Self> {
|
||||
let contents = std::fs::read_to_string(path)
|
||||
.with_context(|| format!("Failed to read config file: {:?}", path))?;
|
||||
Self::from_toml(&contents)
|
||||
}
|
||||
|
||||
/// NEW: Check if autocomplete should auto-trigger (simple logic)
|
||||
pub fn should_auto_trigger_autocomplete(&self) -> bool {
|
||||
// If trigger_autocomplete keybinding exists anywhere, use manual mode only
|
||||
// If no trigger_autocomplete keybinding, use auto-trigger mode
|
||||
!self.has_trigger_autocomplete_keybinding()
|
||||
}
|
||||
|
||||
/// NEW: Check if user has configured manual trigger keybinding
|
||||
pub fn has_trigger_autocomplete_keybinding(&self) -> bool {
|
||||
self.keybindings.edit.contains_key("trigger_autocomplete") ||
|
||||
self.keybindings.read_only.contains_key("trigger_autocomplete") ||
|
||||
self.keybindings.global.contains_key("trigger_autocomplete")
|
||||
}
|
||||
|
||||
/// Get action for key in read-only mode
|
||||
pub fn get_read_only_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
|
||||
self.get_action_in_mode(&self.keybindings.read_only, key, modifiers)
|
||||
.or_else(|| self.get_action_in_mode(&self.keybindings.global, key, modifiers))
|
||||
}
|
||||
|
||||
/// Get action for key in edit mode
|
||||
pub fn get_edit_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
|
||||
self.get_action_in_mode(&self.keybindings.edit, key, modifiers)
|
||||
.or_else(|| self.get_action_in_mode(&self.keybindings.global, key, modifiers))
|
||||
}
|
||||
|
||||
/// Get action for key in suggestions mode
|
||||
pub fn get_suggestion_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
|
||||
self.get_action_in_mode(&self.keybindings.suggestions, key, modifiers)
|
||||
}
|
||||
|
||||
/// Get action for key (mode-aware)
|
||||
pub fn get_action_for_key(&self, key: KeyCode, modifiers: KeyModifiers, is_edit_mode: bool, has_suggestions: bool) -> Option<&str> {
|
||||
// Suggestions take priority when active
|
||||
if has_suggestions {
|
||||
if let Some(action) = self.get_suggestion_action(key, modifiers) {
|
||||
return Some(action);
|
||||
}
|
||||
}
|
||||
|
||||
// Then check mode-specific
|
||||
if is_edit_mode {
|
||||
self.get_edit_action(key, modifiers)
|
||||
} else {
|
||||
self.get_read_only_action(key, modifiers)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_action_in_mode<'a>(&self, mode_bindings: &'a HashMap<String, Vec<String>>, key: KeyCode, modifiers: KeyModifiers) -> Option<&'a str> {
|
||||
for (action, bindings) in mode_bindings {
|
||||
for binding in bindings {
|
||||
if self.matches_keybinding(binding, key, modifiers) {
|
||||
return Some(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn matches_keybinding(&self, binding: &str, key: KeyCode, modifiers: KeyModifiers) -> bool {
|
||||
// Special handling for shift+character combinations
|
||||
if binding.to_lowercase().starts_with("shift+") {
|
||||
let parts: Vec<&str> = binding.split('+').collect();
|
||||
if parts.len() == 2 && parts[1].len() == 1 {
|
||||
let expected_lowercase = parts[1].chars().next().unwrap().to_lowercase().next().unwrap();
|
||||
let expected_uppercase = expected_lowercase.to_uppercase().next().unwrap();
|
||||
if let KeyCode::Char(actual_char) = key {
|
||||
if actual_char == expected_uppercase && modifiers.contains(KeyModifiers::SHIFT) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle Shift+Tab -> BackTab
|
||||
if binding.to_lowercase() == "shift+tab" && key == KeyCode::BackTab && modifiers.is_empty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Handle multi-character bindings (all standard keys without modifiers)
|
||||
if binding.len() > 1 && !binding.contains('+') {
|
||||
return match binding.to_lowercase().as_str() {
|
||||
// Navigation keys
|
||||
"left" => key == KeyCode::Left,
|
||||
"right" => key == KeyCode::Right,
|
||||
"up" => key == KeyCode::Up,
|
||||
"down" => key == KeyCode::Down,
|
||||
"home" => key == KeyCode::Home,
|
||||
"end" => key == KeyCode::End,
|
||||
"pageup" | "pgup" => key == KeyCode::PageUp,
|
||||
"pagedown" | "pgdn" => key == KeyCode::PageDown,
|
||||
|
||||
// Editing keys
|
||||
"insert" | "ins" => key == KeyCode::Insert,
|
||||
"delete" | "del" => key == KeyCode::Delete,
|
||||
"backspace" => key == KeyCode::Backspace,
|
||||
|
||||
// Tab keys
|
||||
"tab" => key == KeyCode::Tab,
|
||||
"backtab" => key == KeyCode::BackTab,
|
||||
|
||||
// Special keys
|
||||
"enter" | "return" => key == KeyCode::Enter,
|
||||
"escape" | "esc" => key == KeyCode::Esc,
|
||||
"space" => key == KeyCode::Char(' '),
|
||||
|
||||
// Function keys F1-F24
|
||||
"f1" => key == KeyCode::F(1),
|
||||
"f2" => key == KeyCode::F(2),
|
||||
"f3" => key == KeyCode::F(3),
|
||||
"f4" => key == KeyCode::F(4),
|
||||
"f5" => key == KeyCode::F(5),
|
||||
"f6" => key == KeyCode::F(6),
|
||||
"f7" => key == KeyCode::F(7),
|
||||
"f8" => key == KeyCode::F(8),
|
||||
"f9" => key == KeyCode::F(9),
|
||||
"f10" => key == KeyCode::F(10),
|
||||
"f11" => key == KeyCode::F(11),
|
||||
"f12" => key == KeyCode::F(12),
|
||||
"f13" => key == KeyCode::F(13),
|
||||
"f14" => key == KeyCode::F(14),
|
||||
"f15" => key == KeyCode::F(15),
|
||||
"f16" => key == KeyCode::F(16),
|
||||
"f17" => key == KeyCode::F(17),
|
||||
"f18" => key == KeyCode::F(18),
|
||||
"f19" => key == KeyCode::F(19),
|
||||
"f20" => key == KeyCode::F(20),
|
||||
"f21" => key == KeyCode::F(21),
|
||||
"f22" => key == KeyCode::F(22),
|
||||
"f23" => key == KeyCode::F(23),
|
||||
"f24" => key == KeyCode::F(24),
|
||||
|
||||
// Lock keys (may not work reliably in all terminals)
|
||||
"capslock" => key == KeyCode::CapsLock,
|
||||
"scrolllock" => key == KeyCode::ScrollLock,
|
||||
"numlock" => key == KeyCode::NumLock,
|
||||
|
||||
// System keys
|
||||
"printscreen" => key == KeyCode::PrintScreen,
|
||||
"pause" => key == KeyCode::Pause,
|
||||
"menu" => key == KeyCode::Menu,
|
||||
"keypadbegin" => key == KeyCode::KeypadBegin,
|
||||
|
||||
// Media keys (rarely supported but included for completeness)
|
||||
"mediaplay" => key == KeyCode::Media(crossterm::event::MediaKeyCode::Play),
|
||||
"mediapause" => key == KeyCode::Media(crossterm::event::MediaKeyCode::Pause),
|
||||
"mediaplaypause" => key == KeyCode::Media(crossterm::event::MediaKeyCode::PlayPause),
|
||||
"mediareverse" => key == KeyCode::Media(crossterm::event::MediaKeyCode::Reverse),
|
||||
"mediastop" => key == KeyCode::Media(crossterm::event::MediaKeyCode::Stop),
|
||||
"mediafastforward" => key == KeyCode::Media(crossterm::event::MediaKeyCode::FastForward),
|
||||
"mediarewind" => key == KeyCode::Media(crossterm::event::MediaKeyCode::Rewind),
|
||||
"mediatracknext" => key == KeyCode::Media(crossterm::event::MediaKeyCode::TrackNext),
|
||||
"mediatrackprevious" => key == KeyCode::Media(crossterm::event::MediaKeyCode::TrackPrevious),
|
||||
"mediarecord" => key == KeyCode::Media(crossterm::event::MediaKeyCode::Record),
|
||||
"medialowervolume" => key == KeyCode::Media(crossterm::event::MediaKeyCode::LowerVolume),
|
||||
"mediaraisevolume" => key == KeyCode::Media(crossterm::event::MediaKeyCode::RaiseVolume),
|
||||
"mediamutevolume" => key == KeyCode::Media(crossterm::event::MediaKeyCode::MuteVolume),
|
||||
|
||||
// Modifier keys (these work better as part of combinations)
|
||||
"leftshift" => key == KeyCode::Modifier(crossterm::event::ModifierKeyCode::LeftShift),
|
||||
"leftcontrol" | "leftctrl" => key == KeyCode::Modifier(crossterm::event::ModifierKeyCode::LeftControl),
|
||||
"leftalt" => key == KeyCode::Modifier(crossterm::event::ModifierKeyCode::LeftAlt),
|
||||
"leftsuper" | "leftwindows" | "leftcmd" => key == KeyCode::Modifier(crossterm::event::ModifierKeyCode::LeftSuper),
|
||||
"lefthyper" => key == KeyCode::Modifier(crossterm::event::ModifierKeyCode::LeftHyper),
|
||||
"leftmeta" => key == KeyCode::Modifier(crossterm::event::ModifierKeyCode::LeftMeta),
|
||||
"rightshift" => key == KeyCode::Modifier(crossterm::event::ModifierKeyCode::RightShift),
|
||||
"rightcontrol" | "rightctrl" => key == KeyCode::Modifier(crossterm::event::ModifierKeyCode::RightControl),
|
||||
"rightalt" => key == KeyCode::Modifier(crossterm::event::ModifierKeyCode::RightAlt),
|
||||
"rightsuper" | "rightwindows" | "rightcmd" => key == KeyCode::Modifier(crossterm::event::ModifierKeyCode::RightSuper),
|
||||
"righthyper" => key == KeyCode::Modifier(crossterm::event::ModifierKeyCode::RightHyper),
|
||||
"rightmeta" => key == KeyCode::Modifier(crossterm::event::ModifierKeyCode::RightMeta),
|
||||
"isolevel3shift" => key == KeyCode::Modifier(crossterm::event::ModifierKeyCode::IsoLevel3Shift),
|
||||
"isolevel5shift" => key == KeyCode::Modifier(crossterm::event::ModifierKeyCode::IsoLevel5Shift),
|
||||
|
||||
// Multi-key sequences need special handling
|
||||
"gg" => false, // This needs sequence handling
|
||||
_ => {
|
||||
// Handle single characters and punctuation
|
||||
if binding.len() == 1 {
|
||||
if let Some(c) = binding.chars().next() {
|
||||
key == KeyCode::Char(c)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Handle modifier combinations (like "Ctrl+F5", "Alt+Shift+A")
|
||||
let parts: Vec<&str> = binding.split('+').collect();
|
||||
let mut expected_modifiers = KeyModifiers::empty();
|
||||
let mut expected_key = None;
|
||||
|
||||
for part in parts {
|
||||
match part.to_lowercase().as_str() {
|
||||
// Modifiers
|
||||
"ctrl" | "control" => expected_modifiers |= KeyModifiers::CONTROL,
|
||||
"shift" => expected_modifiers |= KeyModifiers::SHIFT,
|
||||
"alt" => expected_modifiers |= KeyModifiers::ALT,
|
||||
"super" | "windows" | "cmd" => expected_modifiers |= KeyModifiers::SUPER,
|
||||
"hyper" => expected_modifiers |= KeyModifiers::HYPER,
|
||||
"meta" => expected_modifiers |= KeyModifiers::META,
|
||||
|
||||
// Navigation keys
|
||||
"left" => expected_key = Some(KeyCode::Left),
|
||||
"right" => expected_key = Some(KeyCode::Right),
|
||||
"up" => expected_key = Some(KeyCode::Up),
|
||||
"down" => expected_key = Some(KeyCode::Down),
|
||||
"home" => expected_key = Some(KeyCode::Home),
|
||||
"end" => expected_key = Some(KeyCode::End),
|
||||
"pageup" | "pgup" => expected_key = Some(KeyCode::PageUp),
|
||||
"pagedown" | "pgdn" => expected_key = Some(KeyCode::PageDown),
|
||||
|
||||
// Editing keys
|
||||
"insert" | "ins" => expected_key = Some(KeyCode::Insert),
|
||||
"delete" | "del" => expected_key = Some(KeyCode::Delete),
|
||||
"backspace" => expected_key = Some(KeyCode::Backspace),
|
||||
|
||||
// Tab keys
|
||||
"tab" => expected_key = Some(KeyCode::Tab),
|
||||
"backtab" => expected_key = Some(KeyCode::BackTab),
|
||||
|
||||
// Special keys
|
||||
"enter" | "return" => expected_key = Some(KeyCode::Enter),
|
||||
"escape" | "esc" => expected_key = Some(KeyCode::Esc),
|
||||
"space" => expected_key = Some(KeyCode::Char(' ')),
|
||||
|
||||
// Function keys
|
||||
"f1" => expected_key = Some(KeyCode::F(1)),
|
||||
"f2" => expected_key = Some(KeyCode::F(2)),
|
||||
"f3" => expected_key = Some(KeyCode::F(3)),
|
||||
"f4" => expected_key = Some(KeyCode::F(4)),
|
||||
"f5" => expected_key = Some(KeyCode::F(5)),
|
||||
"f6" => expected_key = Some(KeyCode::F(6)),
|
||||
"f7" => expected_key = Some(KeyCode::F(7)),
|
||||
"f8" => expected_key = Some(KeyCode::F(8)),
|
||||
"f9" => expected_key = Some(KeyCode::F(9)),
|
||||
"f10" => expected_key = Some(KeyCode::F(10)),
|
||||
"f11" => expected_key = Some(KeyCode::F(11)),
|
||||
"f12" => expected_key = Some(KeyCode::F(12)),
|
||||
"f13" => expected_key = Some(KeyCode::F(13)),
|
||||
"f14" => expected_key = Some(KeyCode::F(14)),
|
||||
"f15" => expected_key = Some(KeyCode::F(15)),
|
||||
"f16" => expected_key = Some(KeyCode::F(16)),
|
||||
"f17" => expected_key = Some(KeyCode::F(17)),
|
||||
"f18" => expected_key = Some(KeyCode::F(18)),
|
||||
"f19" => expected_key = Some(KeyCode::F(19)),
|
||||
"f20" => expected_key = Some(KeyCode::F(20)),
|
||||
"f21" => expected_key = Some(KeyCode::F(21)),
|
||||
"f22" => expected_key = Some(KeyCode::F(22)),
|
||||
"f23" => expected_key = Some(KeyCode::F(23)),
|
||||
"f24" => expected_key = Some(KeyCode::F(24)),
|
||||
|
||||
// Lock keys
|
||||
"capslock" => expected_key = Some(KeyCode::CapsLock),
|
||||
"scrolllock" => expected_key = Some(KeyCode::ScrollLock),
|
||||
"numlock" => expected_key = Some(KeyCode::NumLock),
|
||||
|
||||
// System keys
|
||||
"printscreen" => expected_key = Some(KeyCode::PrintScreen),
|
||||
"pause" => expected_key = Some(KeyCode::Pause),
|
||||
"menu" => expected_key = Some(KeyCode::Menu),
|
||||
"keypadbegin" => expected_key = Some(KeyCode::KeypadBegin),
|
||||
|
||||
// Single character (letters, numbers, punctuation)
|
||||
part => {
|
||||
if part.len() == 1 {
|
||||
if let Some(c) = part.chars().next() {
|
||||
expected_key = Some(KeyCode::Char(c));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
modifiers == expected_modifiers && Some(key) == expected_key
|
||||
}
|
||||
|
||||
/// Convenience method to create vim preset
|
||||
pub fn vim_preset() -> Self {
|
||||
Self {
|
||||
keybindings: CanvasKeybindings::with_vim_defaults(),
|
||||
behavior: CanvasBehavior::default(),
|
||||
appearance: CanvasAppearance::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience method to create emacs preset
|
||||
pub fn emacs_preset() -> Self {
|
||||
Self {
|
||||
keybindings: CanvasKeybindings::with_emacs_defaults(),
|
||||
behavior: CanvasBehavior::default(),
|
||||
appearance: CanvasAppearance::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Debug method to print loaded keybindings
|
||||
pub fn debug_keybindings(&self) {
|
||||
println!("📋 Canvas keybindings loaded:");
|
||||
println!(" Read-only: {} actions", self.keybindings.read_only.len());
|
||||
println!(" Edit: {} actions", self.keybindings.edit.len());
|
||||
println!(" Suggestions: {} actions", self.keybindings.suggestions.len());
|
||||
println!(" Global: {} actions", self.keybindings.global.len());
|
||||
}
|
||||
}
|
||||
|
||||
// Re-export for convenience
|
||||
pub use crate::canvas::actions::CanvasAction;
|
||||
pub use crate::dispatcher::ActionDispatcher;
|
||||
363
canvas/src/config/config.rs
Normal file
363
canvas/src/config/config.rs
Normal file
@@ -0,0 +1,363 @@
|
||||
// canvas/src/config.rs
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use crossterm::event::{KeyCode, KeyModifiers};
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
use super::registry::{ActionRegistry, ActionSpec, ModeRegistry};
|
||||
use super::validation::{ConfigValidator, ValidationError, ValidationResult, ValidationWarning};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CanvasConfig {
|
||||
#[serde(default)]
|
||||
pub keybindings: CanvasKeybindings,
|
||||
#[serde(default)]
|
||||
pub behavior: CanvasBehavior,
|
||||
#[serde(default)]
|
||||
pub appearance: CanvasAppearance,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct CanvasKeybindings {
|
||||
#[serde(default)]
|
||||
pub read_only: HashMap<String, Vec<String>>,
|
||||
#[serde(default)]
|
||||
pub edit: HashMap<String, Vec<String>>,
|
||||
#[serde(default)]
|
||||
pub suggestions: HashMap<String, Vec<String>>,
|
||||
#[serde(default)]
|
||||
pub global: HashMap<String, Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CanvasBehavior {
|
||||
#[serde(default = "default_wrap_around")]
|
||||
pub wrap_around_fields: bool,
|
||||
#[serde(default = "default_auto_save")]
|
||||
pub auto_save_on_field_change: bool,
|
||||
#[serde(default = "default_word_chars")]
|
||||
pub word_chars: String,
|
||||
#[serde(default = "default_suggestion_limit")]
|
||||
pub max_suggestions: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CanvasAppearance {
|
||||
#[serde(default = "default_cursor_style")]
|
||||
pub cursor_style: String, // "block", "bar", "underline"
|
||||
#[serde(default = "default_show_field_numbers")]
|
||||
pub show_field_numbers: bool,
|
||||
#[serde(default = "default_highlight_current_field")]
|
||||
pub highlight_current_field: bool,
|
||||
}
|
||||
|
||||
// Default values
|
||||
fn default_wrap_around() -> bool { true }
|
||||
fn default_auto_save() -> bool { false }
|
||||
fn default_word_chars() -> String { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_".to_string() }
|
||||
fn default_suggestion_limit() -> usize { 10 }
|
||||
fn default_cursor_style() -> String { "block".to_string() }
|
||||
fn default_show_field_numbers() -> bool { false }
|
||||
fn default_highlight_current_field() -> bool { true }
|
||||
|
||||
impl Default for CanvasBehavior {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
wrap_around_fields: default_wrap_around(),
|
||||
auto_save_on_field_change: default_auto_save(),
|
||||
word_chars: default_word_chars(),
|
||||
max_suggestions: default_suggestion_limit(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CanvasAppearance {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
cursor_style: default_cursor_style(),
|
||||
show_field_numbers: default_show_field_numbers(),
|
||||
highlight_current_field: default_highlight_current_field(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CanvasConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
keybindings: CanvasKeybindings::with_vim_defaults(),
|
||||
behavior: CanvasBehavior::default(),
|
||||
appearance: CanvasAppearance::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CanvasKeybindings {
|
||||
pub fn with_vim_defaults() -> Self {
|
||||
let mut keybindings = Self::default();
|
||||
|
||||
// Read-only mode (vim-style navigation)
|
||||
keybindings.read_only.insert("move_left".to_string(), vec!["h".to_string()]);
|
||||
keybindings.read_only.insert("move_right".to_string(), vec!["l".to_string()]);
|
||||
keybindings.read_only.insert("move_up".to_string(), vec!["k".to_string()]);
|
||||
keybindings.read_only.insert("move_down".to_string(), vec!["j".to_string()]);
|
||||
keybindings.read_only.insert("move_word_next".to_string(), vec!["w".to_string()]);
|
||||
keybindings.read_only.insert("move_word_end".to_string(), vec!["e".to_string()]);
|
||||
keybindings.read_only.insert("move_word_prev".to_string(), vec!["b".to_string()]);
|
||||
keybindings.read_only.insert("move_word_end_prev".to_string(), vec!["ge".to_string()]);
|
||||
keybindings.read_only.insert("move_line_start".to_string(), vec!["0".to_string()]);
|
||||
keybindings.read_only.insert("move_line_end".to_string(), vec!["$".to_string()]);
|
||||
keybindings.read_only.insert("move_first_line".to_string(), vec!["gg".to_string()]);
|
||||
keybindings.read_only.insert("move_last_line".to_string(), vec!["G".to_string()]);
|
||||
keybindings.read_only.insert("next_field".to_string(), vec!["Tab".to_string()]);
|
||||
keybindings.read_only.insert("prev_field".to_string(), vec!["Shift+Tab".to_string()]);
|
||||
|
||||
// Edit mode
|
||||
keybindings.edit.insert("delete_char_backward".to_string(), vec!["Backspace".to_string()]);
|
||||
keybindings.edit.insert("delete_char_forward".to_string(), vec!["Delete".to_string()]);
|
||||
keybindings.edit.insert("move_left".to_string(), vec!["Left".to_string()]);
|
||||
keybindings.edit.insert("move_right".to_string(), vec!["Right".to_string()]);
|
||||
keybindings.edit.insert("move_up".to_string(), vec!["Up".to_string()]);
|
||||
keybindings.edit.insert("move_down".to_string(), vec!["Down".to_string()]);
|
||||
keybindings.edit.insert("move_line_start".to_string(), vec!["Home".to_string()]);
|
||||
keybindings.edit.insert("move_line_end".to_string(), vec!["End".to_string()]);
|
||||
keybindings.edit.insert("move_word_next".to_string(), vec!["Ctrl+Right".to_string()]);
|
||||
keybindings.edit.insert("move_word_prev".to_string(), vec!["Ctrl+Left".to_string()]);
|
||||
keybindings.edit.insert("next_field".to_string(), vec!["Tab".to_string()]);
|
||||
keybindings.edit.insert("prev_field".to_string(), vec!["Shift+Tab".to_string()]);
|
||||
|
||||
// Suggestions
|
||||
keybindings.suggestions.insert("suggestion_up".to_string(), vec!["Up".to_string(), "Ctrl+p".to_string()]);
|
||||
keybindings.suggestions.insert("suggestion_down".to_string(), vec!["Down".to_string(), "Ctrl+n".to_string()]);
|
||||
keybindings.suggestions.insert("select_suggestion".to_string(), vec!["Enter".to_string(), "Tab".to_string()]);
|
||||
keybindings.suggestions.insert("exit_suggestions".to_string(), vec!["Esc".to_string()]);
|
||||
|
||||
// Global (works in both modes)
|
||||
keybindings.global.insert("move_up".to_string(), vec!["Up".to_string()]);
|
||||
keybindings.global.insert("move_down".to_string(), vec!["Down".to_string()]);
|
||||
|
||||
keybindings
|
||||
}
|
||||
|
||||
pub fn with_emacs_defaults() -> Self {
|
||||
let mut keybindings = Self::default();
|
||||
|
||||
// Emacs-style bindings
|
||||
keybindings.read_only.insert("move_left".to_string(), vec!["Ctrl+b".to_string()]);
|
||||
keybindings.read_only.insert("move_right".to_string(), vec!["Ctrl+f".to_string()]);
|
||||
keybindings.read_only.insert("move_up".to_string(), vec!["Ctrl+p".to_string()]);
|
||||
keybindings.read_only.insert("move_down".to_string(), vec!["Ctrl+n".to_string()]);
|
||||
keybindings.read_only.insert("move_word_next".to_string(), vec!["Alt+f".to_string()]);
|
||||
keybindings.read_only.insert("move_word_prev".to_string(), vec!["Alt+b".to_string()]);
|
||||
keybindings.read_only.insert("move_line_start".to_string(), vec!["Ctrl+a".to_string()]);
|
||||
keybindings.read_only.insert("move_line_end".to_string(), vec!["Ctrl+e".to_string()]);
|
||||
|
||||
keybindings.edit.insert("delete_char_backward".to_string(), vec!["Ctrl+h".to_string(), "Backspace".to_string()]);
|
||||
keybindings.edit.insert("delete_char_forward".to_string(), vec!["Ctrl+d".to_string(), "Delete".to_string()]);
|
||||
|
||||
keybindings
|
||||
}
|
||||
}
|
||||
|
||||
impl CanvasConfig {
|
||||
/// NEW: Load and validate configuration
|
||||
pub fn load() -> Self {
|
||||
match Self::load_and_validate() {
|
||||
Ok(config) => config,
|
||||
Err(e) => {
|
||||
eprintln!("⚠️ Canvas config validation failed: {}", e);
|
||||
eprintln!(" Using vim defaults. Run CanvasConfig::generate_template() for help.");
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// NEW: Load configuration with validation
|
||||
pub fn load_and_validate() -> Result<Self> {
|
||||
// Try to load canvas_config.toml from current directory
|
||||
let config = if let Ok(config) = Self::from_file(std::path::Path::new("canvas_config.toml")) {
|
||||
config
|
||||
} else {
|
||||
// Fallback to vim defaults
|
||||
Self::default()
|
||||
};
|
||||
|
||||
// Validate the configuration
|
||||
let validator = ConfigValidator::new();
|
||||
let validation_result = validator.validate_keybindings(&config.keybindings);
|
||||
|
||||
if !validation_result.is_valid {
|
||||
// Print validation errors
|
||||
validator.print_validation_result(&validation_result);
|
||||
|
||||
// Create error with suggestions
|
||||
let error_msg = format!(
|
||||
"Configuration validation failed with {} errors",
|
||||
validation_result.errors.len()
|
||||
);
|
||||
return Err(anyhow::anyhow!(error_msg));
|
||||
}
|
||||
|
||||
// Print warnings if any
|
||||
if !validation_result.warnings.is_empty() {
|
||||
validator.print_validation_result(&validation_result);
|
||||
}
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
/// NEW: Generate a complete configuration template
|
||||
pub fn generate_template() -> String {
|
||||
let registry = ActionRegistry::new();
|
||||
registry.generate_config_template()
|
||||
}
|
||||
|
||||
/// NEW: Generate a clean, minimal configuration template
|
||||
pub fn generate_clean_template() -> String {
|
||||
let registry = ActionRegistry::new();
|
||||
registry.generate_clean_template()
|
||||
}
|
||||
|
||||
/// NEW: Validate current configuration
|
||||
pub fn validate(&self) -> ValidationResult {
|
||||
let validator = ConfigValidator::new();
|
||||
validator.validate_keybindings(&self.keybindings)
|
||||
}
|
||||
|
||||
/// NEW: Print validation results for current config
|
||||
pub fn print_validation(&self) {
|
||||
let validator = ConfigValidator::new();
|
||||
let result = validator.validate_keybindings(&self.keybindings);
|
||||
validator.print_validation_result(&result);
|
||||
}
|
||||
|
||||
/// NEW: Generate config for missing required actions
|
||||
pub fn generate_missing_config(&self) -> String {
|
||||
let validator = ConfigValidator::new();
|
||||
validator.generate_missing_config(&self.keybindings)
|
||||
}
|
||||
|
||||
/// Load from TOML string
|
||||
pub fn from_toml(toml_str: &str) -> Result<Self> {
|
||||
toml::from_str(toml_str)
|
||||
.with_context(|| "Failed to parse canvas config TOML")
|
||||
}
|
||||
|
||||
/// Load from file
|
||||
pub fn from_file(path: &std::path::Path) -> Result<Self> {
|
||||
let contents = std::fs::read_to_string(path)
|
||||
.with_context(|| format!("Failed to read config file: {:?}", path))?;
|
||||
Self::from_toml(&contents)
|
||||
}
|
||||
|
||||
/// NEW: Check if autocomplete should auto-trigger (simple logic)
|
||||
pub fn should_auto_trigger_autocomplete(&self) -> bool {
|
||||
// If trigger_autocomplete keybinding exists anywhere, use manual mode only
|
||||
// If no trigger_autocomplete keybinding, use auto-trigger mode
|
||||
!self.has_trigger_autocomplete_keybinding()
|
||||
}
|
||||
|
||||
/// NEW: Check if user has configured manual trigger keybinding
|
||||
pub fn has_trigger_autocomplete_keybinding(&self) -> bool {
|
||||
self.keybindings.edit.contains_key("trigger_autocomplete") ||
|
||||
self.keybindings.read_only.contains_key("trigger_autocomplete") ||
|
||||
self.keybindings.global.contains_key("trigger_autocomplete")
|
||||
}
|
||||
|
||||
// ... rest of your existing methods stay the same ...
|
||||
|
||||
/// Get action for key in read-only mode
|
||||
pub fn get_read_only_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
|
||||
self.get_action_in_mode(&self.keybindings.read_only, key, modifiers)
|
||||
.or_else(|| self.get_action_in_mode(&self.keybindings.global, key, modifiers))
|
||||
}
|
||||
|
||||
/// Get action for key in edit mode
|
||||
pub fn get_edit_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
|
||||
self.get_action_in_mode(&self.keybindings.edit, key, modifiers)
|
||||
.or_else(|| self.get_action_in_mode(&self.keybindings.global, key, modifiers))
|
||||
}
|
||||
|
||||
/// Get action for key in suggestions mode
|
||||
pub fn get_suggestion_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
|
||||
self.get_action_in_mode(&self.keybindings.suggestions, key, modifiers)
|
||||
}
|
||||
|
||||
/// Get action for key (mode-aware)
|
||||
pub fn get_action_for_key(&self, key: KeyCode, modifiers: KeyModifiers, is_edit_mode: bool, has_suggestions: bool) -> Option<&str> {
|
||||
// Suggestions take priority when active
|
||||
if has_suggestions {
|
||||
if let Some(action) = self.get_suggestion_action(key, modifiers) {
|
||||
return Some(action);
|
||||
}
|
||||
}
|
||||
|
||||
// Then check mode-specific
|
||||
if is_edit_mode {
|
||||
self.get_edit_action(key, modifiers)
|
||||
} else {
|
||||
self.get_read_only_action(key, modifiers)
|
||||
}
|
||||
}
|
||||
|
||||
// ... keep all your existing private methods ...
|
||||
fn get_action_in_mode<'a>(&self, mode_bindings: &'a HashMap<String, Vec<String>>, key: KeyCode, modifiers: KeyModifiers) -> Option<&'a str> {
|
||||
for (action, bindings) in mode_bindings {
|
||||
for binding in bindings {
|
||||
if self.matches_keybinding(binding, key, modifiers) {
|
||||
return Some(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn matches_keybinding(&self, binding: &str, key: KeyCode, modifiers: KeyModifiers) -> bool {
|
||||
// ... keep all your existing key matching logic ...
|
||||
// (This is a very long method, so I'm just indicating to keep it as-is)
|
||||
|
||||
// Your existing implementation here...
|
||||
true // placeholder - use your actual implementation
|
||||
}
|
||||
|
||||
/// Convenience method to create vim preset
|
||||
pub fn vim_preset() -> Self {
|
||||
Self {
|
||||
keybindings: CanvasKeybindings::with_vim_defaults(),
|
||||
behavior: CanvasBehavior::default(),
|
||||
appearance: CanvasAppearance::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience method to create emacs preset
|
||||
pub fn emacs_preset() -> Self {
|
||||
Self {
|
||||
keybindings: CanvasKeybindings::with_emacs_defaults(),
|
||||
behavior: CanvasBehavior::default(),
|
||||
appearance: CanvasAppearance::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Debug method to print loaded keybindings
|
||||
pub fn debug_keybindings(&self) {
|
||||
println!("📋 Canvas keybindings loaded:");
|
||||
println!(" Read-only: {} actions", self.keybindings.read_only.len());
|
||||
println!(" Edit: {} actions", self.keybindings.edit.len());
|
||||
println!(" Suggestions: {} actions", self.keybindings.suggestions.len());
|
||||
println!(" Global: {} actions", self.keybindings.global.len());
|
||||
|
||||
// NEW: Show validation status
|
||||
let validation = self.validate();
|
||||
if validation.is_valid {
|
||||
println!(" ✅ Configuration is valid");
|
||||
} else {
|
||||
println!(" ❌ Configuration has {} errors", validation.errors.len());
|
||||
}
|
||||
if !validation.warnings.is_empty() {
|
||||
println!(" ⚠️ Configuration has {} warnings", validation.warnings.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Re-export for convenience
|
||||
pub use crate::canvas::actions::CanvasAction;
|
||||
pub use crate::dispatcher::ActionDispatcher;
|
||||
10
canvas/src/config/mod.rs
Normal file
10
canvas/src/config/mod.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
// src/config/mod.rs
|
||||
|
||||
mod registry;
|
||||
mod config;
|
||||
mod validation;
|
||||
|
||||
// Re-export everything from the main config module
|
||||
pub use registry::*;
|
||||
pub use validation::*;
|
||||
pub use config::*;
|
||||
451
canvas/src/config/registry.rs
Normal file
451
canvas/src/config/registry.rs
Normal file
@@ -0,0 +1,451 @@
|
||||
// src/config/registry.rs
|
||||
|
||||
use std::collections::HashMap;
|
||||
use crate::canvas::modes::AppMode;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ActionSpec {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub examples: Vec<String>,
|
||||
pub mode_specific: bool, // true if different behavior per mode
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ModeRegistry {
|
||||
pub required: HashMap<String, ActionSpec>,
|
||||
pub optional: HashMap<String, ActionSpec>,
|
||||
pub auto_handled: Vec<String>, // Never appear in config
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ActionRegistry {
|
||||
pub edit_mode: ModeRegistry,
|
||||
pub readonly_mode: ModeRegistry,
|
||||
pub suggestions: ModeRegistry,
|
||||
pub global: ModeRegistry,
|
||||
}
|
||||
|
||||
impl ActionRegistry {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
edit_mode: Self::edit_mode_registry(),
|
||||
readonly_mode: Self::readonly_mode_registry(),
|
||||
suggestions: Self::suggestions_registry(),
|
||||
global: Self::global_registry(),
|
||||
}
|
||||
}
|
||||
|
||||
fn edit_mode_registry() -> ModeRegistry {
|
||||
let mut required = HashMap::new();
|
||||
let mut optional = HashMap::new();
|
||||
|
||||
// REQUIRED - These MUST be configured
|
||||
required.insert("move_left".to_string(), ActionSpec {
|
||||
name: "move_left".to_string(),
|
||||
description: "Move cursor one position to the left".to_string(),
|
||||
examples: vec!["Left".to_string(), "h".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
required.insert("move_right".to_string(), ActionSpec {
|
||||
name: "move_right".to_string(),
|
||||
description: "Move cursor one position to the right".to_string(),
|
||||
examples: vec!["Right".to_string(), "l".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
required.insert("move_up".to_string(), ActionSpec {
|
||||
name: "move_up".to_string(),
|
||||
description: "Move to previous field or line".to_string(),
|
||||
examples: vec!["Up".to_string(), "k".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
required.insert("move_down".to_string(), ActionSpec {
|
||||
name: "move_down".to_string(),
|
||||
description: "Move to next field or line".to_string(),
|
||||
examples: vec!["Down".to_string(), "j".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
required.insert("delete_char_backward".to_string(), ActionSpec {
|
||||
name: "delete_char_backward".to_string(),
|
||||
description: "Delete character before cursor".to_string(),
|
||||
examples: vec!["Backspace".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
required.insert("next_field".to_string(), ActionSpec {
|
||||
name: "next_field".to_string(),
|
||||
description: "Move to next input field".to_string(),
|
||||
examples: vec!["Tab".to_string(), "Enter".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
required.insert("prev_field".to_string(), ActionSpec {
|
||||
name: "prev_field".to_string(),
|
||||
description: "Move to previous input field".to_string(),
|
||||
examples: vec!["Shift+Tab".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
// OPTIONAL - These can be configured or omitted
|
||||
optional.insert("move_word_next".to_string(), ActionSpec {
|
||||
name: "move_word_next".to_string(),
|
||||
description: "Move cursor to start of next word".to_string(),
|
||||
examples: vec!["Ctrl+Right".to_string(), "w".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
optional.insert("move_word_prev".to_string(), ActionSpec {
|
||||
name: "move_word_prev".to_string(),
|
||||
description: "Move cursor to start of previous word".to_string(),
|
||||
examples: vec!["Ctrl+Left".to_string(), "b".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
optional.insert("move_word_end".to_string(), ActionSpec {
|
||||
name: "move_word_end".to_string(),
|
||||
description: "Move cursor to end of current/next word".to_string(),
|
||||
examples: vec!["e".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
optional.insert("move_word_end_prev".to_string(), ActionSpec {
|
||||
name: "move_word_end_prev".to_string(),
|
||||
description: "Move cursor to end of previous word".to_string(),
|
||||
examples: vec!["ge".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
optional.insert("move_line_start".to_string(), ActionSpec {
|
||||
name: "move_line_start".to_string(),
|
||||
description: "Move cursor to beginning of line".to_string(),
|
||||
examples: vec!["Home".to_string(), "0".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
optional.insert("move_line_end".to_string(), ActionSpec {
|
||||
name: "move_line_end".to_string(),
|
||||
description: "Move cursor to end of line".to_string(),
|
||||
examples: vec!["End".to_string(), "$".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
optional.insert("move_first_line".to_string(), ActionSpec {
|
||||
name: "move_first_line".to_string(),
|
||||
description: "Move to first field".to_string(),
|
||||
examples: vec!["Ctrl+Home".to_string(), "gg".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
optional.insert("move_last_line".to_string(), ActionSpec {
|
||||
name: "move_last_line".to_string(),
|
||||
description: "Move to last field".to_string(),
|
||||
examples: vec!["Ctrl+End".to_string(), "G".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
optional.insert("delete_char_forward".to_string(), ActionSpec {
|
||||
name: "delete_char_forward".to_string(),
|
||||
description: "Delete character after cursor".to_string(),
|
||||
examples: vec!["Delete".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
ModeRegistry {
|
||||
required,
|
||||
optional,
|
||||
auto_handled: vec![
|
||||
"insert_char".to_string(), // Any printable character
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
fn readonly_mode_registry() -> ModeRegistry {
|
||||
let mut required = HashMap::new();
|
||||
let mut optional = HashMap::new();
|
||||
|
||||
// REQUIRED - Navigation is essential in read-only mode
|
||||
required.insert("move_left".to_string(), ActionSpec {
|
||||
name: "move_left".to_string(),
|
||||
description: "Move cursor one position to the left".to_string(),
|
||||
examples: vec!["h".to_string(), "Left".to_string()],
|
||||
mode_specific: true,
|
||||
});
|
||||
|
||||
required.insert("move_right".to_string(), ActionSpec {
|
||||
name: "move_right".to_string(),
|
||||
description: "Move cursor one position to the right".to_string(),
|
||||
examples: vec!["l".to_string(), "Right".to_string()],
|
||||
mode_specific: true,
|
||||
});
|
||||
|
||||
required.insert("move_up".to_string(), ActionSpec {
|
||||
name: "move_up".to_string(),
|
||||
description: "Move to previous field".to_string(),
|
||||
examples: vec!["k".to_string(), "Up".to_string()],
|
||||
mode_specific: true,
|
||||
});
|
||||
|
||||
required.insert("move_down".to_string(), ActionSpec {
|
||||
name: "move_down".to_string(),
|
||||
description: "Move to next field".to_string(),
|
||||
examples: vec!["j".to_string(), "Down".to_string()],
|
||||
mode_specific: true,
|
||||
});
|
||||
|
||||
// OPTIONAL - Advanced navigation
|
||||
optional.insert("move_word_next".to_string(), ActionSpec {
|
||||
name: "move_word_next".to_string(),
|
||||
description: "Move cursor to start of next word".to_string(),
|
||||
examples: vec!["w".to_string()],
|
||||
mode_specific: true,
|
||||
});
|
||||
|
||||
optional.insert("move_word_prev".to_string(), ActionSpec {
|
||||
name: "move_word_prev".to_string(),
|
||||
description: "Move cursor to start of previous word".to_string(),
|
||||
examples: vec!["b".to_string()],
|
||||
mode_specific: true,
|
||||
});
|
||||
|
||||
optional.insert("move_word_end".to_string(), ActionSpec {
|
||||
name: "move_word_end".to_string(),
|
||||
description: "Move cursor to end of current/next word".to_string(),
|
||||
examples: vec!["e".to_string()],
|
||||
mode_specific: true,
|
||||
});
|
||||
|
||||
optional.insert("move_word_end_prev".to_string(), ActionSpec {
|
||||
name: "move_word_end_prev".to_string(),
|
||||
description: "Move cursor to end of previous word".to_string(),
|
||||
examples: vec!["ge".to_string()],
|
||||
mode_specific: true,
|
||||
});
|
||||
|
||||
optional.insert("move_line_start".to_string(), ActionSpec {
|
||||
name: "move_line_start".to_string(),
|
||||
description: "Move cursor to beginning of line".to_string(),
|
||||
examples: vec!["0".to_string()],
|
||||
mode_specific: true,
|
||||
});
|
||||
|
||||
optional.insert("move_line_end".to_string(), ActionSpec {
|
||||
name: "move_line_end".to_string(),
|
||||
description: "Move cursor to end of line".to_string(),
|
||||
examples: vec!["$".to_string()],
|
||||
mode_specific: true,
|
||||
});
|
||||
|
||||
optional.insert("move_first_line".to_string(), ActionSpec {
|
||||
name: "move_first_line".to_string(),
|
||||
description: "Move to first field".to_string(),
|
||||
examples: vec!["gg".to_string()],
|
||||
mode_specific: true,
|
||||
});
|
||||
|
||||
optional.insert("move_last_line".to_string(), ActionSpec {
|
||||
name: "move_last_line".to_string(),
|
||||
description: "Move to last field".to_string(),
|
||||
examples: vec!["G".to_string()],
|
||||
mode_specific: true,
|
||||
});
|
||||
|
||||
optional.insert("next_field".to_string(), ActionSpec {
|
||||
name: "next_field".to_string(),
|
||||
description: "Move to next input field".to_string(),
|
||||
examples: vec!["Tab".to_string()],
|
||||
mode_specific: true,
|
||||
});
|
||||
|
||||
optional.insert("prev_field".to_string(), ActionSpec {
|
||||
name: "prev_field".to_string(),
|
||||
description: "Move to previous input field".to_string(),
|
||||
examples: vec!["Shift+Tab".to_string()],
|
||||
mode_specific: true,
|
||||
});
|
||||
|
||||
ModeRegistry {
|
||||
required,
|
||||
optional,
|
||||
auto_handled: vec![], // Read-only mode has no auto-handled actions
|
||||
}
|
||||
}
|
||||
|
||||
fn suggestions_registry() -> ModeRegistry {
|
||||
let mut required = HashMap::new();
|
||||
|
||||
// REQUIRED - Essential for suggestion navigation
|
||||
required.insert("suggestion_up".to_string(), ActionSpec {
|
||||
name: "suggestion_up".to_string(),
|
||||
description: "Move selection to previous suggestion".to_string(),
|
||||
examples: vec!["Up".to_string(), "Ctrl+p".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
required.insert("suggestion_down".to_string(), ActionSpec {
|
||||
name: "suggestion_down".to_string(),
|
||||
description: "Move selection to next suggestion".to_string(),
|
||||
examples: vec!["Down".to_string(), "Ctrl+n".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
required.insert("select_suggestion".to_string(), ActionSpec {
|
||||
name: "select_suggestion".to_string(),
|
||||
description: "Select the currently highlighted suggestion".to_string(),
|
||||
examples: vec!["Enter".to_string(), "Tab".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
required.insert("exit_suggestions".to_string(), ActionSpec {
|
||||
name: "exit_suggestions".to_string(),
|
||||
description: "Close suggestions without selecting".to_string(),
|
||||
examples: vec!["Esc".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
ModeRegistry {
|
||||
required,
|
||||
optional: HashMap::new(),
|
||||
auto_handled: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn global_registry() -> ModeRegistry {
|
||||
let mut optional = HashMap::new();
|
||||
|
||||
// OPTIONAL - Global overrides
|
||||
optional.insert("move_up".to_string(), ActionSpec {
|
||||
name: "move_up".to_string(),
|
||||
description: "Global override for up movement".to_string(),
|
||||
examples: vec!["Up".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
optional.insert("move_down".to_string(), ActionSpec {
|
||||
name: "move_down".to_string(),
|
||||
description: "Global override for down movement".to_string(),
|
||||
examples: vec!["Down".to_string()],
|
||||
mode_specific: false,
|
||||
});
|
||||
|
||||
ModeRegistry {
|
||||
required: HashMap::new(),
|
||||
optional,
|
||||
auto_handled: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mode_registry(&self, mode: &str) -> &ModeRegistry {
|
||||
match mode {
|
||||
"edit" => &self.edit_mode,
|
||||
"read_only" => &self.readonly_mode,
|
||||
"suggestions" => &self.suggestions,
|
||||
"global" => &self.global,
|
||||
_ => &self.global, // fallback
|
||||
}
|
||||
}
|
||||
|
||||
pub fn all_known_actions(&self) -> Vec<String> {
|
||||
let mut actions = Vec::new();
|
||||
|
||||
for registry in [&self.edit_mode, &self.readonly_mode, &self.suggestions, &self.global] {
|
||||
actions.extend(registry.required.keys().cloned());
|
||||
actions.extend(registry.optional.keys().cloned());
|
||||
}
|
||||
|
||||
actions.sort();
|
||||
actions.dedup();
|
||||
actions
|
||||
}
|
||||
|
||||
pub fn generate_config_template(&self) -> String {
|
||||
let mut template = String::new();
|
||||
template.push_str("# Canvas Library Configuration Template\n");
|
||||
template.push_str("# Generated automatically - customize as needed\n\n");
|
||||
|
||||
template.push_str("[keybindings.edit]\n");
|
||||
template.push_str("# REQUIRED ACTIONS - These must be configured\n");
|
||||
for (name, spec) in &self.edit_mode.required {
|
||||
template.push_str(&format!("# {}\n", spec.description));
|
||||
template.push_str(&format!("{} = {:?}\n\n", name, spec.examples));
|
||||
}
|
||||
|
||||
template.push_str("# OPTIONAL ACTIONS - Configure these if you want them enabled\n");
|
||||
for (name, spec) in &self.edit_mode.optional {
|
||||
template.push_str(&format!("# {}\n", spec.description));
|
||||
template.push_str(&format!("# {} = {:?}\n\n", name, spec.examples));
|
||||
}
|
||||
|
||||
template.push_str("[keybindings.read_only]\n");
|
||||
template.push_str("# REQUIRED ACTIONS - These must be configured\n");
|
||||
for (name, spec) in &self.readonly_mode.required {
|
||||
template.push_str(&format!("# {}\n", spec.description));
|
||||
template.push_str(&format!("{} = {:?}\n\n", name, spec.examples));
|
||||
}
|
||||
|
||||
template.push_str("# OPTIONAL ACTIONS - Configure these if you want them enabled\n");
|
||||
for (name, spec) in &self.readonly_mode.optional {
|
||||
template.push_str(&format!("# {}\n", spec.description));
|
||||
template.push_str(&format!("# {} = {:?}\n\n", name, spec.examples));
|
||||
}
|
||||
|
||||
template.push_str("[keybindings.suggestions]\n");
|
||||
template.push_str("# REQUIRED ACTIONS - These must be configured\n");
|
||||
for (name, spec) in &self.suggestions.required {
|
||||
template.push_str(&format!("# {}\n", spec.description));
|
||||
template.push_str(&format!("{} = {:?}\n\n", name, spec.examples));
|
||||
}
|
||||
|
||||
template
|
||||
}
|
||||
|
||||
pub fn generate_clean_template(&self) -> String {
|
||||
let mut template = String::new();
|
||||
|
||||
// Edit Mode
|
||||
template.push_str("[keybindings.edit]\n");
|
||||
template.push_str("# Required\n");
|
||||
for (name, spec) in &self.edit_mode.required {
|
||||
template.push_str(&format!("{} = {:?}\n", name, spec.examples));
|
||||
}
|
||||
template.push_str("# Optional\n");
|
||||
for (name, spec) in &self.edit_mode.optional {
|
||||
template.push_str(&format!("{} = {:?}\n", name, spec.examples));
|
||||
}
|
||||
template.push('\n');
|
||||
|
||||
// Read-Only Mode
|
||||
template.push_str("[keybindings.read_only]\n");
|
||||
template.push_str("# Required\n");
|
||||
for (name, spec) in &self.readonly_mode.required {
|
||||
template.push_str(&format!("{} = {:?}\n", name, spec.examples));
|
||||
}
|
||||
template.push_str("# Optional\n");
|
||||
for (name, spec) in &self.readonly_mode.optional {
|
||||
template.push_str(&format!("{} = {:?}\n", name, spec.examples));
|
||||
}
|
||||
template.push('\n');
|
||||
|
||||
// Suggestions Mode
|
||||
template.push_str("[keybindings.suggestions]\n");
|
||||
template.push_str("# Required\n");
|
||||
for (name, spec) in &self.suggestions.required {
|
||||
template.push_str(&format!("{} = {:?}\n", name, spec.examples));
|
||||
}
|
||||
template.push('\n');
|
||||
|
||||
// Global (all optional)
|
||||
if !self.global.optional.is_empty() {
|
||||
template.push_str("[keybindings.global]\n");
|
||||
template.push_str("# Optional\n");
|
||||
for (name, spec) in &self.global.optional {
|
||||
template.push_str(&format!("{} = {:?}\n", name, spec.examples));
|
||||
}
|
||||
}
|
||||
|
||||
template
|
||||
}
|
||||
}
|
||||
279
canvas/src/config/validation.rs
Normal file
279
canvas/src/config/validation.rs
Normal file
@@ -0,0 +1,279 @@
|
||||
// src/config/validation.rs
|
||||
|
||||
use std::collections::HashMap;
|
||||
use thiserror::Error;
|
||||
use crate::config::registry::{ActionRegistry, ModeRegistry};
|
||||
use crate::config::CanvasKeybindings;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ValidationError {
|
||||
#[error("Missing required action '{action}' in {mode} mode")]
|
||||
MissingRequired {
|
||||
action: String,
|
||||
mode: String,
|
||||
suggestion: String,
|
||||
},
|
||||
|
||||
#[error("Unknown action '{action}' in {mode} mode")]
|
||||
UnknownAction {
|
||||
action: String,
|
||||
mode: String,
|
||||
similar: Vec<String>,
|
||||
},
|
||||
|
||||
#[error("Multiple validation errors")]
|
||||
Multiple(Vec<ValidationError>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ValidationWarning {
|
||||
pub message: String,
|
||||
pub suggestion: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ValidationResult {
|
||||
pub errors: Vec<ValidationError>,
|
||||
pub warnings: Vec<ValidationWarning>,
|
||||
pub is_valid: bool,
|
||||
}
|
||||
|
||||
impl ValidationResult {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
errors: Vec::new(),
|
||||
warnings: Vec::new(),
|
||||
is_valid: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_error(&mut self, error: ValidationError) {
|
||||
self.errors.push(error);
|
||||
self.is_valid = false;
|
||||
}
|
||||
|
||||
pub fn add_warning(&mut self, warning: ValidationWarning) {
|
||||
self.warnings.push(warning);
|
||||
}
|
||||
|
||||
pub fn merge(&mut self, other: ValidationResult) {
|
||||
self.errors.extend(other.errors);
|
||||
self.warnings.extend(other.warnings);
|
||||
if !other.is_valid {
|
||||
self.is_valid = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ConfigValidator {
|
||||
registry: ActionRegistry,
|
||||
}
|
||||
|
||||
impl ConfigValidator {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
registry: ActionRegistry::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate_keybindings(&self, keybindings: &CanvasKeybindings) -> ValidationResult {
|
||||
let mut result = ValidationResult::new();
|
||||
|
||||
// Validate each mode
|
||||
result.merge(self.validate_mode_bindings(
|
||||
"edit",
|
||||
&keybindings.edit,
|
||||
self.registry.get_mode_registry("edit")
|
||||
));
|
||||
|
||||
result.merge(self.validate_mode_bindings(
|
||||
"read_only",
|
||||
&keybindings.read_only,
|
||||
self.registry.get_mode_registry("read_only")
|
||||
));
|
||||
|
||||
result.merge(self.validate_mode_bindings(
|
||||
"suggestions",
|
||||
&keybindings.suggestions,
|
||||
self.registry.get_mode_registry("suggestions")
|
||||
));
|
||||
|
||||
result.merge(self.validate_mode_bindings(
|
||||
"global",
|
||||
&keybindings.global,
|
||||
self.registry.get_mode_registry("global")
|
||||
));
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn validate_mode_bindings(
|
||||
&self,
|
||||
mode_name: &str,
|
||||
bindings: &HashMap<String, Vec<String>>,
|
||||
registry: &ModeRegistry
|
||||
) -> ValidationResult {
|
||||
let mut result = ValidationResult::new();
|
||||
|
||||
// Check for missing required actions
|
||||
for (action_name, spec) in ®istry.required {
|
||||
if !bindings.contains_key(action_name) {
|
||||
result.add_error(ValidationError::MissingRequired {
|
||||
action: action_name.clone(),
|
||||
mode: mode_name.to_string(),
|
||||
suggestion: format!(
|
||||
"Add to config: {} = {:?}",
|
||||
action_name,
|
||||
spec.examples
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Check for unknown actions
|
||||
let all_known: std::collections::HashSet<_> = registry.required.keys()
|
||||
.chain(registry.optional.keys())
|
||||
.collect();
|
||||
|
||||
for action_name in bindings.keys() {
|
||||
if !all_known.contains(action_name) {
|
||||
let similar = self.find_similar_actions(action_name, &all_known);
|
||||
result.add_error(ValidationError::UnknownAction {
|
||||
action: action_name.clone(),
|
||||
mode: mode_name.to_string(),
|
||||
similar,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Check for empty keybinding arrays
|
||||
for (action_name, key_list) in bindings {
|
||||
if key_list.is_empty() {
|
||||
result.add_warning(ValidationWarning {
|
||||
message: format!(
|
||||
"Action '{}' in {} mode has empty keybinding list",
|
||||
action_name, mode_name
|
||||
),
|
||||
suggestion: Some(format!(
|
||||
"Either add keybindings or remove the action from config"
|
||||
)),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Warn about auto-handled actions that shouldn't be in config
|
||||
for auto_action in ®istry.auto_handled {
|
||||
if bindings.contains_key(auto_action) {
|
||||
result.add_warning(ValidationWarning {
|
||||
message: format!(
|
||||
"Action '{}' in {} mode is auto-handled and shouldn't be in config",
|
||||
auto_action, mode_name
|
||||
),
|
||||
suggestion: Some(format!(
|
||||
"Remove '{}' from config - it's handled automatically",
|
||||
auto_action
|
||||
)),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn find_similar_actions(&self, action: &str, known_actions: &std::collections::HashSet<&String>) -> Vec<String> {
|
||||
let mut similar = Vec::new();
|
||||
|
||||
for known in known_actions {
|
||||
if self.is_similar(action, known) {
|
||||
similar.push(known.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
similar.sort();
|
||||
similar.truncate(3); // Limit to 3 suggestions
|
||||
similar
|
||||
}
|
||||
|
||||
fn is_similar(&self, a: &str, b: &str) -> bool {
|
||||
// Simple similarity check - could be improved with proper edit distance
|
||||
let a_lower = a.to_lowercase();
|
||||
let b_lower = b.to_lowercase();
|
||||
|
||||
// Check if one contains the other
|
||||
if a_lower.contains(&b_lower) || b_lower.contains(&a_lower) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check for common prefixes
|
||||
let common_prefixes = ["move_", "delete_", "suggestion_"];
|
||||
for prefix in &common_prefixes {
|
||||
if a_lower.starts_with(prefix) && b_lower.starts_with(prefix) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub fn print_validation_result(&self, result: &ValidationResult) {
|
||||
if result.is_valid && result.warnings.is_empty() {
|
||||
println!("✅ Canvas configuration is valid!");
|
||||
return;
|
||||
}
|
||||
|
||||
if !result.errors.is_empty() {
|
||||
println!("❌ Canvas configuration has errors:");
|
||||
for error in &result.errors {
|
||||
match error {
|
||||
ValidationError::MissingRequired { action, mode, suggestion } => {
|
||||
println!(" • Missing required action '{}' in {} mode", action, mode);
|
||||
println!(" 💡 {}", suggestion);
|
||||
}
|
||||
ValidationError::UnknownAction { action, mode, similar } => {
|
||||
println!(" • Unknown action '{}' in {} mode", action, mode);
|
||||
if !similar.is_empty() {
|
||||
println!(" 💡 Did you mean: {}", similar.join(", "));
|
||||
}
|
||||
}
|
||||
ValidationError::Multiple(_) => {
|
||||
println!(" • Multiple errors occurred");
|
||||
}
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
if !result.warnings.is_empty() {
|
||||
println!("⚠️ Canvas configuration has warnings:");
|
||||
for warning in &result.warnings {
|
||||
println!(" • {}", warning.message);
|
||||
if let Some(suggestion) = &warning.suggestion {
|
||||
println!(" 💡 {}", suggestion);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
if !result.is_valid {
|
||||
println!("🔧 To generate a config template, use:");
|
||||
println!(" CanvasConfig::generate_template()");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_missing_config(&self, keybindings: &CanvasKeybindings) -> String {
|
||||
let mut config = String::new();
|
||||
let validation = self.validate_keybindings(keybindings);
|
||||
|
||||
for error in &validation.errors {
|
||||
if let ValidationError::MissingRequired { action, mode, suggestion } = error {
|
||||
if config.is_empty() {
|
||||
config.push_str(&format!("# Missing required actions for canvas\n\n"));
|
||||
config.push_str(&format!("[keybindings.{}]\n", mode));
|
||||
}
|
||||
config.push_str(&format!("{}\n", suggestion));
|
||||
}
|
||||
}
|
||||
|
||||
config
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user