Skip to content

Commit

Permalink
Resume last picker
Browse files Browse the repository at this point in the history
Inspired by space ' in doom emacs.
  • Loading branch information
pickfire authored and archseer committed Jul 22, 2021
1 parent df0ed80 commit eba0bbd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
14 changes: 14 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ impl Command {
file_picker,
buffer_picker,
symbol_picker,
last_picker,
prepend_to_line,
append_to_line,
open_below,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions helix-term/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type Terminal = tui::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
pub struct Compositor {
layers: Vec<Box<dyn Component>>,
terminal: Terminal,

pub(crate) last_picker: Option<Box<dyn Component>>,
}

impl Compositor {
Expand All @@ -83,6 +85,7 @@ impl Compositor {
Ok(Self {
layers: Vec::new(),
terminal,
last_picker: None,
})
}

Expand All @@ -103,8 +106,8 @@ impl Compositor {
self.layers.push(layer);
}

pub fn pop(&mut self) {
self.layers.pop();
pub fn pop(&mut self) -> Option<Box<dyn Component>> {
self.layers.pop()
}

pub fn handle_event(&mut self, event: Event, cx: &mut Context) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl<T: 'static> Component for Picker<T> {

let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| {
// remove the layer
compositor.pop();
compositor.last_picker = compositor.pop();
})));

match key_event {
Expand Down

0 comments on commit eba0bbd

Please sign in to comment.