support insert register in prompt

pull/2458/head
Bob Qi 3 years ago
parent 7b287f636f
commit d58850f194

@ -367,6 +367,7 @@ Keys to use within prompt, Remapping currently not supported.
| `Ctrl-s` | Insert a word under doc cursor, may be changed to Ctrl-r Ctrl-w later |
| `Ctrl-p`, `Up` | Select previous history |
| `Ctrl-n`, `Down` | Select next history |
| `Ctrl-r` | Insert the content of the register selected by following input char |
| `Tab` | Select next completion item |
| `BackTab` | Select previous completion item |
| `Enter` | Open selected |

@ -28,6 +28,7 @@ pub struct Prompt {
completion_fn: Box<dyn FnMut(&Editor, &str) -> Vec<Completion>>,
callback_fn: Box<dyn FnMut(&mut Context, &str, PromptEvent)>,
pub doc_fn: Box<dyn Fn(&str) -> Option<Cow<str>>>,
selecting_register: bool,
}
#[derive(Clone, Copy, PartialEq)]
@ -78,6 +79,7 @@ impl Prompt {
completion_fn: Box::new(completion_fn),
callback_fn: Box::new(callback_fn),
doc_fn: Box::new(|_| None),
selecting_register: false,
}
}
@ -538,17 +540,33 @@ impl Component for Prompt {
(self.callback_fn)(cx, &self.line, PromptEvent::Update)
}
ctrl!('q') => self.exit_selection(),
ctrl!('r') => {
self.selecting_register = true;
(self.callback_fn)(cx, &self.line, PromptEvent::Update);
return EventResult::Consumed(None);
}
// any char event that's not mapped to any other combo
KeyEvent {
code: KeyCode::Char(c),
modifiers: _,
} => {
self.insert_char(c, cx);
if self.selecting_register {
self.insert_str(
cx.editor
.registers
.read(c)
.and_then(|r| r.first())
.map_or("", |r| r.as_str()),
);
} else {
self.insert_char(c, cx);
}
(self.callback_fn)(cx, &self.line, PromptEvent::Update);
}
_ => (),
};
self.selecting_register = false;
EventResult::Consumed(None)
}

Loading…
Cancel
Save