refactor: remove unneeded macro

pull/12902/head
Nikita Revenco 2025-02-18 14:08:16 +00:00
parent 24bd14863b
commit 4fabd7927d
2 changed files with 12 additions and 26 deletions

View File

@ -16,7 +16,7 @@ mod text_decorations;
use crate::compositor::{Compositor, Context};
use crate::job::{self, Callback};
use crate::{alt, declare_key_handlers, filter_picker_entry};
use crate::{alt, filter_picker_entry};
pub use completion::Completion;
pub use editor::EditorView;
use helix_core::hashmap;
@ -418,10 +418,9 @@ pub fn file_explorer(cursor: Option<u32>, root: PathBuf, editor: &Editor) -> Res
)
.with_cursor(cursor.unwrap_or_default())
.with_preview(|_editor, (path, _is_dir)| Some((path.as_path().into(), None)))
.with_key_handlers(declare_key_handlers! {
|cx, (path, _is_dir): &(PathBuf, bool), cursor|,
.with_key_handlers(hashmap! {
// create
alt!('n') => {
alt!('n') => Box::new(|cx: &mut Context, (path, _is_dir): &(PathBuf, bool), cursor: u32| {
create_file_operation_prompt(
cursor,
"create:",
@ -472,9 +471,9 @@ pub fn file_explorer(cursor: Option<u32>, root: PathBuf, editor: &Editor) -> Res
create_op(cursor, cx, root, to_create_str, &to_create)
})
},
}) as Box<dyn Fn(&mut Context, &(PathBuf, bool), u32) + 'static>,
// move
alt!('m') => {
alt!('m') => Box::new(|cx: &mut Context, (path, _is_dir): &(PathBuf, bool), cursor: u32|{
create_file_operation_prompt(
cursor,
"move:",
@ -523,9 +522,9 @@ pub fn file_explorer(cursor: Option<u32>, root: PathBuf, editor: &Editor) -> Res
move_op(cursor, cx, root, move_to_str, move_from)
})
},
}) as Box<dyn Fn(&mut Context, &(PathBuf, bool), u32) + 'static>,
// delete
alt!('d') => {
alt!('d') => Box::new(|cx: &mut Context, (path, _is_dir): &(PathBuf, bool), cursor: u32|{
create_file_operation_prompt(
cursor,
"delete? (y/n):",
@ -567,9 +566,9 @@ pub fn file_explorer(cursor: Option<u32>, root: PathBuf, editor: &Editor) -> Res
None
}
})
},
}) as Box<dyn Fn(&mut Context, &(PathBuf, bool), u32) + 'static>,
// copy file / directory
alt!('c') => {
alt!('c') => Box::new(|cx: &mut Context, (path, _is_dir): &(PathBuf, bool), cursor: u32|{
create_file_operation_prompt(
cursor,
"copy-to:",
@ -618,9 +617,9 @@ pub fn file_explorer(cursor: Option<u32>, root: PathBuf, editor: &Editor) -> Res
copy_op(cursor, cx, root, copy_to_str, copy_from)
}
})
},
}) as Box<dyn Fn(&mut Context, &(PathBuf, bool), u32) + 'static>,
// copy path into register
alt!('y') => {
alt!('y') => Box::new(|cx: &mut Context, (path, _is_dir): &(PathBuf, bool), _cursor: u32|{
let register = cx.editor.selected_register.unwrap_or(
cx.editor.config().default_yank_register
);
@ -632,7 +631,7 @@ pub fn file_explorer(cursor: Option<u32>, root: PathBuf, editor: &Editor) -> Res
Ok(()) => cx.editor.set_status(message),
Err(err) => cx.editor.set_error(err.to_string())
};
}
}) as Box<dyn Fn(&mut Context, &(PathBuf, bool), u32) + 'static>
});
Ok(picker)

View File

@ -1186,16 +1186,3 @@ impl<T: 'static + Send + Sync, D> Drop for Picker<T, D> {
type PickerCallback<T> = Box<dyn Fn(&mut Context, &T, Action)>;
pub type PickerKeyHandler<T> = HashMap<KeyEvent, Box<dyn Fn(&mut Context, &T, u32) + 'static>>;
/// Convenience macro to add custom keybindings per picker
#[macro_export]
macro_rules! declare_key_handlers {
(|$cx:ident, $item:tt : $t:ty, $cursor:ident|, $($key:expr => $handler:block),* $(,)?) => {
hashmap!(
$(
$key => Box::new(|$cx: &mut Context, $item: $t, $cursor: u32| $handler)
as Box<dyn Fn(&mut Context, $t, u32) + 'static>
),*
)
};
}