parse `KeybindKeystroke`

Junkui Zhang created

Change summary

crates/gpui/src/platform/keystroke.rs | 126 ++++++++++++++++++++++++++--
1 file changed, 116 insertions(+), 10 deletions(-)

Detailed changes

crates/gpui/src/platform/keystroke.rs 🔗

@@ -5,16 +5,6 @@ use std::{
     fmt::{Display, Write},
 };
 
-/// TODO:
-pub struct KeybindKeystroke {
-    /// TODO:
-    pub inner: Keystroke,
-    /// TODO:
-    pub modifiers: Modifiers,
-    /// TODO:
-    pub key: String,
-}
-
 /// A keystroke and associated metadata generated by the platform
 #[derive(Clone, Debug, Eq, PartialEq, Default, Deserialize, Hash)]
 pub struct Keystroke {
@@ -31,6 +21,16 @@ pub struct Keystroke {
     pub key_char: Option<String>,
 }
 
+/// TODO:
+pub struct KeybindKeystroke {
+    /// TODO:
+    pub inner: Keystroke,
+    /// TODO:
+    pub modifiers: Modifiers,
+    /// TODO:
+    pub key: String,
+}
+
 /// Error type for `Keystroke::parse`. This is used instead of `anyhow::Error` so that Zed can use
 /// markdown to display it.
 #[derive(Debug)]
@@ -273,6 +273,112 @@ impl Keystroke {
     }
 }
 
+impl KeybindKeystroke {
+    /// TODO:
+    pub fn parse(source: &str) -> std::result::Result<Self, InvalidKeystrokeError> {
+        let keystroke = Keystroke::parse(source)?;
+        let Keystroke {
+            mut modifiers, key, ..
+        } = keystroke.clone();
+        let (key, modifiers) = temp_keyboard_mapper(key, modifiers);
+        Ok(KeybindKeystroke {
+            inner: keystroke,
+            modifiers,
+            key,
+        })
+    }
+}
+
+fn temp_keyboard_mapper(key: String, mut modifiers: Modifiers) -> (String, Modifiers) {
+    match key.as_str() {
+        "~" => {
+            modifiers.shift = true;
+            ("`".to_string(), modifiers)
+        }
+        "!" => {
+            modifiers.shift = true;
+            ("1".to_string(), modifiers)
+        }
+        "@" => {
+            modifiers.shift = true;
+            ("2".to_string(), modifiers)
+        }
+        "#" => {
+            modifiers.shift = true;
+            ("3".to_string(), modifiers)
+        }
+        "$" => {
+            modifiers.shift = true;
+            ("4".to_string(), modifiers)
+        }
+        "%" => {
+            modifiers.shift = true;
+            ("5".to_string(), modifiers)
+        }
+        "^" => {
+            modifiers.shift = true;
+            ("6".to_string(), modifiers)
+        }
+        "&" => {
+            modifiers.shift = true;
+            ("7".to_string(), modifiers)
+        }
+        "*" => {
+            modifiers.shift = true;
+            ("8".to_string(), modifiers)
+        }
+        "(" => {
+            modifiers.shift = true;
+            ("9".to_string(), modifiers)
+        }
+        ")" => {
+            modifiers.shift = true;
+            ("0".to_string(), modifiers)
+        }
+        "_" => {
+            modifiers.shift = true;
+            ("-".to_string(), modifiers)
+        }
+        "+" => {
+            modifiers.shift = true;
+            ("=".to_string(), modifiers)
+        }
+        "{" => {
+            modifiers.shift = true;
+            ("[".to_string(), modifiers)
+        }
+        "}" => {
+            modifiers.shift = true;
+            ("]".to_string(), modifiers)
+        }
+        "|" => {
+            modifiers.shift = true;
+            ("\\".to_string(), modifiers)
+        }
+        ":" => {
+            modifiers.shift = true;
+            (";".to_string(), modifiers)
+        }
+        "\"" => {
+            modifiers.shift = true;
+            ("'".to_string(), modifiers)
+        }
+        "<" => {
+            modifiers.shift = true;
+            (",".to_string(), modifiers)
+        }
+        ">" => {
+            modifiers.shift = true;
+            (">".to_string(), modifiers)
+        }
+        "?" => {
+            modifiers.shift = true;
+            ("/".to_string(), modifiers)
+        }
+        _ => (key, modifiers),
+    }
+}
+
 fn is_printable_key(key: &str) -> bool {
     !matches!(
         key,