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.
This commit is contained in:
Gokul Soumya 2021-11-10 21:28:46 +05:30 committed by GitHub
parent e863e3b62d
commit efc2b4c77b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 240 deletions

View File

@ -25,6 +25,38 @@ macro_rules! key {
}; };
} }
#[macro_export]
macro_rules! ctrl {
($key:ident) => {
::helix_view::input::KeyEvent {
code: ::helix_view::keyboard::KeyCode::$key,
modifiers: ::helix_view::keyboard::KeyModifiers::CONTROL,
}
};
($($ch:tt)*) => {
::helix_view::input::KeyEvent {
code: ::helix_view::keyboard::KeyCode::Char($($ch)*),
modifiers: ::helix_view::keyboard::KeyModifiers::CONTROL,
}
};
}
#[macro_export]
macro_rules! alt {
($key:ident) => {
::helix_view::input::KeyEvent {
code: ::helix_view::keyboard::KeyCode::$key,
modifiers: ::helix_view::keyboard::KeyModifiers::ALT,
}
};
($($ch:tt)*) => {
::helix_view::input::KeyEvent {
code: ::helix_view::keyboard::KeyCode::Char($($ch)*),
modifiers: ::helix_view::keyboard::KeyModifiers::ALT,
}
};
}
/// Macro for defining the root of a `Keymap` object. Example: /// Macro for defining the root of a `Keymap` object. Example:
/// ///
/// ``` /// ```

View File

@ -1,5 +1,8 @@
use crate::compositor::{Component, Compositor, Context, EventResult}; use crate::{
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; compositor::{Component, Compositor, Context, EventResult},
ctrl, key,
};
use crossterm::event::Event;
use tui::{buffer::Buffer as Surface, widgets::Table}; use tui::{buffer::Buffer as Surface, widgets::Table};
pub use tui::widgets::{Cell, Row}; pub use tui::widgets::{Cell, Row};
@ -192,63 +195,25 @@ impl<T: Item + 'static> Component for Menu<T> {
compositor.pop(); compositor.pop();
}))); })));
match event { match event.into() {
// esc or ctrl-c aborts the completion and closes the menu // esc or ctrl-c aborts the completion and closes the menu
KeyEvent { key!(Esc) | ctrl!('c') => {
code: KeyCode::Esc, ..
}
| KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::CONTROL,
} => {
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Abort); (self.callback_fn)(cx.editor, self.selection(), MenuEvent::Abort);
return close_fn; return close_fn;
} }
// arrow up/ctrl-p/shift-tab prev completion choice (including updating the doc) // arrow up/ctrl-p/shift-tab prev completion choice (including updating the doc)
KeyEvent { key!(BackTab) | key!(Up) | ctrl!('p') | ctrl!('k') => {
code: KeyCode::BackTab,
..
}
| KeyEvent {
code: KeyCode::Up, ..
}
| KeyEvent {
code: KeyCode::Char('p'),
modifiers: KeyModifiers::CONTROL,
}
| KeyEvent {
code: KeyCode::Char('k'),
modifiers: KeyModifiers::CONTROL,
} => {
self.move_up(); self.move_up();
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Update); (self.callback_fn)(cx.editor, self.selection(), MenuEvent::Update);
return EventResult::Consumed(None); return EventResult::Consumed(None);
} }
key!(Tab) | key!(Down) | ctrl!('n') | ctrl!('j') => {
// arrow down/ctrl-n/tab advances completion choice (including updating the doc) // 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,
} => {
self.move_down(); self.move_down();
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Update); (self.callback_fn)(cx.editor, self.selection(), MenuEvent::Update);
return EventResult::Consumed(None); return EventResult::Consumed(None);
} }
KeyEvent { key!(Enter) => {
code: KeyCode::Enter,
..
} => {
if let Some(selection) = self.selection() { if let Some(selection) = self.selection() {
(self.callback_fn)(cx.editor, Some(selection), MenuEvent::Validate); (self.callback_fn)(cx.editor, Some(selection), MenuEvent::Validate);
} }

View File

@ -1,8 +1,9 @@
use crate::{ use crate::{
compositor::{Component, Compositor, Context, EventResult}, compositor::{Component, Compositor, Context, EventResult},
ctrl, key,
ui::EditorView, ui::EditorView,
}; };
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use crossterm::event::Event;
use tui::{ use tui::{
buffer::Buffer as Surface, buffer::Buffer as Surface,
widgets::{Block, BorderType, Borders}, widgets::{Block, BorderType, Borders},
@ -402,81 +403,35 @@ impl<T: 'static> Component for Picker<T> {
compositor.last_picker = compositor.pop(); compositor.last_picker = compositor.pop();
}))); })));
match key_event { match key_event.into() {
KeyEvent { key!(BackTab) | key!(Up) | ctrl!('p') | ctrl!('k') => {
code: KeyCode::Up, ..
}
| KeyEvent {
code: KeyCode::BackTab,
..
}
| KeyEvent {
code: KeyCode::Char('k'),
modifiers: KeyModifiers::CONTROL,
}
| KeyEvent {
code: KeyCode::Char('p'),
modifiers: KeyModifiers::CONTROL,
} => {
self.move_up(); self.move_up();
} }
KeyEvent { key!(Tab) | key!(Down) | ctrl!('n') | ctrl!('j') => {
code: KeyCode::Down,
..
}
| KeyEvent {
code: KeyCode::Tab, ..
}
| KeyEvent {
code: KeyCode::Char('j'),
modifiers: KeyModifiers::CONTROL,
}
| KeyEvent {
code: KeyCode::Char('n'),
modifiers: KeyModifiers::CONTROL,
} => {
self.move_down(); self.move_down();
} }
KeyEvent { key!(Esc) | ctrl!('c') => {
code: KeyCode::Esc, ..
}
| KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::CONTROL,
} => {
return close_fn; return close_fn;
} }
KeyEvent { key!(Enter) => {
code: KeyCode::Enter,
..
} => {
if let Some(option) = self.selection() { if let Some(option) = self.selection() {
(self.callback_fn)(&mut cx.editor, option, Action::Replace); (self.callback_fn)(&mut cx.editor, option, Action::Replace);
} }
return close_fn; return close_fn;
} }
KeyEvent { ctrl!('s') => {
code: KeyCode::Char('s'),
modifiers: KeyModifiers::CONTROL,
} => {
if let Some(option) = self.selection() { if let Some(option) = self.selection() {
(self.callback_fn)(&mut cx.editor, option, Action::HorizontalSplit); (self.callback_fn)(&mut cx.editor, option, Action::HorizontalSplit);
} }
return close_fn; return close_fn;
} }
KeyEvent { ctrl!('v') => {
code: KeyCode::Char('v'),
modifiers: KeyModifiers::CONTROL,
} => {
if let Some(option) = self.selection() { if let Some(option) = self.selection() {
(self.callback_fn)(&mut cx.editor, option, Action::VerticalSplit); (self.callback_fn)(&mut cx.editor, option, Action::VerticalSplit);
} }
return close_fn; return close_fn;
} }
KeyEvent { ctrl!(' ') => {
code: KeyCode::Char(' '),
modifiers: KeyModifiers::CONTROL,
} => {
self.save_filter(); self.save_filter();
} }
_ => { _ => {

View File

@ -1,5 +1,8 @@
use crate::compositor::{Component, Compositor, Context, EventResult}; use crate::{
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; compositor::{Component, Compositor, Context, EventResult},
ctrl, key,
};
use crossterm::event::Event;
use tui::buffer::Buffer as Surface; use tui::buffer::Buffer as Surface;
use helix_core::Position; use helix_core::Position;
@ -95,27 +98,14 @@ impl<T: Component> Component for Popup<T> {
compositor.pop(); compositor.pop();
}))); })));
match key { match key.into() {
// esc or ctrl-c aborts the completion and closes the menu // esc or ctrl-c aborts the completion and closes the menu
KeyEvent { key!(Esc) | ctrl!('c') => close_fn,
code: KeyCode::Esc, .. ctrl!('d') => {
}
| KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::CONTROL,
} => close_fn,
KeyEvent {
code: KeyCode::Char('d'),
modifiers: KeyModifiers::CONTROL,
} => {
self.scroll(self.size.1 as usize / 2, true); self.scroll(self.size.1 as usize / 2, true);
EventResult::Consumed(None) EventResult::Consumed(None)
} }
KeyEvent { ctrl!('u') => {
code: KeyCode::Char('u'),
modifiers: KeyModifiers::CONTROL,
} => {
self.scroll(self.size.1 as usize / 2, false); self.scroll(self.size.1 as usize / 2, false);
EventResult::Consumed(None) EventResult::Consumed(None)
} }

View File

@ -1,6 +1,8 @@
use crate::compositor::{Component, Compositor, Context, EventResult}; use crate::compositor::{Component, Compositor, Context, EventResult};
use crate::ui; use crate::{alt, ctrl, key, ui};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use crossterm::event::Event;
use helix_view::input::KeyEvent;
use helix_view::keyboard::{KeyCode, KeyModifiers};
use std::{borrow::Cow, ops::RangeFrom}; use std::{borrow::Cow, ops::RangeFrom};
use tui::buffer::Buffer as Surface; use tui::buffer::Buffer as Surface;
@ -421,103 +423,29 @@ impl Component for Prompt {
compositor.pop(); compositor.pop();
}))); })));
match event { match event.into() {
KeyEvent { ctrl!('c') | key!(Esc) => {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::CONTROL,
}
| KeyEvent {
code: KeyCode::Esc, ..
} => {
(self.callback_fn)(cx, &self.line, PromptEvent::Abort); (self.callback_fn)(cx, &self.line, PromptEvent::Abort);
return close_fn; return close_fn;
} }
KeyEvent { alt!('b') | alt!(Left) => self.move_cursor(Movement::BackwardWord(1)),
code: KeyCode::Left, alt!('f') | alt!(Right) => self.move_cursor(Movement::ForwardWord(1)),
modifiers: KeyModifiers::ALT, ctrl!('b') | ctrl!(Left) => self.move_cursor(Movement::BackwardChar(1)),
} ctrl!('f') | ctrl!(Right) => self.move_cursor(Movement::ForwardChar(1)),
| KeyEvent { ctrl!('e') | key!(End) => self.move_end(),
code: KeyCode::Char('b'), ctrl!('a') | key!(Home) => self.move_start(),
modifiers: KeyModifiers::ALT, ctrl!('w') => self.delete_word_backwards(),
} => self.move_cursor(Movement::BackwardWord(1)), ctrl!('k') => self.kill_to_end_of_line(),
KeyEvent { ctrl!('u') => self.kill_to_start_of_line(),
code: KeyCode::Right, ctrl!('h') | key!(Backspace) => {
modifiers: KeyModifiers::ALT,
}
| KeyEvent {
code: KeyCode::Char('f'),
modifiers: KeyModifiers::ALT,
} => self.move_cursor(Movement::ForwardWord(1)),
KeyEvent {
code: KeyCode::Char('f'),
modifiers: KeyModifiers::CONTROL,
}
| KeyEvent {
code: KeyCode::Right,
..
} => self.move_cursor(Movement::ForwardChar(1)),
KeyEvent {
code: KeyCode::Char('b'),
modifiers: KeyModifiers::CONTROL,
}
| KeyEvent {
code: KeyCode::Left,
..
} => self.move_cursor(Movement::BackwardChar(1)),
KeyEvent {
code: KeyCode::End,
modifiers: KeyModifiers::NONE,
}
| KeyEvent {
code: KeyCode::Char('e'),
modifiers: KeyModifiers::CONTROL,
} => self.move_end(),
KeyEvent {
code: KeyCode::Home,
modifiers: KeyModifiers::NONE,
}
| KeyEvent {
code: KeyCode::Char('a'),
modifiers: KeyModifiers::CONTROL,
} => self.move_start(),
KeyEvent {
code: KeyCode::Char('w'),
modifiers: KeyModifiers::CONTROL,
} => self.delete_word_backwards(),
KeyEvent {
code: KeyCode::Char('k'),
modifiers: KeyModifiers::CONTROL,
} => self.kill_to_end_of_line(),
KeyEvent {
code: KeyCode::Char('u'),
modifiers: KeyModifiers::CONTROL,
} => self.kill_to_start_of_line(),
KeyEvent {
code: KeyCode::Char('h'),
modifiers: KeyModifiers::CONTROL,
}
| KeyEvent {
code: KeyCode::Backspace,
modifiers: KeyModifiers::NONE,
} => {
self.delete_char_backwards(); self.delete_char_backwards();
(self.callback_fn)(cx, &self.line, PromptEvent::Update); (self.callback_fn)(cx, &self.line, PromptEvent::Update);
} }
KeyEvent { ctrl!('d') | key!(Delete) => {
code: KeyCode::Char('d'),
modifiers: KeyModifiers::CONTROL,
}
| KeyEvent {
code: KeyCode::Delete,
modifiers: KeyModifiers::NONE,
} => {
self.delete_char_forwards(); self.delete_char_forwards();
(self.callback_fn)(cx, &self.line, PromptEvent::Update); (self.callback_fn)(cx, &self.line, PromptEvent::Update);
} }
KeyEvent { ctrl!('s') => {
code: KeyCode::Char('s'),
modifiers: KeyModifiers::CONTROL,
} => {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..); let text = doc.text().slice(..);
@ -535,10 +463,7 @@ impl Component for Prompt {
(self.callback_fn)(cx, &self.line, PromptEvent::Update); (self.callback_fn)(cx, &self.line, PromptEvent::Update);
} }
} }
KeyEvent { key!(Enter) => {
code: KeyCode::Enter,
..
} => {
if self.selection.is_some() && self.line.ends_with('/') { if self.selection.is_some() && self.line.ends_with('/') {
self.completion = (self.completion_fn)(&self.line); self.completion = (self.completion_fn)(&self.line);
self.exit_selection(); self.exit_selection();
@ -553,50 +478,29 @@ impl Component for Prompt {
return close_fn; return close_fn;
} }
} }
KeyEvent { ctrl!('p') | key!(Up) => {
code: KeyCode::Char('p'),
modifiers: KeyModifiers::CONTROL,
}
| KeyEvent {
code: KeyCode::Up, ..
} => {
if let Some(register) = self.history_register { if let Some(register) = self.history_register {
let register = cx.editor.registers.get_mut(register); let register = cx.editor.registers.get_mut(register);
self.change_history(register.read(), CompletionDirection::Backward); self.change_history(register.read(), CompletionDirection::Backward);
(self.callback_fn)(cx, &self.line, PromptEvent::Update); (self.callback_fn)(cx, &self.line, PromptEvent::Update);
} }
} }
KeyEvent { ctrl!('n') | key!(Down) => {
code: KeyCode::Char('n'),
modifiers: KeyModifiers::CONTROL,
}
| KeyEvent {
code: KeyCode::Down,
..
} => {
if let Some(register) = self.history_register { if let Some(register) = self.history_register {
let register = cx.editor.registers.get_mut(register); let register = cx.editor.registers.get_mut(register);
self.change_history(register.read(), CompletionDirection::Forward); self.change_history(register.read(), CompletionDirection::Forward);
(self.callback_fn)(cx, &self.line, PromptEvent::Update); (self.callback_fn)(cx, &self.line, PromptEvent::Update);
} }
} }
KeyEvent { key!(Tab) => {
code: KeyCode::Tab, ..
} => {
self.change_completion_selection(CompletionDirection::Forward); self.change_completion_selection(CompletionDirection::Forward);
(self.callback_fn)(cx, &self.line, PromptEvent::Update) (self.callback_fn)(cx, &self.line, PromptEvent::Update)
} }
KeyEvent { key!(BackTab) => {
code: KeyCode::BackTab,
..
} => {
self.change_completion_selection(CompletionDirection::Backward); self.change_completion_selection(CompletionDirection::Backward);
(self.callback_fn)(cx, &self.line, PromptEvent::Update) (self.callback_fn)(cx, &self.line, PromptEvent::Update)
} }
KeyEvent { ctrl!('q') => self.exit_selection(),
code: KeyCode::Char('q'),
modifiers: KeyModifiers::CONTROL,
} => self.exit_selection(),
// any char event that's not combined with control or mapped to any other combo // any char event that's not combined with control or mapped to any other combo
KeyEvent { KeyEvent {
code: KeyCode::Char(c), code: KeyCode::Char(c),