From d5fb7b29994920b6398b131b8e9ce676f02917c7 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Mon, 17 Feb 2025 21:54:45 +0000 Subject: [PATCH] refactor: improve the declare_key_handlers macro --- helix-term/src/ui/mod.rs | 18 +++++++++--------- helix-term/src/ui/picker.rs | 12 +++++++----- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index d6bb66610..ee8bbb32d 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -302,11 +302,11 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result $handler:expr),*) => { + ($($op:literal $key:expr => |$cx:ident, $path:ident| $handler:block),* $(,)?) => { hashmap!( $( - $key => Box::new($handler) - as Box + $key => Box::new(|$cx: &mut Context, $path: &(PathBuf, bool)| $handler) + as Box ),* ) }; @@ -342,18 +342,18 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result |cx: &mut Context| { + "Create" alt!('c') => |cx, path| { log::error!("create file"); }, - "Delete" alt!('d') => |cx: &mut Context| { + "Rename" alt!('r') => |cx, path| { + log::error!("rename file"); + }, + "Delete" alt!('d') => |cx, path| { log::error!("delete file"); }, - "Copy" alt!('y') => |cx: &mut Context| { + "Copy" alt!('y') => |cx, path| { log::error!("copy file"); }, - "Rename" alt!('r') => |cx: &mut Context| { - log::error!("rename file"); - } }); Ok(picker) diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 3513717b6..7bc998bfe 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -259,7 +259,7 @@ pub struct Picker { widths: Vec, callback_fn: PickerCallback, - custom_key_handlers: PickerKeyHandler, + custom_key_handlers: PickerKeyHandler, pub truncate_start: bool, /// Caches paths to documents @@ -395,7 +395,7 @@ impl Picker { } } - pub fn with_key_handlers(mut self, handlers: PickerKeyHandler) -> Self { + pub fn with_key_handlers(mut self, handlers: PickerKeyHandler) -> Self { self.custom_key_handlers = handlers; self } @@ -518,8 +518,10 @@ impl Picker { } fn custom_event_handler(&mut self, event: &KeyEvent, cx: &mut Context) -> EventResult { - if let Some(callback) = self.custom_key_handlers.get(event) { - callback(cx); + if let (Some(callback), Some(selected)) = + (self.custom_key_handlers.get(event), self.selection()) + { + callback(cx, selected); EventResult::Consumed(None) } else { EventResult::Ignored(None) @@ -1179,4 +1181,4 @@ impl Drop for Picker { } type PickerCallback = Box; -pub type PickerKeyHandler = HashMap>; +pub type PickerKeyHandler = HashMap>;