on master help sign in
Implement key ordering for info box
commiteb8745db0999a50464ac183baa138c4e511430f2 authorDaniel Ebert <daniel.ebert@protonmail.com> dateMon 1 Nov 2021, 15:50 (5 years ago) parente505bf2
4 files changed+18−10
helix-term/src/keymap.rs+14−6 helix-view/src/info.rs+2−2 helix-view/src/input.rs+1−1 helix-view/src/keyboard.rs+1−1
helix-term/src/keymap.rs+14−6
@@ -5,7 +5,7 @@ use helix_view::{document::Mode, info::Info, input::KeyEvent};
55use serde::Deserialize;
66use std::{
77 borrow::Cow,
8− collections::HashMap,
8+ collections::{BTreeSet, HashMap},
99 ops::{Deref, DerefMut},
1010};
1111
@@ -137,20 +137,28 @@ impl KeyTrieNode {
137137 }
138138
139139 pub fn infobox(&self) -> Info {
140− let mut body: Vec<(&str, Vec<KeyEvent>)> = Vec::with_capacity(self.len());
140+ let mut body: Vec<(&str, BTreeSet<KeyEvent>)> = Vec::with_capacity(self.len());
141141 for (&key, trie) in self.iter() {
142142 let desc = match trie {
143143 KeyTrie::Leaf(cmd) => cmd.doc(),
144144 KeyTrie::Node(n) => n.name(),
145145 };
146146 match body.iter().position(|(d, _)| d == &desc) {
147− // FIXME: multiple keys are ordered randomly (use BTreeSet)
148− Some(pos) => body[pos].1.push(key),
149− None => body.push((desc, vec![key])),
147+ Some(pos) => {
148+ body[pos].1.insert(key);
149+ }
150+ None => {
151+ let mut keys = BTreeSet::new();
152+ keys.insert(key);
153+ body.push((desc, keys));
154+ }
150155 }
151156 }
152157 body.sort_unstable_by_key(|(_, keys)| {
153− self.order.iter().position(|&k| k == keys[0]).unwrap()
158+ self.order
159+ .iter()
160+ .position(|&k| k == *keys.iter().next().unwrap())
161+ .unwrap()
154162 });
155163 let prefix = format!("{} ", self.name());
156164 if body.iter().all(|(desc, _)| desc.starts_with(&prefix)) {
helix-view/src/info.rs+2−2
@@ -1,6 +1,6 @@
11use crate::input::KeyEvent;
22use helix_core::unicode::width::UnicodeWidthStr;
3−use std::fmt::Write;
3+use std::{collections::BTreeSet, fmt::Write};
44
55#[derive(Debug)]
66/// Info box used in editor. Rendering logic will be in other crate.
@@ -16,7 +16,7 @@ pub struct Info {
1616}
1717
1818impl Info {
19− pub fn new(title: &str, body: Vec<(&str, Vec<KeyEvent>)>) -> Info {
19+ pub fn new(title: &str, body: Vec<(&str, BTreeSet<KeyEvent>)>) -> Info {
2020 let body = body
2121 .into_iter()
2222 .map(|(desc, events)| {
helix-view/src/input.rs+1−1
@@ -8,7 +8,7 @@ use crate::keyboard::{KeyCode, KeyModifiers};
88
99/// Represents a key event.
1010// We use a newtype here because we want to customize Deserialize and Display.
11−#[derive(Debug, PartialEq, Eq, PartialOrd, Clone, Copy, Hash)]
11+#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
1212pub struct KeyEvent {
1313 pub code: KeyCode,
1414 pub modifiers: KeyModifiers,
helix-view/src/keyboard.rs+1−1
@@ -54,7 +54,7 @@ impl From<crossterm::event::KeyModifiers> for KeyModifiers {
5454}
5555
5656/// Represents a key.
57−#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
57+#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Hash)]
5858#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5959pub enum KeyCode {
6060 /// Backspace key.