mirror of https://github.com/helix-editor/helix
refactor: improve the declare_key_handlers macro
parent
5d29a175f3
commit
d5fb7b2999
|
@ -302,11 +302,11 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
|
|||
)];
|
||||
|
||||
macro_rules! declare_key_handlers {
|
||||
($($op:literal $key:expr => $handler:expr),*) => {
|
||||
($($op:literal $key:expr => |$cx:ident, $path:ident| $handler:block),* $(,)?) => {
|
||||
hashmap!(
|
||||
$(
|
||||
$key => Box::new($handler)
|
||||
as Box<dyn Fn(&mut Context) + 'static>
|
||||
$key => Box::new(|$cx: &mut Context, $path: &(PathBuf, bool)| $handler)
|
||||
as Box<dyn Fn(&mut Context, &(PathBuf, bool)) + 'static>
|
||||
),*
|
||||
)
|
||||
};
|
||||
|
@ -342,18 +342,18 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
|
|||
)
|
||||
.with_preview(|_editor, (path, _is_dir)| Some((path.as_path().into(), None)))
|
||||
.with_key_handlers(declare_key_handlers! {
|
||||
"Create" alt!('c') => |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)
|
||||
|
|
|
@ -259,7 +259,7 @@ pub struct Picker<T: 'static + Send + Sync, D: 'static> {
|
|||
widths: Vec<Constraint>,
|
||||
|
||||
callback_fn: PickerCallback<T>,
|
||||
custom_key_handlers: PickerKeyHandler,
|
||||
custom_key_handlers: PickerKeyHandler<T>,
|
||||
|
||||
pub truncate_start: bool,
|
||||
/// Caches paths to documents
|
||||
|
@ -395,7 +395,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn with_key_handlers(mut self, handlers: PickerKeyHandler) -> Self {
|
||||
pub fn with_key_handlers(mut self, handlers: PickerKeyHandler<T>) -> Self {
|
||||
self.custom_key_handlers = handlers;
|
||||
self
|
||||
}
|
||||
|
@ -518,8 +518,10 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
|
|||
}
|
||||
|
||||
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<T: 'static + Send + Sync, D> Drop for Picker<T, D> {
|
|||
}
|
||||
|
||||
type PickerCallback<T> = Box<dyn Fn(&mut Context, &T, Action)>;
|
||||
pub type PickerKeyHandler = HashMap<KeyEvent, Box<dyn Fn(&mut Context) + 'static>>;
|
||||
pub type PickerKeyHandler<T> = HashMap<KeyEvent, Box<dyn Fn(&mut Context, &T) + 'static>>;
|
||||
|
|
Loading…
Reference in New Issue