Skip to content

Commit fc9968b

Browse files
fix: allow to parse "-" as a key code (#12191)
Co-authored-by: Michael Davis <[email protected]>
1 parent 28953ef commit fc9968b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

helix-view/src/input.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,23 @@ impl std::str::FromStr for KeyEvent {
387387
.then_some(KeyCode::F(function))
388388
.ok_or_else(|| anyhow!("Invalid function key '{}'", function))?
389389
}
390+
// Checking that the last token is empty ensures that this branch is only taken if
391+
// `-` is used as a code. For example this branch will not be taken for `S-` (which is
392+
// missing a code).
393+
_ if s.ends_with('-') && tokens.last().is_some_and(|t| t.is_empty()) => {
394+
if s == "-" {
395+
return Ok(KeyEvent {
396+
code: KeyCode::Char('-'),
397+
modifiers: KeyModifiers::empty(),
398+
});
399+
} else {
400+
let suggestion = format!("{}-{}", s.trim_end_matches('-'), keys::MINUS);
401+
return Err(anyhow!(
402+
"Key '-' cannot be used with modifiers, use '{}' instead",
403+
suggestion
404+
));
405+
}
406+
}
390407
invalid => return Err(anyhow!("Invalid key code '{}'", invalid)),
391408
};
392409

@@ -661,6 +678,13 @@ mod test {
661678
modifiers: KeyModifiers::NONE
662679
}
663680
);
681+
assert_eq!(
682+
str::parse::<KeyEvent>("-").unwrap(),
683+
KeyEvent {
684+
code: KeyCode::Char('-'),
685+
modifiers: KeyModifiers::NONE,
686+
}
687+
);
664688
}
665689

666690
#[test]
@@ -721,6 +745,7 @@ mod test {
721745
assert!(str::parse::<KeyEvent>("FU").is_err());
722746
assert!(str::parse::<KeyEvent>("123").is_err());
723747
assert!(str::parse::<KeyEvent>("S--").is_err());
748+
assert!(str::parse::<KeyEvent>("S-").is_err());
724749
assert!(str::parse::<KeyEvent>("S-percent").is_err());
725750
}
726751

0 commit comments

Comments
 (0)