on master help sign in
Merge pull request #1 from helix-editor/insertKeymapMove

moved insert keymap to keymap.rs

commit1da0be0aa045a4acd0246d82bf6214f8ec651476 authorBlaž Hrastnik <blaz@mxxn.io> dateThu 1 Oct 2020, 11:57 (6 years ago) parent592c5b013800e4
4 files changed+34−20
helix-term/src/editor.rs+11−20 helix-view/Cargo.toml+1 helix-view/src/commands.rs+4 helix-view/src/keymap.rs+18
helix-term/src/editor.rs+11−20
@@ -320,29 +320,20 @@ impl Editor {
320320 if let Some(view) = &mut self.view {
321321 match view.state.mode() {
322322 Mode::Insert => {
323− match event {
324− KeyEvent {
325− code: KeyCode::Esc, ..
326− } => commands::normal_mode(view, 1),
327− KeyEvent {
328− code: KeyCode::Backspace,
329− ..
330− } => commands::delete_char_backward(view, 1),
331− KeyEvent {
332− code: KeyCode::Delete,
333− ..
334− } => commands::delete_char_forward(view, 1),
335− KeyEvent {
323+ // TODO: handle modes and sequences (`gg`)
324+ let keys = vec![event];
325+ if let Some(command) = keymap[&Mode::Insert].get(&keys) {
326+ // TODO: handle count other than 1
327+ command(view, 1);
328+ } else {
329+ if let KeyEvent {
336330 code: KeyCode::Char(c),
337331 ..
338− } => commands::insert_char(view, c),
339− KeyEvent {
340− code: KeyCode::Enter,
341− ..
342− } => commands::insert_char(view, '\n'),
343− _ => (), // skip
332+ } = event
333+ {
334+ commands::insert_char(view, c);
335+ }
344336 }
345− // TODO: simplistic ensure cursor in view for now
346337 view.ensure_cursor_in_view();
347338
348339 self.render();
helix-view/Cargo.toml+1
@@ -8,6 +8,7 @@ edition = "2018"
88
99[features]
1010term = ["tui", "crossterm"]
11+default = ["term"]
1112
1213[dependencies]
1314anyhow = "1"
helix-view/src/commands.rs+4
@@ -280,6 +280,10 @@ pub fn insert_char(view: &mut View, c: char) {
280280 // TODO: need to store into history if successful
281281}
282282
283+pub fn insert_newline(view: &mut View, _count: usize) {
284+ insert_char(view, '\n');
285+}
286+
283287// TODO: handle indent-aware delete
284288pub fn delete_char_backward(view: &mut View, count: usize) {
285289 let text = &view.state.doc.slice(..);
helix-view/src/keymap.rs+18
@@ -182,6 +182,24 @@ pub fn default() -> Keymaps {
182182 code: KeyCode::Esc,
183183 modifiers: Modifiers::NONE
184184 }] => commands::normal_mode as Command,
185+ ),
186+ state::Mode::Insert => hashmap!(
187+ vec![Key {
188+ code: KeyCode::Esc,
189+ modifiers: Modifiers::NONE
190+ }] => commands::normal_mode as Command,
191+ vec![Key {
192+ code: KeyCode::Backspace,
193+ modifiers: Modifiers::NONE
194+ }] => commands::delete_char_backward as Command,
195+ vec![Key {
196+ code: KeyCode::Delete,
197+ modifiers: Modifiers::NONE
198+ }] => commands::delete_char_forward as Command,
199+ vec![Key {
200+ code: KeyCode::Enter,
201+ modifiers: Modifiers::NONE
202+ }] => commands::insert_newline as Command,
185203 )
186204 )
187205}