|
|
@ -4,6 +4,7 @@ use helix_core::hashmap;
|
|
|
|
use helix_view::{document::Mode, info::Info, input::KeyEvent};
|
|
|
|
use helix_view::{document::Mode, info::Info, input::KeyEvent};
|
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use std::{
|
|
|
|
use std::{
|
|
|
|
|
|
|
|
borrow::Cow,
|
|
|
|
collections::HashMap,
|
|
|
|
collections::HashMap,
|
|
|
|
ops::{Deref, DerefMut},
|
|
|
|
ops::{Deref, DerefMut},
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -47,13 +48,13 @@ macro_rules! keymap {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
(@trie
|
|
|
|
(@trie
|
|
|
|
{ $label:literal $($($key:literal)|+ => $value:tt,)+ }
|
|
|
|
{ $label:literal $(sticky=$sticky:literal)? $($($key:literal)|+ => $value:tt,)+ }
|
|
|
|
) => {
|
|
|
|
) => {
|
|
|
|
keymap!({ $label $($($key)|+ => $value,)+ })
|
|
|
|
keymap!({ $label $(sticky=$sticky)? $($($key)|+ => $value,)+ })
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
(
|
|
|
|
{ $label:literal $($($key:literal)|+ => $value:tt,)+ }
|
|
|
|
{ $label:literal $(sticky=$sticky:literal)? $($($key:literal)|+ => $value:tt,)+ }
|
|
|
|
) => {
|
|
|
|
) => {
|
|
|
|
// modified from the hashmap! macro
|
|
|
|
// modified from the hashmap! macro
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -70,7 +71,9 @@ macro_rules! keymap {
|
|
|
|
_order.push(_key);
|
|
|
|
_order.push(_key);
|
|
|
|
)+
|
|
|
|
)+
|
|
|
|
)*
|
|
|
|
)*
|
|
|
|
$crate::keymap::KeyTrie::Node($crate::keymap::KeyTrieNode::new($label, _map, _order))
|
|
|
|
let mut _node = $crate::keymap::KeyTrieNode::new($label, _map, _order);
|
|
|
|
|
|
|
|
$( _node.is_sticky = $sticky; )?
|
|
|
|
|
|
|
|
$crate::keymap::KeyTrie::Node(_node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -84,6 +87,8 @@ pub struct KeyTrieNode {
|
|
|
|
map: HashMap<KeyEvent, KeyTrie>,
|
|
|
|
map: HashMap<KeyEvent, KeyTrie>,
|
|
|
|
#[serde(skip)]
|
|
|
|
#[serde(skip)]
|
|
|
|
order: Vec<KeyEvent>,
|
|
|
|
order: Vec<KeyEvent>,
|
|
|
|
|
|
|
|
#[serde(skip)]
|
|
|
|
|
|
|
|
pub is_sticky: bool,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl KeyTrieNode {
|
|
|
|
impl KeyTrieNode {
|
|
|
@ -92,6 +97,7 @@ impl KeyTrieNode {
|
|
|
|
name: name.to_string(),
|
|
|
|
name: name.to_string(),
|
|
|
|
map,
|
|
|
|
map,
|
|
|
|
order,
|
|
|
|
order,
|
|
|
|
|
|
|
|
is_sticky: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -119,12 +125,10 @@ impl KeyTrieNode {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl From<KeyTrieNode> for Info {
|
|
|
|
pub fn infobox(&self) -> Info {
|
|
|
|
fn from(node: KeyTrieNode) -> Self {
|
|
|
|
let mut body: Vec<(&str, Vec<KeyEvent>)> = Vec::with_capacity(self.len());
|
|
|
|
let mut body: Vec<(&str, Vec<KeyEvent>)> = Vec::with_capacity(node.len());
|
|
|
|
for (&key, trie) in self.iter() {
|
|
|
|
for (&key, trie) in node.iter() {
|
|
|
|
|
|
|
|
let desc = match trie {
|
|
|
|
let desc = match trie {
|
|
|
|
KeyTrie::Leaf(cmd) => cmd.doc(),
|
|
|
|
KeyTrie::Leaf(cmd) => cmd.doc(),
|
|
|
|
KeyTrie::Node(n) => n.name(),
|
|
|
|
KeyTrie::Node(n) => n.name(),
|
|
|
@ -136,16 +140,16 @@ impl From<KeyTrieNode> for Info {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
body.sort_unstable_by_key(|(_, keys)| {
|
|
|
|
body.sort_unstable_by_key(|(_, keys)| {
|
|
|
|
node.order.iter().position(|&k| k == keys[0]).unwrap()
|
|
|
|
self.order.iter().position(|&k| k == keys[0]).unwrap()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
let prefix = format!("{} ", node.name());
|
|
|
|
let prefix = format!("{} ", self.name());
|
|
|
|
if body.iter().all(|(desc, _)| desc.starts_with(&prefix)) {
|
|
|
|
if body.iter().all(|(desc, _)| desc.starts_with(&prefix)) {
|
|
|
|
body = body
|
|
|
|
body = body
|
|
|
|
.into_iter()
|
|
|
|
.into_iter()
|
|
|
|
.map(|(desc, keys)| (desc.strip_prefix(&prefix).unwrap(), keys))
|
|
|
|
.map(|(desc, keys)| (desc.strip_prefix(&prefix).unwrap(), keys))
|
|
|
|
.collect();
|
|
|
|
.collect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Info::new(node.name(), body)
|
|
|
|
Info::new(self.name(), body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -218,7 +222,7 @@ impl KeyTrie {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum KeymapResult {
|
|
|
|
pub enum KeymapResultKind {
|
|
|
|
/// Needs more keys to execute a command. Contains valid keys for next keystroke.
|
|
|
|
/// Needs more keys to execute a command. Contains valid keys for next keystroke.
|
|
|
|
Pending(KeyTrieNode),
|
|
|
|
Pending(KeyTrieNode),
|
|
|
|
Matched(Command),
|
|
|
|
Matched(Command),
|
|
|
@ -229,14 +233,31 @@ pub enum KeymapResult {
|
|
|
|
Cancelled(Vec<KeyEvent>),
|
|
|
|
Cancelled(Vec<KeyEvent>),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Returned after looking up a key in [`Keymap`]. The `sticky` field has a
|
|
|
|
|
|
|
|
/// reference to the sticky node if one is currently active.
|
|
|
|
|
|
|
|
pub struct KeymapResult<'a> {
|
|
|
|
|
|
|
|
pub kind: KeymapResultKind,
|
|
|
|
|
|
|
|
pub sticky: Option<&'a KeyTrieNode>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> KeymapResult<'a> {
|
|
|
|
|
|
|
|
pub fn new(kind: KeymapResultKind, sticky: Option<&'a KeyTrieNode>) -> Self {
|
|
|
|
|
|
|
|
Self { kind, sticky }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
|
|
|
pub struct Keymap {
|
|
|
|
pub struct Keymap {
|
|
|
|
/// Always a Node
|
|
|
|
/// Always a Node
|
|
|
|
#[serde(flatten)]
|
|
|
|
#[serde(flatten)]
|
|
|
|
root: KeyTrie,
|
|
|
|
root: KeyTrie,
|
|
|
|
/// Stores pending keys waiting for the next key
|
|
|
|
/// Stores pending keys waiting for the next key. This is relative to a
|
|
|
|
|
|
|
|
/// sticky node if one is in use.
|
|
|
|
#[serde(skip)]
|
|
|
|
#[serde(skip)]
|
|
|
|
state: Vec<KeyEvent>,
|
|
|
|
state: Vec<KeyEvent>,
|
|
|
|
|
|
|
|
/// Stores the sticky node if one is activated.
|
|
|
|
|
|
|
|
#[serde(skip)]
|
|
|
|
|
|
|
|
sticky: Option<KeyTrieNode>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Keymap {
|
|
|
|
impl Keymap {
|
|
|
@ -244,6 +265,7 @@ impl Keymap {
|
|
|
|
Keymap {
|
|
|
|
Keymap {
|
|
|
|
root,
|
|
|
|
root,
|
|
|
|
state: Vec::new(),
|
|
|
|
state: Vec::new(),
|
|
|
|
|
|
|
|
sticky: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -251,27 +273,60 @@ impl Keymap {
|
|
|
|
&self.root
|
|
|
|
&self.root
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn sticky(&self) -> Option<&KeyTrieNode> {
|
|
|
|
|
|
|
|
self.sticky.as_ref()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns list of keys waiting to be disambiguated.
|
|
|
|
/// Returns list of keys waiting to be disambiguated.
|
|
|
|
pub fn pending(&self) -> &[KeyEvent] {
|
|
|
|
pub fn pending(&self) -> &[KeyEvent] {
|
|
|
|
&self.state
|
|
|
|
&self.state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Lookup `key` in the keymap to try and find a command to execute
|
|
|
|
/// Lookup `key` in the keymap to try and find a command to execute. Escape
|
|
|
|
|
|
|
|
/// key cancels pending keystrokes. If there are no pending keystrokes but a
|
|
|
|
|
|
|
|
/// sticky node is in use, it will be cleared.
|
|
|
|
pub fn get(&mut self, key: KeyEvent) -> KeymapResult {
|
|
|
|
pub fn get(&mut self, key: KeyEvent) -> KeymapResult {
|
|
|
|
let &first = self.state.get(0).unwrap_or(&key);
|
|
|
|
if let key!(Esc) = key {
|
|
|
|
let trie = match self.root.search(&[first]) {
|
|
|
|
if self.state.is_empty() {
|
|
|
|
Some(&KeyTrie::Leaf(cmd)) => return KeymapResult::Matched(cmd),
|
|
|
|
self.sticky = None;
|
|
|
|
None => return KeymapResult::NotFound,
|
|
|
|
}
|
|
|
|
|
|
|
|
return KeymapResult::new(
|
|
|
|
|
|
|
|
KeymapResultKind::Cancelled(self.state.drain(..).collect()),
|
|
|
|
|
|
|
|
self.sticky(),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let first = self.state.get(0).unwrap_or(&key);
|
|
|
|
|
|
|
|
let trie_node = match self.sticky {
|
|
|
|
|
|
|
|
Some(ref trie) => Cow::Owned(KeyTrie::Node(trie.clone())),
|
|
|
|
|
|
|
|
None => Cow::Borrowed(&self.root),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let trie = match trie_node.search(&[*first]) {
|
|
|
|
|
|
|
|
Some(&KeyTrie::Leaf(cmd)) => {
|
|
|
|
|
|
|
|
return KeymapResult::new(KeymapResultKind::Matched(cmd), self.sticky())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
None => return KeymapResult::new(KeymapResultKind::NotFound, self.sticky()),
|
|
|
|
Some(t) => t,
|
|
|
|
Some(t) => t,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
self.state.push(key);
|
|
|
|
self.state.push(key);
|
|
|
|
match trie.search(&self.state[1..]) {
|
|
|
|
match trie.search(&self.state[1..]) {
|
|
|
|
Some(&KeyTrie::Node(ref map)) => KeymapResult::Pending(map.clone()),
|
|
|
|
Some(&KeyTrie::Node(ref map)) => {
|
|
|
|
Some(&KeyTrie::Leaf(command)) => {
|
|
|
|
if map.is_sticky {
|
|
|
|
|
|
|
|
self.state.clear();
|
|
|
|
|
|
|
|
self.sticky = Some(map.clone());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
KeymapResult::new(KeymapResultKind::Pending(map.clone()), self.sticky())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(&KeyTrie::Leaf(cmd)) => {
|
|
|
|
self.state.clear();
|
|
|
|
self.state.clear();
|
|
|
|
KeymapResult::Matched(command)
|
|
|
|
return KeymapResult::new(KeymapResultKind::Matched(cmd), self.sticky());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => KeymapResult::Cancelled(self.state.drain(..).collect()),
|
|
|
|
None => KeymapResult::new(
|
|
|
|
|
|
|
|
KeymapResultKind::Cancelled(self.state.drain(..).collect()),
|
|
|
|
|
|
|
|
self.sticky(),
|
|
|
|
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -602,19 +657,19 @@ fn merge_partial_keys() {
|
|
|
|
|
|
|
|
|
|
|
|
let keymap = merged_config.keys.0.get_mut(&Mode::Normal).unwrap();
|
|
|
|
let keymap = merged_config.keys.0.get_mut(&Mode::Normal).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
assert_eq!(
|
|
|
|
keymap.get(key!('i')),
|
|
|
|
keymap.get(key!('i')).kind,
|
|
|
|
KeymapResult::Matched(Command::normal_mode),
|
|
|
|
KeymapResultKind::Matched(Command::normal_mode),
|
|
|
|
"Leaf should replace leaf"
|
|
|
|
"Leaf should replace leaf"
|
|
|
|
);
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
assert_eq!(
|
|
|
|
keymap.get(key!('无')),
|
|
|
|
keymap.get(key!('无')).kind,
|
|
|
|
KeymapResult::Matched(Command::insert_mode),
|
|
|
|
KeymapResultKind::Matched(Command::insert_mode),
|
|
|
|
"New leaf should be present in merged keymap"
|
|
|
|
"New leaf should be present in merged keymap"
|
|
|
|
);
|
|
|
|
);
|
|
|
|
// Assumes that z is a node in the default keymap
|
|
|
|
// Assumes that z is a node in the default keymap
|
|
|
|
assert_eq!(
|
|
|
|
assert_eq!(
|
|
|
|
keymap.get(key!('z')),
|
|
|
|
keymap.get(key!('z')).kind,
|
|
|
|
KeymapResult::Matched(Command::jump_backward),
|
|
|
|
KeymapResultKind::Matched(Command::jump_backward),
|
|
|
|
"Leaf should replace node"
|
|
|
|
"Leaf should replace node"
|
|
|
|
);
|
|
|
|
);
|
|
|
|
// Assumes that `g` is a node in default keymap
|
|
|
|
// Assumes that `g` is a node in default keymap
|
|
|
|