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

simplify some keymap key names #2677

Merged
Merged
Show file tree
Hide file tree
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
5 changes: 0 additions & 5 deletions book/src/remapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ Control, Shift and Alt modifiers are encoded respectively with the prefixes
| Backspace | `"backspace"` |
| Space | `"space"` |
| Return/Enter | `"ret"` |
| < | `"lt"` |
| \> | `"gt"` |
| \+ | `"plus"` |
| \- | `"minus"` |
| ; | `"semicolon"` |
| % | `"percent"` |
| Left | `"left"` |
| Right | `"right"` |
| Up | `"up"` |
Expand Down
38 changes: 18 additions & 20 deletions helix-view/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ pub(crate) mod keys {
pub(crate) const NULL: &str = "null";
pub(crate) const ESC: &str = "esc";
pub(crate) const SPACE: &str = "space";
pub(crate) const LESS_THAN: &str = "lt";
pub(crate) const GREATER_THAN: &str = "gt";
pub(crate) const PLUS: &str = "plus";
pub(crate) const MINUS: &str = "minus";
pub(crate) const SEMICOLON: &str = "semicolon";
pub(crate) const PERCENT: &str = "percent";
}

impl fmt::Display for KeyEvent {
Expand Down Expand Up @@ -86,12 +81,7 @@ impl fmt::Display for KeyEvent {
KeyCode::Null => f.write_str(keys::NULL)?,
KeyCode::Esc => f.write_str(keys::ESC)?,
KeyCode::Char(' ') => f.write_str(keys::SPACE)?,
KeyCode::Char('<') => f.write_str(keys::LESS_THAN)?,
KeyCode::Char('>') => f.write_str(keys::GREATER_THAN)?,
KeyCode::Char('+') => f.write_str(keys::PLUS)?,
KeyCode::Char('-') => f.write_str(keys::MINUS)?,
KeyCode::Char(';') => f.write_str(keys::SEMICOLON)?,
KeyCode::Char('%') => f.write_str(keys::PERCENT)?,
KeyCode::F(i) => f.write_fmt(format_args!("F{}", i))?,
KeyCode::Char(c) => f.write_fmt(format_args!("{}", c))?,
};
Expand Down Expand Up @@ -119,12 +109,7 @@ impl UnicodeWidthStr for KeyEvent {
KeyCode::Null => keys::NULL.len(),
KeyCode::Esc => keys::ESC.len(),
KeyCode::Char(' ') => keys::SPACE.len(),
KeyCode::Char('<') => keys::LESS_THAN.len(),
KeyCode::Char('>') => keys::GREATER_THAN.len(),
KeyCode::Char('+') => keys::PLUS.len(),
KeyCode::Char('-') => keys::MINUS.len(),
KeyCode::Char(';') => keys::SEMICOLON.len(),
KeyCode::Char('%') => keys::PERCENT.len(),
KeyCode::F(1..=9) => 2,
KeyCode::F(_) => 3,
KeyCode::Char(c) => c.width().unwrap_or(0),
Expand Down Expand Up @@ -168,12 +153,7 @@ impl std::str::FromStr for KeyEvent {
keys::NULL => KeyCode::Null,
keys::ESC => KeyCode::Esc,
keys::SPACE => KeyCode::Char(' '),
keys::LESS_THAN => KeyCode::Char('<'),
keys::GREATER_THAN => KeyCode::Char('>'),
keys::PLUS => KeyCode::Char('+'),
keys::MINUS => KeyCode::Char('-'),
keys::SEMICOLON => KeyCode::Char(';'),
keys::PERCENT => KeyCode::Char('%'),
single if single.chars().count() == 1 => KeyCode::Char(single.chars().next().unwrap()),
function if function.len() > 1 && function.starts_with('F') => {
let function: String = function.chars().skip(1).collect();
Expand Down Expand Up @@ -336,6 +316,14 @@ mod test {
modifiers: KeyModifiers::NONE
}
);

assert_eq!(
str::parse::<KeyEvent>("%").unwrap(),
KeyEvent {
code: KeyCode::Char('%'),
modifiers: KeyModifiers::NONE
}
)
}

#[test]
Expand Down Expand Up @@ -375,6 +363,16 @@ mod test {
assert!(str::parse::<KeyEvent>("FU").is_err());
assert!(str::parse::<KeyEvent>("123").is_err());
assert!(str::parse::<KeyEvent>("S--").is_err());
assert!(str::parse::<KeyEvent>("S-percent").is_err());
}

#[test]
fn parsing_unsupported_named_keys() {
assert!(str::parse::<KeyEvent>("lt").is_err());
assert!(str::parse::<KeyEvent>("gt").is_err());
assert!(str::parse::<KeyEvent>("plus").is_err());
assert!(str::parse::<KeyEvent>("percent").is_err());
assert!(str::parse::<KeyEvent>("semicolon").is_err());
Comment on lines +370 to +375
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than just testing these keys are now unsupported, shouldn't we also test that they get parsed correctly by the new syntax?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@archseer Great catch! I intended to do this, but only added a test for percent. I created a new PR to add in the additional tests. #2694

}

#[test]
Expand Down