Simplify keymap.rs with macros

This commit is contained in:
Blaž Hrastnik 2020-10-01 16:06:42 +09:00
parent 4c49197e20
commit d9d59cd209

View File

@ -90,98 +90,54 @@ pub use crossterm::event::{KeyCode, KeyEvent as Key, KeyModifiers as Modifiers};
type Keymap = HashMap<Vec<Key>, Command>; type Keymap = HashMap<Vec<Key>, Command>;
type Keymaps = HashMap<state::Mode, Keymap>; type Keymaps = HashMap<state::Mode, Keymap>;
macro_rules! key {
($ch:expr) => {
Key {
code: KeyCode::Char($ch),
modifiers: Modifiers::NONE,
}
};
}
macro_rules! shift {
($ch:expr) => {
Key {
code: KeyCode::Char($ch),
modifiers: Modifiers::SHIFT,
}
};
}
pub fn default() -> Keymaps { pub fn default() -> Keymaps {
hashmap!( hashmap!(
state::Mode::Normal => state::Mode::Normal =>
// as long as you cast the first item, rust is able to infer the other cases
hashmap!( hashmap!(
vec![Key { vec![key!('h')] => commands::move_char_left as Command,
code: KeyCode::Char('h'), vec![key!('j')] => commands::move_line_down,
modifiers: Modifiers::NONE vec![key!('k')] => commands::move_line_up,
}] => commands::move_char_left as Command, vec![key!('0')] => commands::move_line_start,
vec![Key { vec![key!('$')] => commands::move_line_end,
code: KeyCode::Char('j'), vec![key!('l')] => commands::move_char_right,
modifiers: Modifiers::NONE vec![shift!('H')] => commands::extend_char_left,
}] => commands::move_line_down as Command, vec![shift!('J')] => commands::extend_line_down,
vec![Key { vec![shift!('K')] => commands::extend_line_up,
code: KeyCode::Char('k'), vec![shift!('L')] => commands::extend_char_right,
modifiers: Modifiers::NONE vec![key!('w')] => commands::move_next_word_start,
}] => commands::move_line_up as Command, vec![key!('b')] => commands::move_prev_word_start,
vec![Key { vec![key!('e')] => commands::move_next_word_end,
code: KeyCode::Char('0'), vec![key!('i')] => commands::insert_mode,
modifiers: Modifiers::NONE vec![shift!('I')] => commands::prepend_to_line,
}] => commands::move_line_start as Command, vec![key!('a')] => commands::append_mode,
vec![Key { vec![shift!('A')] => commands::append_to_line,
code: KeyCode::Char('$'), vec![key!('o')] => commands::open_below,
modifiers: Modifiers::NONE vec![key!('d')] => commands::delete_selection,
}] => commands::move_line_end as Command, vec![key!('c')] => commands::change_selection,
vec![Key { vec![key!('s')] => commands::split_selection_on_newline,
code: KeyCode::Char('l'),
modifiers: Modifiers::NONE
}] => commands::move_char_right as Command,
vec![Key {
code: KeyCode::Char('H'),
modifiers: Modifiers::SHIFT
}] => commands::extend_char_left as Command,
vec![Key {
code: KeyCode::Char('J'),
modifiers: Modifiers::SHIFT
}] => commands::extend_line_down as Command,
vec![Key {
code: KeyCode::Char('K'),
modifiers: Modifiers::SHIFT
}] => commands::extend_line_up as Command,
vec![Key {
code: KeyCode::Char('L'),
modifiers: Modifiers::SHIFT
}] => commands::extend_char_right as Command,
vec![Key {
code: KeyCode::Char('w'),
modifiers: Modifiers::NONE
}] => commands::move_next_word_start as Command,
vec![Key {
code: KeyCode::Char('b'),
modifiers: Modifiers::NONE
}] => commands::move_prev_word_start as Command,
vec![Key {
code: KeyCode::Char('e'),
modifiers: Modifiers::NONE
}] => commands::move_next_word_end as Command,
vec![Key {
code: KeyCode::Char('i'),
modifiers: Modifiers::NONE
}] => commands::insert_mode as Command,
vec![Key {
code: KeyCode::Char('I'),
modifiers: Modifiers::SHIFT,
}] => commands::prepend_to_line as Command,
vec![Key {
code: KeyCode::Char('a'),
modifiers: Modifiers::NONE
}] => commands::append_mode as Command,
vec![Key {
code: KeyCode::Char('A'),
modifiers: Modifiers::SHIFT,
}] => commands::append_to_line as Command,
vec![Key {
code: KeyCode::Char('o'),
modifiers: Modifiers::NONE
}] => commands::open_below as Command,
vec![Key {
code: KeyCode::Char('d'),
modifiers: Modifiers::NONE
}] => commands::delete_selection as Command,
vec![Key {
code: KeyCode::Char('c'),
modifiers: Modifiers::NONE
}] => commands::change_selection as Command,
vec![Key {
code: KeyCode::Char('s'),
modifiers: Modifiers::NONE
}] => commands::split_selection_on_newline as Command,
vec![Key { vec![Key {
code: KeyCode::Esc, code: KeyCode::Esc,
modifiers: Modifiers::NONE modifiers: Modifiers::NONE
}] => commands::normal_mode as Command, }] => commands::normal_mode,
), ),
state::Mode::Insert => hashmap!( state::Mode::Insert => hashmap!(
vec![Key { vec![Key {
@ -191,15 +147,15 @@ pub fn default() -> Keymaps {
vec![Key { vec![Key {
code: KeyCode::Backspace, code: KeyCode::Backspace,
modifiers: Modifiers::NONE modifiers: Modifiers::NONE
}] => commands::delete_char_backward as Command, }] => commands::delete_char_backward,
vec![Key { vec![Key {
code: KeyCode::Delete, code: KeyCode::Delete,
modifiers: Modifiers::NONE modifiers: Modifiers::NONE
}] => commands::delete_char_forward as Command, }] => commands::delete_char_forward,
vec![Key { vec![Key {
code: KeyCode::Enter, code: KeyCode::Enter,
modifiers: Modifiers::NONE modifiers: Modifiers::NONE
}] => commands::insert_newline as Command, }] => commands::insert_newline,
) )
) )
} }