From 67a287dd818bea14d550951a9660e8b0775bd8d0 Mon Sep 17 00:00:00 2001
From: ds-cbo <82801887+ds-cbo@users.noreply.github.com>
Date: Fri, 16 Dec 2022 15:53:37 +0100
Subject: [PATCH] keymap: Test backslash escaping in commands

---
 helix-term/src/commands.rs | 23 ++++++++++++++++------
 helix-term/src/keymap.rs   | 39 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 6 deletions(-)

diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index e7091401..365d5b65 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -448,9 +448,16 @@ 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(),
+        }
     }
 }
 
@@ -505,12 +512,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, ..
diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs
index 416dcd86..e94a5f66 100644
--- a/helix-term/src/keymap.rs
+++ b/helix-term/src/keymap.rs
@@ -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_string(),
+                        args: vec!{
+                            "sed".to_string(),
+                            "-E".to_string(),
+                            "'s/\\s+$//g'".to_string()
+                        },
+                        doc: "".to_string(),
+                    },
+                })
+            },
+            vec![key],
+        )));
+
+        assert_eq!(toml::from_str(keys), Ok(expectation));
+    }
 }