Migrate keybind handling to State::on_key()

This commit is contained in:
mars 2023-04-14 14:11:36 -04:00
parent 878f47c7e9
commit 3020d14081
1 changed files with 25 additions and 8 deletions

View File

@ -33,7 +33,6 @@ use crossterm::{
event::{Event, KeyCode, KeyEvent},
terminal, QueueableCommand, Result,
};
use keybinds::Keybind;
use parking_lot::Mutex;
use ropey::Rope;
use yacexits::{exit, EX_DATAERR, EX_UNAVAILABLE};
@ -46,6 +45,7 @@ mod theme;
use buffer::Buffer;
use config::Config;
use keybinds::{Key, Keybind};
use theme::StyleStore;
#[derive(Copy, Clone, Debug, Default)]
@ -274,9 +274,7 @@ impl State {
fn on_visual_event(&mut self, event: Event) {
match event {
Event::Key(KeyEvent { code, .. }) => {
if let Some(keybind) = self.config.keybinds.visual.map.get(&code) {
self.execute_keybind(keybind.clone());
}
self.on_key(code);
}
event => self.on_any_event(event),
}
@ -284,19 +282,38 @@ impl State {
fn on_insert_event(&mut self, event: Event, _state: InsertState) {
match event {
Event::Key(KeyEvent { code, .. }) => match self.config.keybinds.insert.map.get(&code) {
Some(keybind) => self.execute_keybind(keybind.clone()),
None => {
Event::Key(KeyEvent { code, .. }) => {
if !self.on_key(code) {
if let KeyCode::Char(c) = code {
self.buffer.insert_char(self.cursor, c);
self.move_cursor(Direction::Right);
}
}
},
}
event => self.on_any_event(event),
}
}
/// Processes a key press event.
///
/// Returns `true` if the key was handled by a keybind, `false` otherwise.
fn on_key(&mut self, key: Key) -> bool {
let keybinds = match &self.mode {
Mode::Normal(_) => &self.config.keybinds.normal,
Mode::Insert(_) => &self.config.keybinds.insert,
Mode::Visual => &self.config.keybinds.visual,
Mode::Command(_) => return false, // command mode is handled in [on_command_event]
};
match keybinds.map.get(&key) {
None => false,
Some(keybind) => {
self.execute_keybind(keybind.clone());
return true;
}
}
}
fn on_any_event(&mut self, event: Event) {
match event {
Event::Resize(cols, rows) => {