Merge pull request #1 from helix-editor/insertKeymapMove

moved insert keymap to keymap.rs
This commit is contained in:
Blaž Hrastnik 2020-10-01 11:57:43 +09:00 committed by GitHub
commit 1da0be0aa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 20 deletions

View File

@ -320,29 +320,20 @@ pub async fn event_loop(&mut self) {
if let Some(view) = &mut self.view { if let Some(view) = &mut self.view {
match view.state.mode() { match view.state.mode() {
Mode::Insert => { Mode::Insert => {
match event { // TODO: handle modes and sequences (`gg`)
KeyEvent { let keys = vec![event];
code: KeyCode::Esc, .. if let Some(command) = keymap[&Mode::Insert].get(&keys) {
} => commands::normal_mode(view, 1), // TODO: handle count other than 1
KeyEvent { command(view, 1);
code: KeyCode::Backspace, } else {
.. if let KeyEvent {
} => commands::delete_char_backward(view, 1),
KeyEvent {
code: KeyCode::Delete,
..
} => commands::delete_char_forward(view, 1),
KeyEvent {
code: KeyCode::Char(c), code: KeyCode::Char(c),
.. ..
} => commands::insert_char(view, c), } = event
KeyEvent { {
code: KeyCode::Enter, commands::insert_char(view, c);
.. }
} => commands::insert_char(view, '\n'),
_ => (), // skip
} }
// TODO: simplistic ensure cursor in view for now
view.ensure_cursor_in_view(); view.ensure_cursor_in_view();
self.render(); self.render();

View File

@ -8,6 +8,7 @@ edition = "2018"
[features] [features]
term = ["tui", "crossterm"] term = ["tui", "crossterm"]
default = ["term"]
[dependencies] [dependencies]
anyhow = "1" anyhow = "1"

View File

@ -280,6 +280,10 @@ pub fn insert_char(view: &mut View, c: char) {
// TODO: need to store into history if successful // TODO: need to store into history if successful
} }
pub fn insert_newline(view: &mut View, _count: usize) {
insert_char(view, '\n');
}
// TODO: handle indent-aware delete // TODO: handle indent-aware delete
pub fn delete_char_backward(view: &mut View, count: usize) { pub fn delete_char_backward(view: &mut View, count: usize) {
let text = &view.state.doc.slice(..); let text = &view.state.doc.slice(..);

View File

@ -182,6 +182,24 @@ pub fn default() -> Keymaps {
code: KeyCode::Esc, code: KeyCode::Esc,
modifiers: Modifiers::NONE modifiers: Modifiers::NONE
}] => commands::normal_mode as Command, }] => commands::normal_mode as Command,
),
state::Mode::Insert => hashmap!(
vec![Key {
code: KeyCode::Esc,
modifiers: Modifiers::NONE
}] => commands::normal_mode as Command,
vec![Key {
code: KeyCode::Backspace,
modifiers: Modifiers::NONE
}] => commands::delete_char_backward as Command,
vec![Key {
code: KeyCode::Delete,
modifiers: Modifiers::NONE
}] => commands::delete_char_forward as Command,
vec![Key {
code: KeyCode::Enter,
modifiers: Modifiers::NONE
}] => commands::insert_newline as Command,
) )
) )
} }