Normalize S-<lower-ascii> keymaps to uppercase ascii (#9213)

This commit is contained in:
Michael Davis 2024-01-08 20:04:34 -05:00 committed by GitHub
parent 41ca46cf8c
commit 305d6e9c89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -325,7 +325,7 @@ impl std::str::FromStr for KeyEvent {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut tokens: Vec<_> = s.split('-').collect(); let mut tokens: Vec<_> = s.split('-').collect();
let code = match tokens.pop().ok_or_else(|| anyhow!("Missing key code"))? { let mut code = match tokens.pop().ok_or_else(|| anyhow!("Missing key code"))? {
keys::BACKSPACE => KeyCode::Backspace, keys::BACKSPACE => KeyCode::Backspace,
keys::ENTER => KeyCode::Enter, keys::ENTER => KeyCode::Enter,
keys::LEFT => KeyCode::Left, keys::LEFT => KeyCode::Left,
@ -405,6 +405,18 @@ fn from_str(s: &str) -> Result<Self, Self::Err> {
modifiers.insert(flag); modifiers.insert(flag);
} }
// Normalize character keys so that characters like C-S-r and C-R
// are represented by equal KeyEvents.
match code {
KeyCode::Char(ch)
if ch.is_ascii_lowercase() && modifiers.contains(KeyModifiers::SHIFT) =>
{
code = KeyCode::Char(ch.to_ascii_uppercase());
modifiers.remove(KeyModifiers::SHIFT);
}
_ => (),
}
Ok(KeyEvent { code, modifiers }) Ok(KeyEvent { code, modifiers })
} }
} }
@ -684,6 +696,19 @@ fn parsing_modified_keys() {
modifiers: KeyModifiers::ALT | KeyModifiers::CONTROL modifiers: KeyModifiers::ALT | KeyModifiers::CONTROL
} }
); );
assert_eq!(
str::parse::<KeyEvent>("C-S-r").unwrap(),
str::parse::<KeyEvent>("C-R").unwrap(),
);
assert_eq!(
str::parse::<KeyEvent>("S-w").unwrap(),
KeyEvent {
code: KeyCode::Char('W'),
modifiers: KeyModifiers::NONE
}
);
} }
#[test] #[test]