aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/ui/menu.rs
diff options
context:
space:
mode:
authorGokul Soumya2021-11-10 15:58:46 +0000
committerGitHub2021-11-10 15:58:46 +0000
commitefc2b4c77be55afad07762cdde9b3b74c8477933 (patch)
tree273857f4fecd9a7c0f6740217545378d319c2451 /helix-term/src/ui/menu.rs
parente863e3b62d232d650468cb5d1f9da925fb460f5e (diff)
Refactor keyevent handling using key, ctrl macros (#1058)
Adds ctrl! and alt! macros (which existed before the big keymap refactor) and uses them in event handling of Components. Note that this converts crossterm's KeyEvent to our own KeyEvent on each invocation of handle_event in Components.
Diffstat (limited to 'helix-term/src/ui/menu.rs')
-rw-r--r--helix-term/src/ui/menu.rs57
1 files changed, 11 insertions, 46 deletions
diff --git a/helix-term/src/ui/menu.rs b/helix-term/src/ui/menu.rs
index 3c492d14..8278bd29 100644
--- a/helix-term/src/ui/menu.rs
+++ b/helix-term/src/ui/menu.rs
@@ -1,5 +1,8 @@
-use crate::compositor::{Component, Compositor, Context, EventResult};
-use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
+use crate::{
+ compositor::{Component, Compositor, Context, EventResult},
+ ctrl, key,
+};
+use crossterm::event::Event;
use tui::{buffer::Buffer as Surface, widgets::Table};
pub use tui::widgets::{Cell, Row};
@@ -192,63 +195,25 @@ impl<T: Item + 'static> Component for Menu<T> {
compositor.pop();
})));
- match event {
+ match event.into() {
// esc or ctrl-c aborts the completion and closes the menu
- KeyEvent {
- code: KeyCode::Esc, ..
- }
- | KeyEvent {
- code: KeyCode::Char('c'),
- modifiers: KeyModifiers::CONTROL,
- } => {
+ key!(Esc) | ctrl!('c') => {
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Abort);
return close_fn;
}
// arrow up/ctrl-p/shift-tab prev completion choice (including updating the doc)
- KeyEvent {
- code: KeyCode::BackTab,
- ..
- }
- | KeyEvent {
- code: KeyCode::Up, ..
- }
- | KeyEvent {
- code: KeyCode::Char('p'),
- modifiers: KeyModifiers::CONTROL,
- }
- | KeyEvent {
- code: KeyCode::Char('k'),
- modifiers: KeyModifiers::CONTROL,
- } => {
+ key!(BackTab) | key!(Up) | ctrl!('p') | ctrl!('k') => {
self.move_up();
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Update);
return EventResult::Consumed(None);
}
- // arrow down/ctrl-n/tab advances completion choice (including updating the doc)
- KeyEvent {
- code: KeyCode::Tab,
- modifiers: KeyModifiers::NONE,
- }
- | KeyEvent {
- code: KeyCode::Down,
- ..
- }
- | KeyEvent {
- code: KeyCode::Char('n'),
- modifiers: KeyModifiers::CONTROL,
- }
- | KeyEvent {
- code: KeyCode::Char('j'),
- modifiers: KeyModifiers::CONTROL,
- } => {
+ key!(Tab) | key!(Down) | ctrl!('n') | ctrl!('j') => {
+ // arrow down/ctrl-n/tab advances completion choice (including updating the doc)
self.move_down();
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Update);
return EventResult::Consumed(None);
}
- KeyEvent {
- code: KeyCode::Enter,
- ..
- } => {
+ key!(Enter) => {
if let Some(selection) = self.selection() {
(self.callback_fn)(cx.editor, Some(selection), MenuEvent::Validate);
}