Skip to content

Commit

Permalink
keymap: Allow backslash escaping in commands (helix-editor#5144)
Browse files Browse the repository at this point in the history
  • Loading branch information
ds-cbo committed Dec 16, 2022
1 parent ec9aa66 commit 911279a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
25 changes: 19 additions & 6 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,18 @@ impl MappableCommand {

impl fmt::Debug for MappableCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("MappableCommand")
.field(&self.name())
.finish()
match self {
MappableCommand::Static { name, .. } => {
f.debug_tuple("MappableCommand").field(name).finish()
}
MappableCommand::Typable { name, args, .. } => {
//
f.debug_tuple("MappableCommand")
.field(name)
.field(args)
.finish()
}
}
}
}

Expand Down Expand Up @@ -506,12 +515,16 @@ impl PartialEq for MappableCommand {
match (self, other) {
(
MappableCommand::Typable {
name: first_name, ..
name: first_name,
args: first_args,
..
},
MappableCommand::Typable {
name: second_name, ..
name: second_name,
args: second_args,
..
},
) => first_name == second_name,
) => first_name == second_name && first_args == second_args,
(
MappableCommand::Static {
name: first_name, ..
Expand Down
41 changes: 40 additions & 1 deletion helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl<'de> serde::de::Visitor<'de> for KeyTrieVisitor {
S: serde::de::SeqAccess<'de>,
{
let mut commands = Vec::new();
while let Some(command) = seq.next_element::<&str>()? {
while let Some(command) = seq.next_element::<Cow<str>>()? {
commands.push(
command
.parse::<MappableCommand>()
Expand Down Expand Up @@ -600,4 +600,43 @@ mod tests {
"Mismatch"
)
}

#[test]
fn escaped_keymap() {
use crate::commands::MappableCommand;
use helix_view::input::{KeyCode, KeyEvent, KeyModifiers};

let keys = r#"
"+" = [
"select_all",
":pipe sed -E 's/\\s+$//g'",
]
"#;

let key = KeyEvent {
code: KeyCode::Char('+'),
modifiers: KeyModifiers::NONE,
};

let expectation = Keymap::new(KeyTrie::Node(KeyTrieNode::new(
"",
hashmap! {
key => KeyTrie::Sequence(vec!{
MappableCommand::select_all,
MappableCommand::Typable {
name: "pipe".to_owned(),
args: vec!{
"sed".to_owned(),
"-E".to_owned(),
"'s/\\s+$//g'".to_owned()
},
doc: "".to_owned(),
},
})
},
vec![key],
)));

assert_eq!(toml::from_str(keys), Ok(expectation));
}
}

0 comments on commit 911279a

Please sign in to comment.