From eba0bbda2ee10e9ba53f01f1d324d2c4c82229d2 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Sun, 18 Jul 2021 12:24:07 +0800 Subject: [PATCH] Resume last picker Inspired by space ' in doom emacs. --- helix-term/src/commands.rs | 14 ++++++++++++++ helix-term/src/compositor.rs | 7 +++++-- helix-term/src/ui/picker.rs | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 5fc96cd9..9b72a8e9 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -217,6 +217,7 @@ impl Command { file_picker, buffer_picker, symbol_picker, + last_picker, prepend_to_line, append_to_line, open_below, @@ -2091,6 +2092,17 @@ fn symbol_picker(cx: &mut Context) { ) } +fn last_picker(cx: &mut Context) { + // TODO: last picker does not seemed to work well with buffer_picker + cx.callback = Some(Box::new(|compositor: &mut Compositor| { + if let Some(picker) = compositor.last_picker.take() { + compositor.push(picker); + } + // XXX: figure out how to show error when no last picker lifetime + // cx.editor.set_error("no last picker".to_owned()) + })); +} + // I inserts at the first nonwhitespace character of each line with a selection fn prepend_to_line(cx: &mut Context) { goto_first_nonwhitespace(cx); @@ -3749,6 +3761,8 @@ macro_rules! mode_info { mode_info! { /// space mode space_mode, SPACE_MODE, + /// resume last picker + "'" => last_picker, /// file picker "f" => file_picker, /// buffer picker diff --git a/helix-term/src/compositor.rs b/helix-term/src/compositor.rs index 5fcb552a..c2cfa3a7 100644 --- a/helix-term/src/compositor.rs +++ b/helix-term/src/compositor.rs @@ -74,6 +74,8 @@ type Terminal = tui::terminal::Terminal>; pub struct Compositor { layers: Vec>, terminal: Terminal, + + pub(crate) last_picker: Option>, } impl Compositor { @@ -83,6 +85,7 @@ impl Compositor { Ok(Self { layers: Vec::new(), terminal, + last_picker: None, }) } @@ -103,8 +106,8 @@ impl Compositor { self.layers.push(layer); } - pub fn pop(&mut self) { - self.layers.pop(); + pub fn pop(&mut self) -> Option> { + self.layers.pop() } pub fn handle_event(&mut self, event: Event, cx: &mut Context) -> bool { diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index d7fc9d86..733be2fc 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -159,7 +159,7 @@ impl Component for Picker { let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| { // remove the layer - compositor.pop(); + compositor.last_picker = compositor.pop(); }))); match key_event {