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