2022-03-25 10:05:20 +01:00
|
|
|
pub mod default;
|
|
|
|
pub mod macros;
|
|
|
|
|
2021-12-04 15:47:18 +01:00
|
|
|
pub use crate::commands::MappableCommand;
|
2021-06-22 19:04:04 +02:00
|
|
|
use crate::config::Config;
|
2022-03-25 10:05:20 +01:00
|
|
|
use arc_swap::{
|
|
|
|
access::{DynAccess, DynGuard},
|
|
|
|
ArcSwap,
|
|
|
|
};
|
2021-07-26 18:07:13 +02:00
|
|
|
use helix_view::{document::Mode, info::Info, input::KeyEvent};
|
2021-06-22 19:04:04 +02:00
|
|
|
use serde::Deserialize;
|
2021-06-17 13:08:05 +02:00
|
|
|
use std::{
|
2021-09-05 05:55:13 +02:00
|
|
|
borrow::Cow,
|
2021-11-01 15:50:12 +01:00
|
|
|
collections::{BTreeSet, HashMap},
|
2021-06-17 13:08:05 +02:00
|
|
|
ops::{Deref, DerefMut},
|
2022-03-25 10:05:20 +01:00
|
|
|
sync::Arc,
|
2021-06-17 13:08:05 +02:00
|
|
|
};
|
2020-06-07 17:28:17 +02:00
|
|
|
|
2022-03-25 10:05:20 +01:00
|
|
|
use default::default;
|
|
|
|
use macros::key;
|
2020-10-05 17:58:16 +02:00
|
|
|
|
2021-10-23 15:24:19 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2021-07-26 18:07:13 +02:00
|
|
|
pub struct KeyTrieNode {
|
|
|
|
/// A label for keys coming under this node, like "Goto mode"
|
|
|
|
name: String,
|
|
|
|
map: HashMap<KeyEvent, KeyTrie>,
|
|
|
|
order: Vec<KeyEvent>,
|
2021-09-05 05:55:13 +02:00
|
|
|
pub is_sticky: bool,
|
2021-07-26 18:07:13 +02:00
|
|
|
}
|
|
|
|
|
2021-10-23 15:24:19 +02:00
|
|
|
impl<'de> Deserialize<'de> for KeyTrieNode {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: serde::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let map = HashMap::<KeyEvent, KeyTrie>::deserialize(deserializer)?;
|
|
|
|
let order = map.keys().copied().collect::<Vec<_>>(); // NOTE: map.keys() has arbitrary order
|
|
|
|
Ok(Self {
|
|
|
|
map,
|
|
|
|
order,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
impl KeyTrieNode {
|
|
|
|
pub fn new(name: &str, map: HashMap<KeyEvent, KeyTrie>, order: Vec<KeyEvent>) -> Self {
|
|
|
|
Self {
|
|
|
|
name: name.to_string(),
|
|
|
|
map,
|
|
|
|
order,
|
2021-09-05 05:55:13 +02:00
|
|
|
is_sticky: false,
|
2021-01-22 09:13:14 +01:00
|
|
|
}
|
2021-07-26 18:07:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Merge another Node in. Leaves and subnodes from the other node replace
|
|
|
|
/// corresponding keyevent in self, except when both other and self have
|
|
|
|
/// subnodes for same key. In that case the merge is recursive.
|
|
|
|
pub fn merge(&mut self, mut other: Self) {
|
|
|
|
for (key, trie) in std::mem::take(&mut other.map) {
|
|
|
|
if let Some(KeyTrie::Node(node)) = self.map.get_mut(&key) {
|
|
|
|
if let KeyTrie::Node(other_node) = trie {
|
|
|
|
node.merge(other_node);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.map.insert(key, trie);
|
|
|
|
}
|
|
|
|
for &key in self.map.keys() {
|
|
|
|
if !self.order.contains(&key) {
|
|
|
|
self.order.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 05:55:13 +02:00
|
|
|
pub fn infobox(&self) -> Info {
|
2021-11-01 15:50:12 +01:00
|
|
|
let mut body: Vec<(&str, BTreeSet<KeyEvent>)> = Vec::with_capacity(self.len());
|
2021-09-05 05:55:13 +02:00
|
|
|
for (&key, trie) in self.iter() {
|
2021-07-26 18:07:13 +02:00
|
|
|
let desc = match trie {
|
2021-11-04 01:50:38 +01:00
|
|
|
KeyTrie::Leaf(cmd) => {
|
|
|
|
if cmd.name() == "no_op" {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cmd.doc()
|
|
|
|
}
|
2021-07-26 18:07:13 +02:00
|
|
|
KeyTrie::Node(n) => n.name(),
|
2021-11-11 05:44:50 +01:00
|
|
|
KeyTrie::Sequence(_) => "[Multiple commands]",
|
2021-07-26 18:07:13 +02:00
|
|
|
};
|
|
|
|
match body.iter().position(|(d, _)| d == &desc) {
|
2021-11-01 15:50:12 +01:00
|
|
|
Some(pos) => {
|
|
|
|
body[pos].1.insert(key);
|
|
|
|
}
|
2021-11-02 17:43:07 +01:00
|
|
|
None => body.push((desc, BTreeSet::from([key]))),
|
2021-07-26 18:07:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
body.sort_unstable_by_key(|(_, keys)| {
|
2021-11-01 15:50:12 +01:00
|
|
|
self.order
|
|
|
|
.iter()
|
|
|
|
.position(|&k| k == *keys.iter().next().unwrap())
|
|
|
|
.unwrap()
|
2021-07-26 18:07:13 +02:00
|
|
|
});
|
2021-09-05 05:55:13 +02:00
|
|
|
let prefix = format!("{} ", self.name());
|
2021-07-26 18:07:13 +02:00
|
|
|
if body.iter().all(|(desc, _)| desc.starts_with(&prefix)) {
|
|
|
|
body = body
|
|
|
|
.into_iter()
|
|
|
|
.map(|(desc, keys)| (desc.strip_prefix(&prefix).unwrap(), keys))
|
|
|
|
.collect();
|
|
|
|
}
|
2021-11-04 19:33:31 +01:00
|
|
|
Info::from_keymap(self.name(), body)
|
2021-07-26 18:07:13 +02:00
|
|
|
}
|
2021-10-17 16:06:08 +02:00
|
|
|
/// Get a reference to the key trie node's order.
|
|
|
|
pub fn order(&self) -> &[KeyEvent] {
|
|
|
|
self.order.as_slice()
|
|
|
|
}
|
2021-07-26 18:07:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for KeyTrieNode {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new("", HashMap::new(), Vec::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for KeyTrieNode {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.map == other.map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for KeyTrieNode {
|
|
|
|
type Target = HashMap<KeyEvent, KeyTrie>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for KeyTrieNode {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 03:30:42 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2021-07-26 18:07:13 +02:00
|
|
|
pub enum KeyTrie {
|
2021-12-04 15:47:18 +01:00
|
|
|
Leaf(MappableCommand),
|
|
|
|
Sequence(Vec<MappableCommand>),
|
2021-07-26 18:07:13 +02:00
|
|
|
Node(KeyTrieNode),
|
|
|
|
}
|
|
|
|
|
2022-09-22 03:30:42 +02:00
|
|
|
impl<'de> Deserialize<'de> for KeyTrie {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: serde::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
deserializer.deserialize_any(KeyTrieVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct KeyTrieVisitor;
|
|
|
|
|
|
|
|
impl<'de> serde::de::Visitor<'de> for KeyTrieVisitor {
|
|
|
|
type Value = KeyTrie;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
write!(formatter, "a command, list of commands, or sub-keymap")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, command: &str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
command
|
|
|
|
.parse::<MappableCommand>()
|
|
|
|
.map(KeyTrie::Leaf)
|
|
|
|
.map_err(E::custom)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::de::SeqAccess<'de>,
|
|
|
|
{
|
|
|
|
let mut commands = Vec::new();
|
|
|
|
while let Some(command) = seq.next_element::<&str>()? {
|
|
|
|
commands.push(
|
|
|
|
command
|
|
|
|
.parse::<MappableCommand>()
|
|
|
|
.map_err(serde::de::Error::custom)?,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Ok(KeyTrie::Sequence(commands))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
|
|
|
|
where
|
|
|
|
M: serde::de::MapAccess<'de>,
|
|
|
|
{
|
|
|
|
let mut mapping = HashMap::new();
|
|
|
|
let mut order = Vec::new();
|
|
|
|
while let Some((key, value)) = map.next_entry::<KeyEvent, KeyTrie>()? {
|
|
|
|
mapping.insert(key, value);
|
|
|
|
order.push(key);
|
|
|
|
}
|
|
|
|
Ok(KeyTrie::Node(KeyTrieNode::new("", mapping, order)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
impl KeyTrie {
|
|
|
|
pub fn node(&self) -> Option<&KeyTrieNode> {
|
|
|
|
match *self {
|
|
|
|
KeyTrie::Node(ref node) => Some(node),
|
2021-11-11 05:44:50 +01:00
|
|
|
KeyTrie::Leaf(_) | KeyTrie::Sequence(_) => None,
|
2021-07-26 18:07:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn node_mut(&mut self) -> Option<&mut KeyTrieNode> {
|
|
|
|
match *self {
|
|
|
|
KeyTrie::Node(ref mut node) => Some(node),
|
2021-11-11 05:44:50 +01:00
|
|
|
KeyTrie::Leaf(_) | KeyTrie::Sequence(_) => None,
|
2021-07-26 18:07:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Merge another KeyTrie in, assuming that this KeyTrie and the other
|
|
|
|
/// are both Nodes. Panics otherwise.
|
|
|
|
pub fn merge_nodes(&mut self, mut other: Self) {
|
|
|
|
let node = std::mem::take(other.node_mut().unwrap());
|
|
|
|
self.node_mut().unwrap().merge(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn search(&self, keys: &[KeyEvent]) -> Option<&KeyTrie> {
|
|
|
|
let mut trie = self;
|
|
|
|
for key in keys {
|
|
|
|
trie = match trie {
|
|
|
|
KeyTrie::Node(map) => map.get(key),
|
|
|
|
// leaf encountered while keys left to process
|
2021-11-11 05:44:50 +01:00
|
|
|
KeyTrie::Leaf(_) | KeyTrie::Sequence(_) => None,
|
2021-07-26 18:07:13 +02:00
|
|
|
}?
|
|
|
|
}
|
|
|
|
Some(trie)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2022-03-20 07:55:11 +01:00
|
|
|
pub enum KeymapResult {
|
2021-07-26 18:07:13 +02:00
|
|
|
/// Needs more keys to execute a command. Contains valid keys for next keystroke.
|
|
|
|
Pending(KeyTrieNode),
|
2021-12-04 15:47:18 +01:00
|
|
|
Matched(MappableCommand),
|
2021-11-11 05:44:50 +01:00
|
|
|
/// Matched a sequence of commands to execute.
|
2021-12-04 15:47:18 +01:00
|
|
|
MatchedSequence(Vec<MappableCommand>),
|
2021-07-26 18:07:13 +02:00
|
|
|
/// Key was not found in the root keymap
|
|
|
|
NotFound,
|
|
|
|
/// Key is invalid in combination with previous keys. Contains keys leading upto
|
|
|
|
/// and including current (invalid) key.
|
|
|
|
Cancelled(Vec<KeyEvent>),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
2022-03-20 07:50:48 +01:00
|
|
|
#[serde(transparent)]
|
2021-07-26 18:07:13 +02:00
|
|
|
pub struct Keymap {
|
|
|
|
/// Always a Node
|
|
|
|
root: KeyTrie,
|
|
|
|
}
|
|
|
|
|
2022-07-02 13:21:27 +02:00
|
|
|
/// A map of command names to keybinds that will execute the command.
|
|
|
|
pub type ReverseKeymap = HashMap<String, Vec<Vec<KeyEvent>>>;
|
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
impl Keymap {
|
|
|
|
pub fn new(root: KeyTrie) -> Self {
|
2022-03-20 07:50:48 +01:00
|
|
|
Keymap { root }
|
2021-07-26 18:07:13 +02:00
|
|
|
}
|
|
|
|
|
2022-07-02 13:21:27 +02:00
|
|
|
pub fn reverse_map(&self) -> ReverseKeymap {
|
2022-02-17 06:03:11 +01:00
|
|
|
// recursively visit all nodes in keymap
|
2022-07-02 13:21:27 +02:00
|
|
|
fn map_node(cmd_map: &mut ReverseKeymap, node: &KeyTrie, keys: &mut Vec<KeyEvent>) {
|
2022-02-17 06:03:11 +01:00
|
|
|
match node {
|
|
|
|
KeyTrie::Leaf(cmd) => match cmd {
|
|
|
|
MappableCommand::Typable { name, .. } => {
|
|
|
|
cmd_map.entry(name.into()).or_default().push(keys.clone())
|
|
|
|
}
|
|
|
|
MappableCommand::Static { name, .. } => cmd_map
|
|
|
|
.entry(name.to_string())
|
|
|
|
.or_default()
|
|
|
|
.push(keys.clone()),
|
|
|
|
},
|
|
|
|
KeyTrie::Node(next) => {
|
|
|
|
for (key, trie) in &next.map {
|
|
|
|
keys.push(*key);
|
|
|
|
map_node(cmd_map, trie, keys);
|
|
|
|
keys.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
KeyTrie::Sequence(_) => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut res = HashMap::new();
|
|
|
|
map_node(&mut res, &self.root, &mut Vec::new());
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
pub fn root(&self) -> &KeyTrie {
|
|
|
|
&self.root
|
|
|
|
}
|
|
|
|
|
2022-03-20 07:50:48 +01:00
|
|
|
pub fn merge(&mut self, other: Self) {
|
|
|
|
self.root.merge_nodes(other.root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Keymap {
|
|
|
|
type Target = KeyTrieNode;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
self.root.node().unwrap()
|
2021-09-05 05:55:13 +02:00
|
|
|
}
|
2022-03-20 07:50:48 +01:00
|
|
|
}
|
2021-09-05 05:55:13 +02:00
|
|
|
|
2022-03-20 07:50:48 +01:00
|
|
|
impl Default for Keymap {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new(KeyTrie::Node(KeyTrieNode::default()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Keymaps {
|
2022-03-25 10:05:20 +01:00
|
|
|
pub map: Box<dyn DynAccess<HashMap<Mode, Keymap>>>,
|
2022-03-20 07:50:48 +01:00
|
|
|
/// Stores pending keys waiting for the next key. This is relative to a
|
|
|
|
/// sticky node if one is in use.
|
|
|
|
state: Vec<KeyEvent>,
|
|
|
|
/// Stores the sticky node if one is activated.
|
|
|
|
pub sticky: Option<KeyTrieNode>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Keymaps {
|
2022-03-25 10:05:20 +01:00
|
|
|
pub fn new(map: Box<dyn DynAccess<HashMap<Mode, Keymap>>>) -> Self {
|
2022-03-20 07:50:48 +01:00
|
|
|
Self {
|
|
|
|
map,
|
|
|
|
state: Vec::new(),
|
|
|
|
sticky: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 10:05:20 +01:00
|
|
|
pub fn map(&self) -> DynGuard<HashMap<Mode, Keymap>> {
|
|
|
|
self.map.load()
|
|
|
|
}
|
|
|
|
|
2022-03-20 07:50:48 +01:00
|
|
|
/// Returns list of keys waiting to be disambiguated in current mode.
|
2021-07-28 06:57:07 +02:00
|
|
|
pub fn pending(&self) -> &[KeyEvent] {
|
|
|
|
&self.state
|
|
|
|
}
|
|
|
|
|
2022-03-20 07:50:48 +01:00
|
|
|
pub fn sticky(&self) -> Option<&KeyTrieNode> {
|
|
|
|
self.sticky.as_ref()
|
|
|
|
}
|
|
|
|
|
2021-09-05 05:55:13 +02:00
|
|
|
/// 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.
|
2022-03-20 07:50:48 +01:00
|
|
|
pub fn get(&mut self, mode: Mode, key: KeyEvent) -> KeymapResult {
|
|
|
|
// TODO: remove the sticky part and look up manually
|
2022-03-25 10:05:20 +01:00
|
|
|
let keymaps = &*self.map();
|
|
|
|
let keymap = &keymaps[&mode];
|
2022-03-20 07:50:48 +01:00
|
|
|
|
2022-01-14 04:25:59 +01:00
|
|
|
if key!(Esc) == key {
|
2021-09-05 14:20:11 +02:00
|
|
|
if !self.state.is_empty() {
|
2022-03-20 07:55:11 +01:00
|
|
|
// Note that Esc is not included here
|
|
|
|
return KeymapResult::Cancelled(self.state.drain(..).collect());
|
2021-09-05 05:55:13 +02:00
|
|
|
}
|
2021-09-05 14:20:11 +02:00
|
|
|
self.sticky = None;
|
2021-09-05 05:55:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let first = self.state.get(0).unwrap_or(&key);
|
|
|
|
let trie_node = match self.sticky {
|
|
|
|
Some(ref trie) => Cow::Owned(KeyTrie::Node(trie.clone())),
|
2022-03-20 07:50:48 +01:00
|
|
|
None => Cow::Borrowed(&keymap.root),
|
2021-09-05 05:55:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let trie = match trie_node.search(&[*first]) {
|
2021-12-04 15:47:18 +01:00
|
|
|
Some(KeyTrie::Leaf(ref cmd)) => {
|
2022-03-20 07:55:11 +01:00
|
|
|
return KeymapResult::Matched(cmd.clone());
|
2021-09-05 05:55:13 +02:00
|
|
|
}
|
2021-12-04 15:47:18 +01:00
|
|
|
Some(KeyTrie::Sequence(ref cmds)) => {
|
2022-03-20 07:55:11 +01:00
|
|
|
return KeymapResult::MatchedSequence(cmds.clone());
|
2021-11-11 05:44:50 +01:00
|
|
|
}
|
2022-03-20 07:55:11 +01:00
|
|
|
None => return KeymapResult::NotFound,
|
2021-07-26 18:07:13 +02:00
|
|
|
Some(t) => t,
|
|
|
|
};
|
2021-09-05 05:55:13 +02:00
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
self.state.push(key);
|
|
|
|
match trie.search(&self.state[1..]) {
|
2021-09-05 05:55:13 +02:00
|
|
|
Some(&KeyTrie::Node(ref map)) => {
|
|
|
|
if map.is_sticky {
|
|
|
|
self.state.clear();
|
|
|
|
self.sticky = Some(map.clone());
|
|
|
|
}
|
2022-03-20 07:55:11 +01:00
|
|
|
KeymapResult::Pending(map.clone())
|
2021-09-05 05:55:13 +02:00
|
|
|
}
|
2021-12-04 15:47:18 +01:00
|
|
|
Some(&KeyTrie::Leaf(ref cmd)) => {
|
2021-07-26 18:07:13 +02:00
|
|
|
self.state.clear();
|
2022-03-20 07:55:11 +01:00
|
|
|
KeymapResult::Matched(cmd.clone())
|
2021-07-26 18:07:13 +02:00
|
|
|
}
|
2021-11-11 05:44:50 +01:00
|
|
|
Some(&KeyTrie::Sequence(ref cmds)) => {
|
|
|
|
self.state.clear();
|
2022-03-20 07:55:11 +01:00
|
|
|
KeymapResult::MatchedSequence(cmds.clone())
|
2021-11-11 05:44:50 +01:00
|
|
|
}
|
2022-03-20 07:55:11 +01:00
|
|
|
None => KeymapResult::Cancelled(self.state.drain(..).collect()),
|
2021-07-26 18:07:13 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-22 19:04:04 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 13:08:05 +02:00
|
|
|
impl Default for Keymaps {
|
2022-01-14 04:25:59 +01:00
|
|
|
fn default() -> Self {
|
2022-03-25 10:05:20 +01:00
|
|
|
Self::new(Box::new(ArcSwap::new(Arc::new(default()))))
|
2021-06-17 13:08:05 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-19 16:59:19 +02:00
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
/// Merge default config keys with user overwritten keys for custom user config.
|
2021-06-22 19:04:04 +02:00
|
|
|
pub fn merge_keys(mut config: Config) -> Config {
|
2022-03-25 10:05:20 +01:00
|
|
|
let mut delta = std::mem::replace(&mut config.keys, default());
|
|
|
|
for (mode, keys) in &mut config.keys {
|
|
|
|
keys.merge(delta.remove(mode).unwrap_or_default())
|
2021-06-19 16:59:19 +02:00
|
|
|
}
|
2021-06-22 19:04:04 +02:00
|
|
|
config
|
2021-06-19 16:59:19 +02:00
|
|
|
}
|
|
|
|
|
2021-10-16 15:22:36 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-03-25 10:05:20 +01:00
|
|
|
use super::macros::keymap;
|
2021-10-16 15:22:36 +02:00
|
|
|
use super::*;
|
2022-03-25 10:05:20 +01:00
|
|
|
use arc_swap::access::Constant;
|
|
|
|
use helix_core::hashmap;
|
2021-11-10 07:15:32 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn duplicate_keys_should_panic() {
|
|
|
|
keymap!({ "Normal mode"
|
|
|
|
"i" => normal_mode,
|
|
|
|
"i" => goto_definition,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn check_duplicate_keys_in_default_keymap() {
|
|
|
|
// will panic on duplicate keys, assumes that `Keymaps` uses keymap! macro
|
|
|
|
Keymaps::default();
|
|
|
|
}
|
|
|
|
|
2021-10-16 15:22:36 +02:00
|
|
|
#[test]
|
|
|
|
fn merge_partial_keys() {
|
|
|
|
let config = Config {
|
2022-03-25 10:05:20 +01:00
|
|
|
keys: hashmap! {
|
2021-10-16 15:22:36 +02:00
|
|
|
Mode::Normal => Keymap::new(
|
|
|
|
keymap!({ "Normal mode"
|
|
|
|
"i" => normal_mode,
|
|
|
|
"无" => insert_mode,
|
|
|
|
"z" => jump_backward,
|
|
|
|
"g" => { "Merge into goto mode"
|
|
|
|
"$" => goto_line_end,
|
|
|
|
"g" => delete_char_forward,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
2022-03-25 10:05:20 +01:00
|
|
|
},
|
2021-10-16 15:22:36 +02:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let mut merged_config = merge_keys(config.clone());
|
|
|
|
assert_ne!(config, merged_config);
|
|
|
|
|
2022-03-25 10:05:20 +01:00
|
|
|
let mut keymap = Keymaps::new(Box::new(Constant(merged_config.keys.clone())));
|
2021-10-16 15:22:36 +02:00
|
|
|
assert_eq!(
|
2022-03-20 07:55:11 +01:00
|
|
|
keymap.get(Mode::Normal, key!('i')),
|
|
|
|
KeymapResult::Matched(MappableCommand::normal_mode),
|
2021-10-16 15:22:36 +02:00
|
|
|
"Leaf should replace leaf"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-03-20 07:55:11 +01:00
|
|
|
keymap.get(Mode::Normal, key!('无')),
|
|
|
|
KeymapResult::Matched(MappableCommand::insert_mode),
|
2021-10-16 15:22:36 +02:00
|
|
|
"New leaf should be present in merged keymap"
|
|
|
|
);
|
|
|
|
// Assumes that z is a node in the default keymap
|
|
|
|
assert_eq!(
|
2022-03-20 07:55:11 +01:00
|
|
|
keymap.get(Mode::Normal, key!('z')),
|
|
|
|
KeymapResult::Matched(MappableCommand::jump_backward),
|
2021-10-16 15:22:36 +02:00
|
|
|
"Leaf should replace node"
|
|
|
|
);
|
2022-03-20 07:50:48 +01:00
|
|
|
|
2022-03-25 10:05:20 +01:00
|
|
|
let keymap = merged_config.keys.get_mut(&Mode::Normal).unwrap();
|
2021-10-16 15:22:36 +02:00
|
|
|
// Assumes that `g` is a node in default keymap
|
|
|
|
assert_eq!(
|
|
|
|
keymap.root().search(&[key!('g'), key!('$')]).unwrap(),
|
2021-12-04 15:47:18 +01:00
|
|
|
&KeyTrie::Leaf(MappableCommand::goto_line_end),
|
2021-10-16 15:22:36 +02:00
|
|
|
"Leaf should be present in merged subnode"
|
|
|
|
);
|
|
|
|
// Assumes that `gg` is in default keymap
|
|
|
|
assert_eq!(
|
|
|
|
keymap.root().search(&[key!('g'), key!('g')]).unwrap(),
|
2021-12-04 15:47:18 +01:00
|
|
|
&KeyTrie::Leaf(MappableCommand::delete_char_forward),
|
2021-10-16 15:22:36 +02:00
|
|
|
"Leaf should replace old leaf in merged subnode"
|
|
|
|
);
|
|
|
|
// Assumes that `ge` is in default keymap
|
|
|
|
assert_eq!(
|
|
|
|
keymap.root().search(&[key!('g'), key!('e')]).unwrap(),
|
2021-12-04 15:47:18 +01:00
|
|
|
&KeyTrie::Leaf(MappableCommand::goto_last_line),
|
2021-10-16 15:22:36 +02:00
|
|
|
"Old leaves in subnode should be present in merged node"
|
|
|
|
);
|
|
|
|
|
2022-03-25 10:05:20 +01:00
|
|
|
assert!(merged_config.keys.get(&Mode::Normal).unwrap().len() > 1);
|
|
|
|
assert!(merged_config.keys.get(&Mode::Insert).unwrap().len() > 0);
|
2021-10-16 15:22:36 +02:00
|
|
|
}
|
2021-10-17 16:06:08 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn order_should_be_set() {
|
|
|
|
let config = Config {
|
2022-03-25 10:05:20 +01:00
|
|
|
keys: hashmap! {
|
2021-10-17 16:06:08 +02:00
|
|
|
Mode::Normal => Keymap::new(
|
|
|
|
keymap!({ "Normal mode"
|
|
|
|
"space" => { ""
|
|
|
|
"s" => { ""
|
|
|
|
"v" => vsplit,
|
|
|
|
"c" => hsplit,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
2022-03-25 10:05:20 +01:00
|
|
|
},
|
2021-10-17 16:06:08 +02:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let mut merged_config = merge_keys(config.clone());
|
|
|
|
assert_ne!(config, merged_config);
|
2022-03-25 10:05:20 +01:00
|
|
|
let keymap = merged_config.keys.get_mut(&Mode::Normal).unwrap();
|
2021-10-17 16:06:08 +02:00
|
|
|
// Make sure mapping works
|
|
|
|
assert_eq!(
|
|
|
|
keymap
|
|
|
|
.root()
|
|
|
|
.search(&[key!(' '), key!('s'), key!('v')])
|
|
|
|
.unwrap(),
|
2021-12-04 15:47:18 +01:00
|
|
|
&KeyTrie::Leaf(MappableCommand::vsplit),
|
2021-10-17 16:06:08 +02:00
|
|
|
"Leaf should be present in merged subnode"
|
|
|
|
);
|
|
|
|
// Make sure an order was set during merge
|
|
|
|
let node = keymap.root().search(&[crate::key!(' ')]).unwrap();
|
|
|
|
assert!(!node.node().unwrap().order().is_empty())
|
|
|
|
}
|
2021-11-10 16:58:35 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn aliased_modes_are_same_in_default_keymap() {
|
2022-03-25 10:05:20 +01:00
|
|
|
let keymaps = Keymaps::default().map();
|
|
|
|
let root = keymaps.get(&Mode::Normal).unwrap().root();
|
2021-11-10 16:58:35 +01:00
|
|
|
assert_eq!(
|
|
|
|
root.search(&[key!(' '), key!('w')]).unwrap(),
|
|
|
|
root.search(&["C-w".parse::<KeyEvent>().unwrap()]).unwrap(),
|
|
|
|
"Mismatch for window mode on `Space-w` and `Ctrl-w`"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
root.search(&[key!('z')]).unwrap(),
|
|
|
|
root.search(&[key!('Z')]).unwrap(),
|
|
|
|
"Mismatch for view mode on `z` and `Z`"
|
|
|
|
);
|
|
|
|
}
|
2022-02-17 06:03:11 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reverse_map() {
|
|
|
|
let normal_mode = keymap!({ "Normal mode"
|
|
|
|
"i" => insert_mode,
|
|
|
|
"g" => { "Goto"
|
|
|
|
"g" => goto_file_start,
|
|
|
|
"e" => goto_file_end,
|
|
|
|
},
|
|
|
|
"j" | "k" => move_line_down,
|
|
|
|
});
|
|
|
|
let keymap = Keymap::new(normal_mode);
|
|
|
|
let mut reverse_map = keymap.reverse_map();
|
|
|
|
|
|
|
|
// sort keybindings in order to have consistent tests
|
|
|
|
// HashMaps can be compared but we can still get different ordering of bindings
|
|
|
|
// for commands that have multiple bindings assigned
|
|
|
|
for v in reverse_map.values_mut() {
|
|
|
|
v.sort()
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
reverse_map,
|
|
|
|
HashMap::from([
|
|
|
|
("insert_mode".to_string(), vec![vec![key!('i')]]),
|
|
|
|
(
|
|
|
|
"goto_file_start".to_string(),
|
|
|
|
vec![vec![key!('g'), key!('g')]]
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"goto_file_end".to_string(),
|
|
|
|
vec![vec![key!('g'), key!('e')]]
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"move_line_down".to_string(),
|
|
|
|
vec![vec![key!('j')], vec![key!('k')]]
|
|
|
|
),
|
|
|
|
]),
|
2022-06-01 19:01:37 +02:00
|
|
|
"Mismatch"
|
2022-02-17 06:03:11 +01:00
|
|
|
)
|
|
|
|
}
|
2021-06-19 16:59:19 +02:00
|
|
|
}
|