diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-03 11:06:14 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-03 11:06:14 +0200 |
| commit | 90daf295c7dd5fedc3219b8947fac110f05cc522 (patch) | |
| tree | a8cc7dba65f1c8a6b7ed5e528e65bc86885d3c22 /src | |
| parent | cd907dd59a48c2aa9d602aa3fb2f24563994420e (diff) | |
add editor keymap
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.rs | 4 | ||||
| -rw-r--r-- | src/config/keymap/keymap_store.rs | 14 | ||||
| -rw-r--r-- | src/config/keymap/mod.rs | 54 | ||||
| -rw-r--r-- | src/config/keymap/template.rs | 10 | ||||
| -rw-r--r-- | src/state/editor/mod.rs | 7 | ||||
| -rw-r--r-- | src/widgets/luaeditor/mod.rs | 68 |
6 files changed, 67 insertions, 90 deletions
@@ -85,8 +85,10 @@ impl App { fn handle_key_event(&mut self, event: KeyEvent) { let focus = { GlobalState::instance().active_window }; + let op = GlobalKeyMap::handle(event); - if !GlobalKeyMap::handle(event) { + + if op.unwrap_or(true) { match focus { Window::View => self.view.handle_key_event(event), Window::Editor => self.editor.handle_key_event(event), diff --git a/src/config/keymap/keymap_store.rs b/src/config/keymap/keymap_store.rs index c782219..233ac52 100644 --- a/src/config/keymap/keymap_store.rs +++ b/src/config/keymap/keymap_store.rs @@ -48,17 +48,16 @@ pub trait Runnable where Self: Send, { - fn run(&self) -> Result<()>; + fn run(&self) -> Result<bool>; } impl<T> Runnable for T where - T: Fn(), + T: Fn() -> bool, Self: Send, { - fn run(&self) -> Result<()> { - self(); - Ok(()) + fn run(&self) -> Result<bool> { + Ok(self()) } } @@ -66,11 +65,10 @@ impl Runnable for RegistryKey where Self: Send, { - fn run(&self) -> Result<()> { + fn run(&self) -> Result<bool> { let lua = lua::get(); let func: Function = lua.registry_value(self)?; - func.call::<(), ()>(())?; - Ok(()) + Ok(func.call::<(), bool>(()).unwrap_or(false)) } } diff --git a/src/config/keymap/mod.rs b/src/config/keymap/mod.rs index e9995f2..dc97ccc 100644 --- a/src/config/keymap/mod.rs +++ b/src/config/keymap/mod.rs @@ -9,33 +9,41 @@ use self::{ use super::{GlobalConfig, DUMMY_CONFIG}; use template::KeyMapSections; -KeyMapSections!( - ViewKeyMap => view, - GlobalKeyMap => global, -); - -#[derive(Debug, Default, Clone)] -pub struct KeyMap { - pub view: ViewKeyMap, - pub global: GlobalKeyMap, -} - pub mod event_from_string; pub mod keymap_store; pub mod template; -impl KeyMap { - pub const fn new() -> Self { - Self { - view: ViewKeyMap::new(), - global: GlobalKeyMap::new(), +macro_rules! KeyMap { + ($($name:ident => $key:ident),+ $(,)?) => { + KeyMapSections!($($name => $key,)*); + + #[derive(Debug, Default, Clone)] + pub struct KeyMap { + $($key: $name,)* } - } -} -impl UserData for KeyMap { - fn add_fields<'lua, F: mlua::prelude::LuaUserDataFields<'lua, Self>>(fields: &mut F) { - fields.add_field_function_get("view", |_, _| Ok(DUMMY_CONFIG.keymap.view.clone())); - fields.add_field_function_get("global", |_, _| Ok(DUMMY_CONFIG.keymap.global.clone())); - } + impl KeyMap { + pub const fn new() -> Self { + Self { + $($key: $name::new(),)* + } + } + } + + impl UserData for KeyMap { + fn add_fields<'lua, F: mlua::prelude::LuaUserDataFields<'lua, Self>>(fields: &mut F) { + $( + fields.add_field_function_get(stringify!($key), |_, _| + Ok(DUMMY_CONFIG.keymap.$key.clone())); + )* + } + } + }; } + +KeyMap!( + ViewKeyMap => view, + GlobalKeyMap => global, + EditorKeyMap => editor, +); + diff --git a/src/config/keymap/template.rs b/src/config/keymap/template.rs index bebcdc3..c5f21dc 100644 --- a/src/config/keymap/template.rs +++ b/src/config/keymap/template.rs @@ -13,17 +13,17 @@ macro_rules! KeyMapSections { } } - pub fn handle(event: KeyEvent) -> bool { + pub fn handle(event: KeyEvent) -> Option<bool> { let func = { GlobalConfig::instance_mut().keymap.$key.store.get(event) }; if let Some(func) = func { let func = func.lock().unwrap(); - if let Err(_error) = func.run() { - // TODO: add error buffer + match func.run() { + Ok(value) => Some(value), + _ => None } - true } else { - false + None } } diff --git a/src/state/editor/mod.rs b/src/state/editor/mod.rs index 058bbf6..47d11bc 100644 --- a/src/state/editor/mod.rs +++ b/src/state/editor/mod.rs @@ -1,3 +1,5 @@ +use crate::cursor::CursorMove; + use self::{bar::EditorBarState, buffer::Buffer}; use super::{GlobalState, DUMMY_STATE}; @@ -84,6 +86,11 @@ impl UserData for EditorState { methods.add_function("run", |lua, _: ()| { EditorState::run(lua); Ok(()) + }); + + methods.add_function("move_cursor", |_, m: CursorMove| { + cfg_mut!().buffer.move_cursor(m); + Ok(()) }) } } diff --git a/src/widgets/luaeditor/mod.rs b/src/widgets/luaeditor/mod.rs index 620de4c..4bcb5d2 100644 --- a/src/widgets/luaeditor/mod.rs +++ b/src/widgets/luaeditor/mod.rs @@ -1,17 +1,17 @@ use ratatui::{ - crossterm::event::{KeyCode, KeyEvent, KeyModifiers}, + crossterm::event::{KeyCode, KeyEvent}, layout::Rect, text::{ToLine, ToSpan}, widgets::{Paragraph, Widget}, }; use tree_sitter_highlight::HighlightConfiguration; +use crate::{config::keymap::EditorKeyMap, lua}; use crate::{ config::{theme::editor::bar::EditorBarTheme, GlobalConfig}, - state::{editor::{bar::EditorBarState, EditorState}, window::Window, GlobalState}, + state::{editor::bar::EditorBarState, GlobalState}, tuicursor::TuiCursor, }; -use crate::{cursor::CursorMove, lua}; use super::statusbar::StatusBar; @@ -34,57 +34,19 @@ impl LuaEditor { } pub fn handle_key_event(&mut self, event: KeyEvent) { - match event.code { - KeyCode::Char('r') if event.modifiers == KeyModifiers::CONTROL => { - EditorState::run(&lua::get()) - } - KeyCode::Char(c) => GlobalState::instance_mut().editor.buffer.insert(c), - KeyCode::Backspace => { - GlobalState::instance_mut().editor.buffer.delete(); - } - KeyCode::Enter => { - GlobalState::instance_mut().editor.buffer.insert('\n'); - } - KeyCode::Left => GlobalState::instance_mut() - .editor - .buffer - .move_cursor(CursorMove::Left(1)), - KeyCode::Right => GlobalState::instance_mut() - .editor - .buffer - .move_cursor(CursorMove::Right(1)), - KeyCode::Up => GlobalState::instance_mut() - .editor - .buffer - .move_cursor(CursorMove::Up(1)), - KeyCode::Down => GlobalState::instance_mut() - .editor - .buffer - .move_cursor(CursorMove::Down(1)), - KeyCode::Home => {} - KeyCode::End => {} - KeyCode::PageUp => {} - KeyCode::PageDown => {} - KeyCode::Tab => GlobalState::instance_mut().editor.buffer.insert('\t'), - KeyCode::BackTab => {} - KeyCode::Delete => {} - KeyCode::Insert => {} - KeyCode::F(_) => {} - KeyCode::Null => {} - KeyCode::Esc => { - let mut state = GlobalState::instance_mut(); - state.editor.visible = false; - state.set_focus(Window::View) + let r = EditorKeyMap::handle(event); + + if r.unwrap_or(true) { + match event.code { + KeyCode::Char(c) => GlobalState::instance_mut().editor.buffer.insert(c), + KeyCode::Backspace => { + GlobalState::instance_mut().editor.buffer.delete(); + } + KeyCode::Enter => { + GlobalState::instance_mut().editor.buffer.insert('\n'); + } + _ => {} } - KeyCode::CapsLock => {} - KeyCode::ScrollLock => {} - KeyCode::NumLock => {} - KeyCode::PrintScreen => {} - KeyCode::Pause => {} - KeyCode::Menu => {} - KeyCode::KeypadBegin => {} - KeyCode::Media(_) => {} - KeyCode::Modifier(_) => {} } } |