diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 3986ad479..81ced9a8e 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -47,6 +47,7 @@ use helix_core::{ use helix_view::{ editor::Action, graphics::{CursorKind, Margin, Modifier, Rect}, + input::KeyEvent, theme::Style, view::ViewPosition, Document, DocumentId, Editor, @@ -258,6 +259,7 @@ pub struct Picker { widths: Vec, callback_fn: PickerCallback, + custom_key_handlers: PickerKeyHandler, pub truncate_start: bool, /// Caches paths to documents @@ -385,6 +387,7 @@ impl Picker { completion_height: 0, widths, preview_cache: HashMap::new(), + custom_key_handlers: HashMap::new(), read_buffer: Vec::with_capacity(1024), file_fn: None, preview_highlight_handler: PreviewHighlightHandler::::default().spawn(), @@ -392,6 +395,11 @@ impl Picker { } } + pub fn with_key_handler(mut self, handlers: PickerKeyHandler) -> Self { + self.custom_key_handlers = handlers; + self + } + pub fn injector(&self) -> Injector { Injector { dst: self.matcher.injector(), @@ -509,6 +517,15 @@ impl Picker { self.show_preview = !self.show_preview; } + fn custom_event_handler(&mut self, event: &KeyEvent, cx: &mut Context) -> EventResult { + if let Some(callback) = self.custom_key_handlers.get(event) { + callback(cx); + EventResult::Consumed(None) + } else { + EventResult::Ignored(None) + } + } + fn prompt_handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult { if let EventResult::Consumed(_) = self.prompt.handle_event(event, cx) { self.handle_prompt_change(matches!(event, Event::Paste(_))); @@ -1113,8 +1130,13 @@ impl Component for Picker { self.toggle_preview(); } - _ => { - self.prompt_handle_event(event, ctx); + key_event => { + if !matches!( + self.custom_event_handler(&key_event, ctx), + EventResult::Consumed(_) + ) { + self.prompt_handle_event(event, ctx); + }; } } @@ -1157,3 +1179,4 @@ impl Drop for Picker { } type PickerCallback = Box; +type PickerKeyHandler = HashMap>;