From 88d6f652390922b389667f469b6d308db569bdaf Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Mon, 26 Jul 2021 21:37:13 +0530 Subject: Allow multi key remappings in config file (#454) * Use tree like structure to store keymaps * Allow multi key keymaps in config file * Allow multi key keymaps in insert mode * Make keymap state self contained * Add keymap! macro for ergonomic declaration * Add descriptions for editor commands * Allow keymap! to take multiple keys * Restore infobox display * Fix keymap merging and add infobox titles * Fix and add tests for keymaps * Clean up comments and apply suggestions * Allow trailing commas in keymap! * Remove mode suffixes from keymaps * Preserve order of keys when showing infobox * Make command descriptions smaller * Strip infobox title prefix from items * Strip infobox title prefix from items--- helix-view/src/editor.rs | 3 --- helix-view/src/info.rs | 17 ++++++++++------- helix-view/src/input.rs | 12 +++++++++++- 3 files changed, 21 insertions(+), 11 deletions(-) (limited to 'helix-view/src') diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index cd9d0a92..7ff689df 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1,7 +1,6 @@ use crate::{ clipboard::{get_clipboard_provider, ClipboardProvider}, graphics::{CursorKind, Rect}, - info::Info, theme::{self, Theme}, tree::Tree, Document, DocumentId, RegisterSelection, View, ViewId, @@ -33,7 +32,6 @@ pub struct Editor { pub syn_loader: Arc, pub theme_loader: Arc, - pub autoinfo: Option<&'static Info>, pub status_msg: Option<(String, Severity)>, } @@ -67,7 +65,6 @@ impl Editor { theme_loader: themes, registers: Registers::default(), clipboard_provider: get_clipboard_provider(), - autoinfo: None, status_msg: None, } } diff --git a/helix-view/src/info.rs b/helix-view/src/info.rs index f3df50fe..70e934cd 100644 --- a/helix-view/src/info.rs +++ b/helix-view/src/info.rs @@ -5,9 +5,9 @@ use std::fmt::Write; #[derive(Debug)] /// Info box used in editor. Rendering logic will be in other crate. pub struct Info { - /// Title kept as static str for now. - pub title: &'static str, - /// Text body, should contains newline. + /// Title shown at top. + pub title: String, + /// Text body, should contain newlines. pub text: String, /// Body width. pub width: u16, @@ -16,17 +16,20 @@ pub struct Info { } impl Info { - pub fn key(title: &'static str, body: Vec<(&[KeyEvent], &'static str)>) -> Info { + // body is a BTreeMap instead of a HashMap because keymaps are represented + // with nested hashmaps with no ordering, and each invocation of infobox would + // show different orders of items + pub fn key(title: &str, body: Vec<(&str, Vec)>) -> Info { let (lpad, mpad, rpad) = (1, 2, 1); let keymaps_width: u16 = body .iter() - .map(|r| r.0.iter().map(|e| e.width() as u16 + 2).sum::() - 2) + .map(|r| r.1.iter().map(|e| e.width() as u16 + 2).sum::() - 2) .max() .unwrap(); let mut text = String::new(); let mut width = 0; let height = body.len() as u16; - for (keyevents, desc) in body { + for (desc, keyevents) in body { let keyevent = keyevents[0]; let mut left = keymaps_width - keyevent.width() as u16; for _ in 0..lpad { @@ -48,7 +51,7 @@ impl Info { writeln!(text, "{}", desc).ok(); } Info { - title, + title: title.to_string(), text, width, height, diff --git a/helix-view/src/input.rs b/helix-view/src/input.rs index 2847bb69..8d9ee6fb 100644 --- a/helix-view/src/input.rs +++ b/helix-view/src/input.rs @@ -14,6 +14,16 @@ pub struct KeyEvent { pub modifiers: KeyModifiers, } +impl KeyEvent { + /// Get only the character involved in this event + pub fn char(&self) -> Option { + match self.code { + KeyCode::Char(ch) => Some(ch), + _ => None, + } + } +} + pub(crate) mod keys { pub(crate) const BACKSPACE: &str = "backspace"; pub(crate) const ENTER: &str = "ret"; @@ -168,7 +178,7 @@ impl std::str::FromStr for KeyEvent { keys::MINUS => KeyCode::Char('-'), keys::SEMICOLON => KeyCode::Char(';'), keys::PERCENT => KeyCode::Char('%'), - single if single.len() == 1 => KeyCode::Char(single.chars().next().unwrap()), + single if single.chars().count() == 1 => KeyCode::Char(single.chars().next().unwrap()), function if function.len() > 1 && function.starts_with('F') => { let function: String = function.chars().skip(1).collect(); let function = str::parse::(&function)?; -- cgit v1.2.3-70-g09d2