From d54ae09d3b7a5eb56050631a58e75a1d1686ea70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Sat, 29 May 2021 10:37:47 +0900 Subject: [PATCH] ESC should exit both completion and insert mode --- helix-term/src/ui/completion.rs | 7 +++++++ helix-term/src/ui/editor.rs | 25 ++++++++++++++++--------- helix-term/src/ui/picker.rs | 6 ++---- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs index 0af03d14..a757406b 100644 --- a/helix-term/src/ui/completion.rs +++ b/helix-term/src/ui/completion.rs @@ -207,6 +207,13 @@ pub fn is_empty(&self) -> bool { impl Component for Completion { fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult { + // let the Editor handle Esc instead + if let Event::Key(KeyEvent { + code: KeyCode::Esc, .. + }) = event + { + return EventResult::Ignored; + } self.popup.handle_event(event, cx) } diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index c597f840..47770ebd 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -584,7 +584,6 @@ fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult { if completion.is_empty() { self.completion = None; } - // TODO: if exiting InsertMode, remove completion } } } @@ -605,16 +604,24 @@ fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult { let (view, doc) = cx.editor.current(); view.ensure_cursor_in_view(doc); - if mode == Mode::Normal && doc.mode() == Mode::Insert { - // HAXX: if we just entered insert mode from normal, clear key buf - // and record the command that got us into this mode. + // mode transitions + match (mode, doc.mode()) { + (Mode::Normal, Mode::Insert) => { + // HAXX: if we just entered insert mode from normal, clear key buf + // and record the command that got us into this mode. - // how we entered insert mode is important, and we should track that so - // we can repeat the side effect. + // how we entered insert mode is important, and we should track that so + // we can repeat the side effect. - self.last_insert.0 = self.keymap[&mode][&key]; - self.last_insert.1.clear(); - }; + self.last_insert.0 = self.keymap[&mode][&key]; + self.last_insert.1.clear(); + } + (Mode::Insert, Mode::Normal) => { + // if exiting insert mode, remove completion + self.completion = None; + } + _ => (), + } EventResult::Consumed(callback) } diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index a715ebb5..4cff4917 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -122,14 +122,12 @@ fn inner_rect(area: Rect) -> Rect { let padding_vertical = area.height * 20 / 100; let padding_horizontal = area.width * 20 / 100; - let area = Rect::new( + Rect::new( area.x + padding_horizontal, area.y + padding_vertical, area.width - padding_horizontal * 2, area.height - padding_vertical * 2, - ); - - area + ) } impl Component for Picker {