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

Don't panic if we get an unexpected key code #4576

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 10 additions & 2 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,22 @@ impl Application {
jobs: &mut self.jobs,
scroll: None,
};
// Handle key events
let should_redraw = match event.unwrap() {
CrosstermEvent::Resize(width, height) => {
self.compositor.resize(width, height);
self.compositor
.handle_event(&Event::Resize(width, height), &mut cx)
}
event => self.compositor.handle_event(&event.into(), &mut cx),
event => match &event.try_into() {
Ok(event) => self.compositor.handle_event(event, &mut cx),
Err(err) => {
log::warn!(
"Unable to convert crossterm event to helix event. Ignoring. Cause: {}",
err
);
false
}
},
};

if should_redraw && !self.editor.should_close() {
Expand Down
28 changes: 15 additions & 13 deletions helix-view/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,17 @@ impl<'de> Deserialize<'de> for KeyEvent {
}

#[cfg(feature = "term")]
impl From<crossterm::event::Event> for Event {
fn from(event: crossterm::event::Event) -> Self {
match event {
crossterm::event::Event::Key(key) => Self::Key(key.into()),
impl TryFrom<crossterm::event::Event> for Event {
type Error = anyhow::Error;
fn try_from(event: crossterm::event::Event) -> Result<Self, Error> {
Ok(match event {
crossterm::event::Event::Key(key) => Self::Key(key.try_into()?),
crossterm::event::Event::Mouse(mouse) => Self::Mouse(mouse.into()),
crossterm::event::Event::Resize(w, h) => Self::Resize(w, h),
crossterm::event::Event::FocusGained => Self::FocusGained,
crossterm::event::Event::FocusLost => Self::FocusLost,
crossterm::event::Event::Paste(s) => Self::Paste(s),
}
})
}
}

Expand Down Expand Up @@ -334,25 +335,26 @@ impl From<crossterm::event::MouseButton> for MouseButton {
}

#[cfg(feature = "term")]
impl From<crossterm::event::KeyEvent> for KeyEvent {
fn from(
impl TryFrom<crossterm::event::KeyEvent> for KeyEvent {
type Error = anyhow::Error;
fn try_from(
crossterm::event::KeyEvent {
code, modifiers, ..
}: crossterm::event::KeyEvent,
) -> Self {
) -> Result<Self, Error> {
if code == crossterm::event::KeyCode::BackTab {
// special case for BackTab -> Shift-Tab
let mut modifiers: KeyModifiers = modifiers.into();
modifiers.insert(KeyModifiers::SHIFT);
Self {
Ok(Self {
code: KeyCode::Tab,
modifiers,
}
})
} else {
Self {
code: code.into(),
Ok(Self {
code: code.try_into()?,
modifiers: modifiers.into(),
}
})
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions helix-view/src/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::{bail, Error};
use bitflags::bitflags;

bitflags! {
Expand Down Expand Up @@ -124,11 +125,12 @@ impl From<KeyCode> for crossterm::event::KeyCode {
}

#[cfg(feature = "term")]
impl From<crossterm::event::KeyCode> for KeyCode {
fn from(val: crossterm::event::KeyCode) -> Self {
impl TryFrom<crossterm::event::KeyCode> for KeyCode {
type Error = Error;
fn try_from(val: crossterm::event::KeyCode) -> Result<Self, Error> {
use crossterm::event::KeyCode as CKeyCode;

match val {
Ok(match val {
CKeyCode::Backspace => KeyCode::Backspace,
CKeyCode::Enter => KeyCode::Enter,
CKeyCode::Left => KeyCode::Left,
Expand All @@ -155,9 +157,9 @@ impl From<crossterm::event::KeyCode> for KeyCode {
| CKeyCode::Menu
| CKeyCode::KeypadBegin
| CKeyCode::Media(_)
| CKeyCode::Modifier(_) => unreachable!(
| CKeyCode::Modifier(_) => bail!(
"Shouldn't get this key without enabling DISAMBIGUATE_ESCAPE_CODES in crossterm"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"Shouldn't get this key without enabling DISAMBIGUATE_ESCAPE_CODES in crossterm"
"Shouldn't get key {:?} without enabling DISAMBIGUATE_ESCAPE_CODES in crossterm",
val

Since the unreachable doesn't point at this line anymore, this should make it easier to debug if it shows up in the logfile

),
}
})
}
}