From f7e00cf720f55ea82933ac6625b7511e9d38139e Mon Sep 17 00:00:00 2001 From: PabloMansanet Date: Thu, 17 Jun 2021 13:08:05 +0200 Subject: Configurable keys 2 (Mapping keys to commands) (#268) * Add convenience/clarity wrapper for Range initialization * Add keycode parse and display methods * Add remapping functions and tests * Implement key remapping * Add remapping book entry * Use raw string literal for toml * Add command constants * Make command functions private * Map directly to commands * Match key parsing/displaying to Kakoune * Formatting pass * Update documentation * Formatting * Fix example in the book * Refactor into single config file * Formatting * Refactor configuration and add keymap newtype wrappers * Address first batch of PR comments * Replace FromStr with custom deserialize--- helix-term/src/ui/editor.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'helix-term/src/ui/editor.rs') diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 63b3e277..07107c9e 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -11,10 +11,7 @@ use helix_core::{ syntax::{self, HighlightEvent}, Position, Range, }; -use helix_view::{ - document::{IndentStyle, Mode}, - Document, Editor, Theme, View, -}; +use helix_view::{document::Mode, Document, Editor, Theme, View}; use std::borrow::Cow; use crossterm::{ @@ -30,7 +27,7 @@ use tui::{ }; pub struct EditorView { - keymap: Keymaps, + keymaps: Keymaps, on_next_key: Option>, last_insert: (commands::Command, Vec), completion: Option, @@ -40,16 +37,16 @@ const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter impl Default for EditorView { fn default() -> Self { - Self::new() + Self::new(Keymaps::default()) } } impl EditorView { - pub fn new() -> Self { + pub fn new(keymaps: Keymaps) -> Self { Self { - keymap: keymap::default(), + keymaps, on_next_key: None, - last_insert: (commands::normal_mode, Vec::new()), + last_insert: (commands::Command::normal_mode, Vec::new()), completion: None, } } @@ -546,8 +543,8 @@ impl EditorView { } fn insert_mode(&self, cx: &mut commands::Context, event: KeyEvent) { - if let Some(command) = self.keymap[&Mode::Insert].get(&event) { - command(cx); + if let Some(command) = self.keymaps[&Mode::Insert].get(&event) { + command.execute(cx); } else if let KeyEvent { code: KeyCode::Char(ch), .. @@ -568,7 +565,7 @@ impl EditorView { // special handling for repeat operator key!('.') => { // first execute whatever put us into insert mode - (self.last_insert.0)(cxt); + self.last_insert.0.execute(cxt); // then replay the inputs for key in &self.last_insert.1 { self.insert_mode(cxt, *key) @@ -584,8 +581,8 @@ impl EditorView { // set the register cxt.selected_register = cxt.editor.selected_register.take(); - if let Some(command) = self.keymap[&mode].get(&event) { - command(cxt); + if let Some(command) = self.keymaps[&mode].get(&event) { + command.execute(cxt); } } } @@ -699,7 +696,7 @@ impl Component for EditorView { // how we entered insert mode is important, and we should track that so // we can repeat the side effect. - self.last_insert.0 = self.keymap[&mode][&key]; + self.last_insert.0 = self.keymaps[&mode][&key]; self.last_insert.1.clear(); } (Mode::Insert, Mode::Normal) => { -- cgit v1.2.3-70-g09d2