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;
|
2020-10-23 06:51:08 +02:00
|
|
|
use helix_core::hashmap;
|
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},
|
|
|
|
};
|
2020-06-07 17:28:17 +02:00
|
|
|
|
2021-03-30 11:19:27 +02:00
|
|
|
#[macro_export]
|
2020-10-01 09:06:42 +02:00
|
|
|
macro_rules! key {
|
2021-06-14 11:28:56 +02:00
|
|
|
($key:ident) => {
|
2021-10-17 16:06:21 +02:00
|
|
|
::helix_view::input::KeyEvent {
|
2021-06-29 09:13:30 +02:00
|
|
|
code: ::helix_view::keyboard::KeyCode::$key,
|
|
|
|
modifiers: ::helix_view::keyboard::KeyModifiers::NONE,
|
2021-06-14 11:28:56 +02:00
|
|
|
}
|
|
|
|
};
|
2021-03-30 11:19:27 +02:00
|
|
|
($($ch:tt)*) => {
|
2021-10-17 16:06:21 +02:00
|
|
|
::helix_view::input::KeyEvent {
|
2021-06-29 09:13:30 +02:00
|
|
|
code: ::helix_view::keyboard::KeyCode::Char($($ch)*),
|
|
|
|
modifiers: ::helix_view::keyboard::KeyModifiers::NONE,
|
2020-10-01 09:06:42 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-12 08:21:03 +01:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! shift {
|
|
|
|
($key:ident) => {
|
|
|
|
::helix_view::input::KeyEvent {
|
|
|
|
code: ::helix_view::keyboard::KeyCode::$key,
|
|
|
|
modifiers: ::helix_view::keyboard::KeyModifiers::SHIFT,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
($($ch:tt)*) => {
|
|
|
|
::helix_view::input::KeyEvent {
|
|
|
|
code: ::helix_view::keyboard::KeyCode::Char($($ch)*),
|
|
|
|
modifiers: ::helix_view::keyboard::KeyModifiers::SHIFT,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-10 16:58:46 +01:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! ctrl {
|
|
|
|
($key:ident) => {
|
|
|
|
::helix_view::input::KeyEvent {
|
|
|
|
code: ::helix_view::keyboard::KeyCode::$key,
|
|
|
|
modifiers: ::helix_view::keyboard::KeyModifiers::CONTROL,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
($($ch:tt)*) => {
|
|
|
|
::helix_view::input::KeyEvent {
|
|
|
|
code: ::helix_view::keyboard::KeyCode::Char($($ch)*),
|
|
|
|
modifiers: ::helix_view::keyboard::KeyModifiers::CONTROL,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! alt {
|
|
|
|
($key:ident) => {
|
|
|
|
::helix_view::input::KeyEvent {
|
|
|
|
code: ::helix_view::keyboard::KeyCode::$key,
|
|
|
|
modifiers: ::helix_view::keyboard::KeyModifiers::ALT,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
($($ch:tt)*) => {
|
|
|
|
::helix_view::input::KeyEvent {
|
|
|
|
code: ::helix_view::keyboard::KeyCode::Char($($ch)*),
|
|
|
|
modifiers: ::helix_view::keyboard::KeyModifiers::ALT,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
/// Macro for defining the root of a `Keymap` object. Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use helix_core::hashmap;
|
|
|
|
/// # use helix_term::keymap;
|
|
|
|
/// # use helix_term::keymap::Keymap;
|
|
|
|
/// let normal_mode = keymap!({ "Normal mode"
|
|
|
|
/// "i" => insert_mode,
|
|
|
|
/// "g" => { "Goto"
|
|
|
|
/// "g" => goto_file_start,
|
|
|
|
/// "e" => goto_file_end,
|
|
|
|
/// },
|
|
|
|
/// "j" | "down" => move_line_down,
|
|
|
|
/// });
|
|
|
|
/// let keymap = Keymap::new(normal_mode);
|
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! keymap {
|
|
|
|
(@trie $cmd:ident) => {
|
2021-12-04 15:47:18 +01:00
|
|
|
$crate::keymap::KeyTrie::Leaf($crate::commands::MappableCommand::$cmd)
|
2021-07-26 18:07:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
(@trie
|
2021-09-05 05:55:13 +02:00
|
|
|
{ $label:literal $(sticky=$sticky:literal)? $($($key:literal)|+ => $value:tt,)+ }
|
2021-07-26 18:07:13 +02:00
|
|
|
) => {
|
2021-09-05 05:55:13 +02:00
|
|
|
keymap!({ $label $(sticky=$sticky)? $($($key)|+ => $value,)+ })
|
2021-07-26 18:07:13 +02:00
|
|
|
};
|
|
|
|
|
2021-11-11 05:44:50 +01:00
|
|
|
(@trie [$($cmd:ident),* $(,)?]) => {
|
|
|
|
$crate::keymap::KeyTrie::Sequence(vec![$($crate::commands::Command::$cmd),*])
|
|
|
|
};
|
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
(
|
2021-09-05 05:55:13 +02:00
|
|
|
{ $label:literal $(sticky=$sticky:literal)? $($($key:literal)|+ => $value:tt,)+ }
|
2021-07-26 18:07:13 +02:00
|
|
|
) => {
|
|
|
|
// modified from the hashmap! macro
|
|
|
|
{
|
|
|
|
let _cap = hashmap!(@count $($($key),+),*);
|
|
|
|
let mut _map = ::std::collections::HashMap::with_capacity(_cap);
|
|
|
|
let mut _order = ::std::vec::Vec::with_capacity(_cap);
|
|
|
|
$(
|
|
|
|
$(
|
|
|
|
let _key = $key.parse::<::helix_view::input::KeyEvent>().unwrap();
|
2021-11-10 07:15:32 +01:00
|
|
|
let _duplicate = _map.insert(
|
2021-07-26 18:07:13 +02:00
|
|
|
_key,
|
|
|
|
keymap!(@trie $value)
|
|
|
|
);
|
2021-12-05 05:04:10 +01:00
|
|
|
assert!(_duplicate.is_none(), "Duplicate key found: {:?}", _duplicate.unwrap());
|
2021-07-26 18:07:13 +02:00
|
|
|
_order.push(_key);
|
|
|
|
)+
|
|
|
|
)*
|
2021-09-05 05:55:13 +02:00
|
|
|
let mut _node = $crate::keymap::KeyTrieNode::new($label, _map, _order);
|
|
|
|
$( _node.is_sticky = $sticky; )?
|
|
|
|
$crate::keymap::KeyTrie::Node(_node)
|
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);
|
|
|
|
}
|
2021-10-23 15:24:19 +02:00
|
|
|
for &key in self.map.keys() {
|
2021-07-26 18:07:13 +02:00
|
|
|
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-09-05 05:55:13 +02:00
|
|
|
Info::new(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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
|
|
|
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)]
|
2021-09-05 05:55:13 +02:00
|
|
|
pub enum KeymapResultKind {
|
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>),
|
|
|
|
}
|
|
|
|
|
2021-09-05 05:55:13 +02:00
|
|
|
/// Returned after looking up a key in [`Keymap`]. The `sticky` field has a
|
|
|
|
/// reference to the sticky node if one is currently active.
|
2021-10-17 16:06:08 +02:00
|
|
|
#[derive(Debug)]
|
2021-09-05 05:55:13 +02:00
|
|
|
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 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
|
|
|
pub struct Keymap {
|
|
|
|
/// Always a Node
|
|
|
|
#[serde(flatten)]
|
|
|
|
root: KeyTrie,
|
2021-09-05 05:55:13 +02:00
|
|
|
/// Stores pending keys waiting for the next key. This is relative to a
|
|
|
|
/// sticky node if one is in use.
|
2021-07-26 18:07:13 +02:00
|
|
|
#[serde(skip)]
|
|
|
|
state: Vec<KeyEvent>,
|
2021-09-05 05:55:13 +02:00
|
|
|
/// Stores the sticky node if one is activated.
|
|
|
|
#[serde(skip)]
|
|
|
|
sticky: Option<KeyTrieNode>,
|
2021-07-26 18:07:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Keymap {
|
|
|
|
pub fn new(root: KeyTrie) -> Self {
|
|
|
|
Keymap {
|
|
|
|
root,
|
|
|
|
state: Vec::new(),
|
2021-09-05 05:55:13 +02:00
|
|
|
sticky: None,
|
2021-07-26 18:07:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn root(&self) -> &KeyTrie {
|
|
|
|
&self.root
|
|
|
|
}
|
|
|
|
|
2021-09-05 05:55:13 +02:00
|
|
|
pub fn sticky(&self) -> Option<&KeyTrieNode> {
|
|
|
|
self.sticky.as_ref()
|
|
|
|
}
|
|
|
|
|
2021-07-28 06:57:07 +02:00
|
|
|
/// Returns list of keys waiting to be disambiguated.
|
|
|
|
pub fn pending(&self) -> &[KeyEvent] {
|
|
|
|
&self.state
|
|
|
|
}
|
|
|
|
|
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.
|
2021-07-26 18:07:13 +02:00
|
|
|
pub fn get(&mut self, key: KeyEvent) -> KeymapResult {
|
2021-09-05 05:55:13 +02:00
|
|
|
if let key!(Esc) = key {
|
2021-09-05 14:20:11 +02:00
|
|
|
if !self.state.is_empty() {
|
|
|
|
return KeymapResult::new(
|
|
|
|
// Note that Esc is not included here
|
|
|
|
KeymapResultKind::Cancelled(self.state.drain(..).collect()),
|
|
|
|
self.sticky(),
|
|
|
|
);
|
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())),
|
|
|
|
None => Cow::Borrowed(&self.root),
|
|
|
|
};
|
|
|
|
|
|
|
|
let trie = match trie_node.search(&[*first]) {
|
2021-12-04 15:47:18 +01:00
|
|
|
Some(KeyTrie::Leaf(ref cmd)) => {
|
|
|
|
return KeymapResult::new(KeymapResultKind::Matched(cmd.clone()), self.sticky())
|
2021-09-05 05:55:13 +02:00
|
|
|
}
|
2021-12-04 15:47:18 +01:00
|
|
|
Some(KeyTrie::Sequence(ref cmds)) => {
|
2021-11-11 05:44:50 +01:00
|
|
|
return KeymapResult::new(
|
|
|
|
KeymapResultKind::MatchedSequence(cmds.clone()),
|
|
|
|
self.sticky(),
|
|
|
|
)
|
|
|
|
}
|
2021-09-05 05:55:13 +02:00
|
|
|
None => return KeymapResult::new(KeymapResultKind::NotFound, self.sticky()),
|
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());
|
|
|
|
}
|
|
|
|
KeymapResult::new(KeymapResultKind::Pending(map.clone()), self.sticky())
|
|
|
|
}
|
2021-12-04 15:47:18 +01:00
|
|
|
Some(&KeyTrie::Leaf(ref cmd)) => {
|
2021-07-26 18:07:13 +02:00
|
|
|
self.state.clear();
|
2021-12-04 15:47:18 +01:00
|
|
|
return KeymapResult::new(KeymapResultKind::Matched(cmd.clone()), self.sticky());
|
2021-07-26 18:07:13 +02:00
|
|
|
}
|
2021-11-11 05:44:50 +01:00
|
|
|
Some(&KeyTrie::Sequence(ref cmds)) => {
|
|
|
|
self.state.clear();
|
|
|
|
KeymapResult::new(
|
|
|
|
KeymapResultKind::MatchedSequence(cmds.clone()),
|
|
|
|
self.sticky(),
|
|
|
|
)
|
|
|
|
}
|
2021-09-05 05:55:13 +02:00
|
|
|
None => KeymapResult::new(
|
|
|
|
KeymapResultKind::Cancelled(self.state.drain(..).collect()),
|
|
|
|
self.sticky(),
|
|
|
|
),
|
2021-07-26 18:07:13 +02: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 {
|
2021-07-29 21:41:24 +02:00
|
|
|
self.root.node().unwrap()
|
2021-07-26 18:07:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Keymap {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new(KeyTrie::Node(KeyTrieNode::default()))
|
|
|
|
}
|
2021-01-22 09:13:14 +01:00
|
|
|
}
|
2020-10-09 09:58:43 +02:00
|
|
|
|
2021-06-22 19:04:04 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
|
|
|
#[serde(transparent)]
|
2021-07-26 18:07:13 +02:00
|
|
|
pub struct Keymaps(pub HashMap<Mode, Keymap>);
|
2021-06-22 19:04:04 +02:00
|
|
|
|
2021-07-28 06:57:07 +02:00
|
|
|
impl Keymaps {
|
|
|
|
/// Returns list of keys waiting to be disambiguated in current mode.
|
|
|
|
pub fn pending(&self) -> &[KeyEvent] {
|
|
|
|
self.0
|
|
|
|
.values()
|
|
|
|
.find_map(|keymap| match keymap.pending().is_empty() {
|
|
|
|
true => None,
|
|
|
|
false => Some(keymap.pending()),
|
|
|
|
})
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 19:04:04 +02:00
|
|
|
impl Deref for Keymaps {
|
2021-07-26 18:07:13 +02:00
|
|
|
type Target = HashMap<Mode, Keymap>;
|
2021-06-22 19:04:04 +02:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for Keymaps {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 13:08:05 +02:00
|
|
|
impl Default for Keymaps {
|
2021-06-22 19:04:04 +02:00
|
|
|
fn default() -> Keymaps {
|
2021-07-26 18:07:13 +02:00
|
|
|
let normal = keymap!({ "Normal mode"
|
|
|
|
"h" | "left" => move_char_left,
|
|
|
|
"j" | "down" => move_line_down,
|
|
|
|
"k" | "up" => move_line_up,
|
|
|
|
"l" | "right" => move_char_right,
|
|
|
|
|
|
|
|
"t" => find_till_char,
|
|
|
|
"f" => find_next_char,
|
|
|
|
"T" => till_prev_char,
|
|
|
|
"F" => find_prev_char,
|
|
|
|
"r" => replace,
|
|
|
|
"R" => replace_with_yanked,
|
2021-10-24 15:47:10 +02:00
|
|
|
"A-." => repeat_last_motion,
|
2021-07-26 18:07:13 +02:00
|
|
|
|
|
|
|
"~" => switch_case,
|
|
|
|
"`" => switch_to_lowercase,
|
|
|
|
"A-`" => switch_to_uppercase,
|
|
|
|
|
|
|
|
"home" => goto_line_start,
|
|
|
|
"end" => goto_line_end,
|
|
|
|
|
|
|
|
"w" => move_next_word_start,
|
|
|
|
"b" => move_prev_word_start,
|
|
|
|
"e" => move_next_word_end,
|
|
|
|
|
|
|
|
"W" => move_next_long_word_start,
|
|
|
|
"B" => move_prev_long_word_start,
|
|
|
|
"E" => move_next_long_word_end,
|
|
|
|
|
|
|
|
"v" => select_mode,
|
2021-07-28 09:55:34 +02:00
|
|
|
"G" => goto_line,
|
2021-07-26 18:07:13 +02:00
|
|
|
"g" => { "Goto"
|
|
|
|
"g" => goto_file_start,
|
2021-08-03 15:49:40 +02:00
|
|
|
"e" => goto_last_line,
|
2021-11-29 02:53:29 +01:00
|
|
|
"f" => goto_file,
|
2021-07-26 18:07:13 +02:00
|
|
|
"h" => goto_line_start,
|
|
|
|
"l" => goto_line_end,
|
|
|
|
"s" => goto_first_nonwhitespace,
|
|
|
|
"d" => goto_definition,
|
|
|
|
"y" => goto_type_definition,
|
|
|
|
"r" => goto_reference,
|
|
|
|
"i" => goto_implementation,
|
|
|
|
"t" => goto_window_top,
|
2021-12-02 05:46:57 +01:00
|
|
|
"c" => goto_window_center,
|
2021-07-26 18:07:13 +02:00
|
|
|
"b" => goto_window_bottom,
|
|
|
|
"a" => goto_last_accessed_file,
|
2021-12-02 05:46:57 +01:00
|
|
|
"m" => goto_last_modified_file,
|
2021-11-01 20:52:47 +01:00
|
|
|
"n" => goto_next_buffer,
|
|
|
|
"p" => goto_previous_buffer,
|
2021-11-14 16:11:53 +01:00
|
|
|
"." => goto_last_modification,
|
2021-07-26 18:07:13 +02:00
|
|
|
},
|
|
|
|
":" => command_mode,
|
|
|
|
|
|
|
|
"i" => insert_mode,
|
|
|
|
"I" => prepend_to_line,
|
|
|
|
"a" => append_mode,
|
|
|
|
"A" => append_to_line,
|
|
|
|
"o" => open_below,
|
|
|
|
"O" => open_above,
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
"d" => delete_selection,
|
2021-11-24 08:46:40 +01:00
|
|
|
"A-d" => delete_selection_noyank,
|
2021-07-26 18:07:13 +02:00
|
|
|
"c" => change_selection,
|
2021-11-24 08:46:40 +01:00
|
|
|
"A-c" => change_selection_noyank,
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-07-17 14:09:46 +02:00
|
|
|
"C" => copy_selection_on_next_line,
|
2021-08-04 04:55:01 +02:00
|
|
|
"A-C" => copy_selection_on_prev_line,
|
2021-07-17 14:09:46 +02:00
|
|
|
|
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
"s" => select_regex,
|
|
|
|
"A-s" => split_selection_on_newline,
|
|
|
|
"S" => split_selection,
|
|
|
|
";" => collapse_selection,
|
|
|
|
"A-;" => flip_selections,
|
|
|
|
"%" => select_all,
|
|
|
|
"x" => extend_line,
|
|
|
|
"X" => extend_to_line_bounds,
|
2021-07-05 03:12:34 +02:00
|
|
|
// crop_to_whole_line
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
"m" => { "Match"
|
|
|
|
"m" => match_brackets,
|
|
|
|
"s" => surround_add,
|
|
|
|
"r" => surround_replace,
|
|
|
|
"d" => surround_delete,
|
|
|
|
"a" => select_textobject_around,
|
|
|
|
"i" => select_textobject_inner,
|
|
|
|
},
|
|
|
|
"[" => { "Left bracket"
|
|
|
|
"d" => goto_prev_diag,
|
|
|
|
"D" => goto_first_diag,
|
2021-09-01 17:55:16 +02:00
|
|
|
"space" => add_newline_above,
|
2021-07-26 18:07:13 +02:00
|
|
|
},
|
|
|
|
"]" => { "Right bracket"
|
|
|
|
"d" => goto_next_diag,
|
|
|
|
"D" => goto_last_diag,
|
2021-09-01 17:55:16 +02:00
|
|
|
"space" => add_newline_below,
|
2021-07-26 18:07:13 +02:00
|
|
|
},
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
"/" => search,
|
2021-11-06 09:33:30 +01:00
|
|
|
"?" => rsearch,
|
2021-07-26 18:07:13 +02:00
|
|
|
"n" => search_next,
|
2021-11-06 09:33:30 +01:00
|
|
|
"N" => search_prev,
|
2021-07-26 18:07:13 +02:00
|
|
|
"*" => search_selection,
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
"u" => undo,
|
|
|
|
"U" => redo,
|
2021-11-14 16:16:47 +01:00
|
|
|
"A-u" => earlier,
|
|
|
|
"A-U" => later,
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
"y" => yank,
|
2021-06-17 13:08:05 +02:00
|
|
|
// yank_all
|
2021-07-26 18:07:13 +02:00
|
|
|
"p" => paste_after,
|
2021-06-17 13:08:05 +02:00
|
|
|
// paste_all
|
2021-07-26 18:07:13 +02:00
|
|
|
"P" => paste_before,
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-12-12 13:16:48 +01:00
|
|
|
"q" => record_macro,
|
|
|
|
"Q" => play_macro,
|
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
">" => indent,
|
|
|
|
"<" => unindent,
|
|
|
|
"=" => format_selections,
|
|
|
|
"J" => join_selections,
|
|
|
|
"K" => keep_selections,
|
2021-11-12 01:34:08 +01:00
|
|
|
"A-K" => remove_selections,
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-09-08 09:53:10 +02:00
|
|
|
"," => keep_primary_selection,
|
2021-09-21 17:51:49 +02:00
|
|
|
"A-," => remove_primary_selection,
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
// "q" => record_macro,
|
|
|
|
// "Q" => replay_macro,
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-11-23 15:08:05 +01:00
|
|
|
"&" => align_selections,
|
2021-11-14 16:16:20 +01:00
|
|
|
"_" => trim_selections,
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-08-06 04:22:01 +02:00
|
|
|
"(" => rotate_selections_backward,
|
|
|
|
")" => rotate_selections_forward,
|
2021-08-08 06:26:13 +02:00
|
|
|
"A-(" => rotate_selection_contents_backward,
|
|
|
|
"A-)" => rotate_selection_contents_forward,
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
"esc" => normal_mode,
|
|
|
|
"C-b" | "pageup" => page_up,
|
|
|
|
"C-f" | "pagedown" => page_down,
|
|
|
|
"C-u" => half_page_up,
|
|
|
|
"C-d" => half_page_down,
|
|
|
|
|
|
|
|
"C-w" => { "Window"
|
|
|
|
"C-w" | "w" => rotate_view,
|
2021-10-23 13:06:40 +02:00
|
|
|
"C-s" | "s" => hsplit,
|
2021-07-26 18:07:13 +02:00
|
|
|
"C-v" | "v" => vsplit,
|
2021-11-29 02:53:29 +01:00
|
|
|
"f" => goto_file_hsplit,
|
|
|
|
"F" => goto_file_vsplit,
|
2021-07-26 18:07:13 +02:00
|
|
|
"C-q" | "q" => wclose,
|
2021-11-11 03:32:23 +01:00
|
|
|
"C-o" | "o" => wonly,
|
2021-10-30 03:48:00 +02:00
|
|
|
"C-h" | "h" | "left" => jump_view_left,
|
|
|
|
"C-j" | "j" | "down" => jump_view_down,
|
|
|
|
"C-k" | "k" | "up" => jump_view_up,
|
|
|
|
"C-l" | "l" | "right" => jump_view_right,
|
2021-07-26 18:07:13 +02:00
|
|
|
},
|
2021-06-17 13:08:05 +02:00
|
|
|
|
|
|
|
// move under <space>c
|
2021-07-26 18:07:13 +02:00
|
|
|
"C-c" => toggle_comments,
|
2021-06-17 13:08:05 +02:00
|
|
|
|
|
|
|
// z family for save/restore/combine from/to sels from register
|
|
|
|
|
2021-08-08 06:50:03 +02:00
|
|
|
"tab" => jump_forward, // tab == <C-i>
|
2021-07-26 18:07:13 +02:00
|
|
|
"C-o" => jump_backward,
|
2021-12-10 03:46:24 +01:00
|
|
|
"C-s" => save_selection,
|
2021-07-26 18:07:13 +02:00
|
|
|
|
|
|
|
"space" => { "Space"
|
|
|
|
"f" => file_picker,
|
|
|
|
"b" => buffer_picker,
|
|
|
|
"s" => symbol_picker,
|
2021-11-14 16:12:56 +01:00
|
|
|
"S" => workspace_symbol_picker,
|
2021-07-26 18:07:13 +02:00
|
|
|
"a" => code_action,
|
|
|
|
"'" => last_picker,
|
|
|
|
"w" => { "Window"
|
|
|
|
"C-w" | "w" => rotate_view,
|
2021-11-10 03:04:03 +01:00
|
|
|
"C-s" | "s" => hsplit,
|
2021-07-26 18:07:13 +02:00
|
|
|
"C-v" | "v" => vsplit,
|
2021-11-29 02:53:29 +01:00
|
|
|
"f" => goto_file_hsplit,
|
|
|
|
"F" => goto_file_vsplit,
|
2021-07-26 18:07:13 +02:00
|
|
|
"C-q" | "q" => wclose,
|
2021-11-11 03:32:23 +01:00
|
|
|
"C-o" | "o" => wonly,
|
2021-11-10 03:04:03 +01:00
|
|
|
"C-h" | "h" | "left" => jump_view_left,
|
|
|
|
"C-j" | "j" | "down" => jump_view_down,
|
|
|
|
"C-k" | "k" | "up" => jump_view_up,
|
|
|
|
"C-l" | "l" | "right" => jump_view_right,
|
2021-07-26 18:07:13 +02:00
|
|
|
},
|
|
|
|
"y" => yank_joined_to_clipboard,
|
|
|
|
"Y" => yank_main_selection_to_clipboard,
|
|
|
|
"p" => paste_clipboard_after,
|
|
|
|
"P" => paste_clipboard_before,
|
|
|
|
"R" => replace_selections_with_clipboard,
|
2021-09-21 18:03:12 +02:00
|
|
|
"/" => global_search,
|
2021-09-08 10:21:10 +02:00
|
|
|
"k" => hover,
|
2021-11-08 16:17:54 +01:00
|
|
|
"r" => rename_symbol,
|
2021-07-26 18:07:13 +02:00
|
|
|
},
|
|
|
|
"z" => { "View"
|
|
|
|
"z" | "c" => align_view_center,
|
|
|
|
"t" => align_view_top,
|
|
|
|
"b" => align_view_bottom,
|
|
|
|
"m" => align_view_middle,
|
2021-11-12 01:48:37 +01:00
|
|
|
"k" | "up" => scroll_up,
|
|
|
|
"j" | "down" => scroll_down,
|
2021-11-09 14:03:15 +01:00
|
|
|
"C-b" | "pageup" => page_up,
|
|
|
|
"C-f" | "pagedown" => page_down,
|
|
|
|
"C-u" => half_page_up,
|
|
|
|
"C-d" => half_page_down,
|
2021-09-10 16:14:23 +02:00
|
|
|
},
|
|
|
|
"Z" => { "View" sticky=true
|
|
|
|
"z" | "c" => align_view_center,
|
|
|
|
"t" => align_view_top,
|
|
|
|
"b" => align_view_bottom,
|
|
|
|
"m" => align_view_middle,
|
2021-11-12 01:48:37 +01:00
|
|
|
"k" | "up" => scroll_up,
|
|
|
|
"j" | "down" => scroll_down,
|
2021-11-09 14:03:15 +01:00
|
|
|
"C-b" | "pageup" => page_up,
|
|
|
|
"C-f" | "pagedown" => page_down,
|
|
|
|
"C-u" => half_page_up,
|
|
|
|
"C-d" => half_page_down,
|
2021-07-26 18:07:13 +02:00
|
|
|
},
|
2021-06-17 13:08:05 +02:00
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
"\"" => select_register,
|
2021-08-31 11:13:16 +02:00
|
|
|
"|" => shell_pipe,
|
|
|
|
"A-|" => shell_pipe_to,
|
|
|
|
"!" => shell_insert_output,
|
|
|
|
"A-!" => shell_append_output,
|
|
|
|
"$" => shell_keep_pipe,
|
2021-07-17 16:47:08 +02:00
|
|
|
"C-z" => suspend,
|
2021-11-15 16:32:58 +01:00
|
|
|
|
|
|
|
"C-a" => increment,
|
|
|
|
"C-x" => decrement,
|
2021-07-26 18:07:13 +02:00
|
|
|
});
|
2021-06-17 13:08:05 +02:00
|
|
|
let mut select = normal.clone();
|
2021-07-26 18:07:13 +02:00
|
|
|
select.merge_nodes(keymap!({ "Select mode"
|
|
|
|
"h" | "left" => extend_char_left,
|
|
|
|
"j" | "down" => extend_line_down,
|
|
|
|
"k" | "up" => extend_line_up,
|
|
|
|
"l" | "right" => extend_char_right,
|
|
|
|
|
|
|
|
"w" => extend_next_word_start,
|
|
|
|
"b" => extend_prev_word_start,
|
|
|
|
"e" => extend_next_word_end,
|
2021-09-04 15:30:32 +02:00
|
|
|
"W" => extend_next_long_word_start,
|
|
|
|
"B" => extend_prev_long_word_start,
|
|
|
|
"E" => extend_next_long_word_end,
|
2021-07-26 18:07:13 +02:00
|
|
|
|
2021-11-06 09:33:30 +01:00
|
|
|
"n" => extend_search_next,
|
|
|
|
"N" => extend_search_prev,
|
|
|
|
|
2021-07-26 18:07:13 +02:00
|
|
|
"t" => extend_till_char,
|
|
|
|
"f" => extend_next_char,
|
|
|
|
"T" => extend_till_prev_char,
|
|
|
|
"F" => extend_prev_char,
|
|
|
|
|
2021-09-07 16:22:39 +02:00
|
|
|
"home" => extend_to_line_start,
|
|
|
|
"end" => extend_to_line_end,
|
2021-07-26 18:07:13 +02:00
|
|
|
"esc" => exit_select_mode,
|
2021-08-27 03:03:49 +02:00
|
|
|
|
|
|
|
"v" => normal_mode,
|
2021-07-26 18:07:13 +02:00
|
|
|
}));
|
|
|
|
let insert = keymap!({ "Insert mode"
|
|
|
|
"esc" => normal_mode,
|
|
|
|
|
|
|
|
"backspace" => delete_char_backward,
|
2021-11-15 16:31:20 +01:00
|
|
|
"C-h" => delete_char_backward,
|
2021-07-26 18:07:13 +02:00
|
|
|
"del" => delete_char_forward,
|
2021-11-15 16:31:20 +01:00
|
|
|
"C-d" => delete_char_forward,
|
2021-07-26 18:07:13 +02:00
|
|
|
"ret" => insert_newline,
|
|
|
|
"tab" => insert_tab,
|
|
|
|
"C-w" => delete_word_backward,
|
2021-11-15 16:31:20 +01:00
|
|
|
"A-d" => delete_word_forward,
|
2021-07-26 18:07:13 +02:00
|
|
|
|
|
|
|
"left" => move_char_left,
|
2021-11-15 16:31:20 +01:00
|
|
|
"C-b" => move_char_left,
|
2021-07-26 18:07:13 +02:00
|
|
|
"down" => move_line_down,
|
2021-11-15 16:31:20 +01:00
|
|
|
"C-n" => move_line_down,
|
2021-07-26 18:07:13 +02:00
|
|
|
"up" => move_line_up,
|
2021-11-15 16:31:20 +01:00
|
|
|
"C-p" => move_line_up,
|
2021-07-26 18:07:13 +02:00
|
|
|
"right" => move_char_right,
|
2021-11-15 16:31:20 +01:00
|
|
|
"C-f" => move_char_right,
|
|
|
|
"A-b" => move_prev_word_end,
|
|
|
|
"A-left" => move_prev_word_end,
|
|
|
|
"A-f" => move_next_word_start,
|
|
|
|
"A-right" => move_next_word_start,
|
2021-07-26 18:07:13 +02:00
|
|
|
"pageup" => page_up,
|
|
|
|
"pagedown" => page_down,
|
|
|
|
"home" => goto_line_start,
|
2021-11-15 16:31:20 +01:00
|
|
|
"C-a" => goto_line_start,
|
2021-07-26 18:07:13 +02:00
|
|
|
"end" => goto_line_end_newline,
|
2021-11-15 16:31:20 +01:00
|
|
|
"C-e" => goto_line_end_newline,
|
|
|
|
|
|
|
|
"C-k" => kill_to_line_end,
|
|
|
|
"C-u" => kill_to_line_start,
|
2021-07-26 18:07:13 +02:00
|
|
|
|
|
|
|
"C-x" => completion,
|
2021-11-15 16:31:20 +01:00
|
|
|
"C-r" => insert_register,
|
2021-07-26 18:07:13 +02:00
|
|
|
});
|
2021-06-17 13:08:05 +02:00
|
|
|
Keymaps(hashmap!(
|
2021-07-26 18:07:13 +02:00
|
|
|
Mode::Normal => Keymap::new(normal),
|
|
|
|
Mode::Select => Keymap::new(select),
|
|
|
|
Mode::Insert => Keymap::new(insert),
|
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 {
|
|
|
|
let mut delta = std::mem::take(&mut config.keys);
|
|
|
|
for (mode, keys) in &mut *config.keys {
|
2021-07-26 18:07:13 +02:00
|
|
|
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 {
|
|
|
|
use super::*;
|
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 {
|
|
|
|
keys: Keymaps(hashmap! {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let mut merged_config = merge_keys(config.clone());
|
|
|
|
assert_ne!(config, merged_config);
|
|
|
|
|
|
|
|
let keymap = merged_config.keys.0.get_mut(&Mode::Normal).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
keymap.get(key!('i')).kind,
|
2021-12-04 15:47:18 +01:00
|
|
|
KeymapResultKind::Matched(MappableCommand::normal_mode),
|
2021-10-16 15:22:36 +02:00
|
|
|
"Leaf should replace leaf"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
keymap.get(key!('无')).kind,
|
2021-12-04 15:47:18 +01:00
|
|
|
KeymapResultKind::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!(
|
|
|
|
keymap.get(key!('z')).kind,
|
2021-12-04 15:47:18 +01:00
|
|
|
KeymapResultKind::Matched(MappableCommand::jump_backward),
|
2021-10-16 15:22:36 +02:00
|
|
|
"Leaf should replace node"
|
|
|
|
);
|
|
|
|
// 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"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(merged_config.keys.0.get(&Mode::Normal).unwrap().len() > 1);
|
|
|
|
assert!(merged_config.keys.0.get(&Mode::Insert).unwrap().len() > 0);
|
|
|
|
}
|
2021-10-17 16:06:08 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn order_should_be_set() {
|
|
|
|
let config = Config {
|
|
|
|
keys: Keymaps(hashmap! {
|
|
|
|
Mode::Normal => Keymap::new(
|
|
|
|
keymap!({ "Normal mode"
|
|
|
|
"space" => { ""
|
|
|
|
"s" => { ""
|
|
|
|
"v" => vsplit,
|
|
|
|
"c" => hsplit,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let mut merged_config = merge_keys(config.clone());
|
|
|
|
assert_ne!(config, merged_config);
|
|
|
|
let keymap = merged_config.keys.0.get_mut(&Mode::Normal).unwrap();
|
|
|
|
// 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() {
|
|
|
|
let keymaps = Keymaps::default();
|
|
|
|
let root = keymaps.get(&Mode::Normal).unwrap().root();
|
|
|
|
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`"
|
|
|
|
);
|
|
|
|
}
|
2021-06-19 16:59:19 +02:00
|
|
|
}
|