Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion helix-view/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ impl std::str::FromStr for KeyEvent {

fn from_str(s: &str) -> Result<Self, Self::Err> {
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::ENTER => KeyCode::Enter,
keys::LEFT => KeyCode::Left,
Expand Down Expand Up @@ -405,6 +405,18 @@ impl std::str::FromStr for KeyEvent {
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 })
}
}
Expand Down Expand Up @@ -684,6 +696,19 @@ mod test {
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]
Expand Down
Loading