mirror of https://github.com/helix-editor/helix
refactor: utility macro to declare multiple handlers with ease
parent
9a28d4fa42
commit
5d29a175f3
|
@ -37,6 +37,8 @@ use std::collections::HashMap;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::{error::Error, path::PathBuf};
|
use std::{error::Error, path::PathBuf};
|
||||||
|
|
||||||
|
use self::picker::PickerKeyHandler;
|
||||||
|
|
||||||
struct Utf8PathBuf {
|
struct Utf8PathBuf {
|
||||||
path: String,
|
path: String,
|
||||||
is_dir: bool,
|
is_dir: bool,
|
||||||
|
@ -299,18 +301,16 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
|
||||||
},
|
},
|
||||||
)];
|
)];
|
||||||
|
|
||||||
let delete: Box<dyn Fn(&mut Context) + 'static> = Box::new(|cx: &mut Context| {
|
macro_rules! declare_key_handlers {
|
||||||
log::error!("delete file");
|
($($op:literal $key:expr => $handler:expr),*) => {
|
||||||
});
|
hashmap!(
|
||||||
let create: Box<dyn Fn(&mut Context) + 'static> = Box::new(|cx: &mut Context| {
|
$(
|
||||||
log::error!("create file");
|
$key => Box::new($handler)
|
||||||
});
|
as Box<dyn Fn(&mut Context) + 'static>
|
||||||
let rename: Box<dyn Fn(&mut Context) + 'static> = Box::new(|cx: &mut Context| {
|
),*
|
||||||
log::error!("rename file");
|
)
|
||||||
});
|
};
|
||||||
let copy: Box<dyn Fn(&mut Context) + 'static> = Box::new(|cx: &mut Context| {
|
}
|
||||||
log::error!("copy file");
|
|
||||||
});
|
|
||||||
|
|
||||||
let picker = Picker::new(
|
let picker = Picker::new(
|
||||||
columns,
|
columns,
|
||||||
|
@ -341,12 +341,20 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.with_preview(|_editor, (path, _is_dir)| Some((path.as_path().into(), None)))
|
.with_preview(|_editor, (path, _is_dir)| Some((path.as_path().into(), None)))
|
||||||
.with_key_handler(HashMap::from([
|
.with_key_handlers(declare_key_handlers! {
|
||||||
(alt!('c'), create),
|
"Create" alt!('c') => |cx: &mut Context| {
|
||||||
(alt!('d'), delete),
|
log::error!("create file");
|
||||||
(alt!('y'), copy),
|
},
|
||||||
(alt!('r'), rename),
|
"Delete" alt!('d') => |cx: &mut Context| {
|
||||||
]));
|
log::error!("delete file");
|
||||||
|
},
|
||||||
|
"Copy" alt!('y') => |cx: &mut Context| {
|
||||||
|
log::error!("copy file");
|
||||||
|
},
|
||||||
|
"Rename" alt!('r') => |cx: &mut Context| {
|
||||||
|
log::error!("rename file");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Ok(picker)
|
Ok(picker)
|
||||||
}
|
}
|
||||||
|
|
|
@ -395,7 +395,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_key_handler(mut self, handlers: PickerKeyHandler) -> Self {
|
pub fn with_key_handlers(mut self, handlers: PickerKeyHandler) -> Self {
|
||||||
self.custom_key_handlers = handlers;
|
self.custom_key_handlers = handlers;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -1179,4 +1179,4 @@ impl<T: 'static + Send + Sync, D> Drop for Picker<T, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
type PickerCallback<T> = Box<dyn Fn(&mut Context, &T, Action)>;
|
type PickerCallback<T> = Box<dyn Fn(&mut Context, &T, Action)>;
|
||||||
type PickerKeyHandler = HashMap<KeyEvent, Box<dyn Fn(&mut Context)>>;
|
pub type PickerKeyHandler = HashMap<KeyEvent, Box<dyn Fn(&mut Context) + 'static>>;
|
||||||
|
|
Loading…
Reference in New Issue