@@ -20,6 +20,8 @@ pub struct Prompt {
20 cursor: usize,
20 cursor: usize,
21 completion: Vec<Completion>,
21 completion: Vec<Completion>,
22 selection: Option<usize>,
22 selection: Option<usize>,
23+ history_register: Option<char>,
24+ history_pos: Option<usize>,
23 completion_fn: Box<dyn FnMut(&str) -> Vec<Completion>>,
25 completion_fn: Box<dyn FnMut(&str) -> Vec<Completion>>,
24 callback_fn: Box<dyn FnMut(&mut Context, &str, PromptEvent)>,
26 callback_fn: Box<dyn FnMut(&mut Context, &str, PromptEvent)>,
25 pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
27 pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
@@ -54,6 +56,7 @@ pub enum Movement {
54impl Prompt {
56impl Prompt {
55 pub fn new(
57 pub fn new(
56 prompt: String,
58 prompt: String,
59+ history_register: Option<char>,
57 mut completion_fn: impl FnMut(&str) -> Vec<Completion> + 'static,
60 mut completion_fn: impl FnMut(&str) -> Vec<Completion> + 'static,
58 callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static,
61 callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static,
59 ) -> Self {
62 ) -> Self {
@@ -63,6 +66,8 @@ impl Prompt {
63 cursor: 0,
66 cursor: 0,
64 completion: completion_fn(""),
67 completion: completion_fn(""),
65 selection: None,
68 selection: None,
66 completion_fn: Box::new(completion_fn),
71 completion_fn: Box::new(completion_fn),
67 callback_fn: Box::new(callback_fn),
72 callback_fn: Box::new(callback_fn),
68 doc_fn: Box::new(|_| None),
73 doc_fn: Box::new(|_| None),
@@ -226,6 +231,28 @@ impl Prompt {
226 self.exit_selection();
231 self.exit_selection();
234+ pub fn change_history(&mut self, register: &[String], direction: CompletionDirection) {
235+ if register.is_empty() {
239+ let end = register.len().saturating_sub(1);
241+ let index = match direction {
242+ CompletionDirection::Forward => self.history_pos.map_or(0, |i| i + 1),
243+ CompletionDirection::Backward => {
244+ self.history_pos.unwrap_or(register.len()).saturating_sub(1)
249+ self.line = register[index].clone();
251+ self.history_pos = Some(index);
229 pub fn change_completion_selection(&mut self, direction: CompletionDirection) {
256 pub fn change_completion_selection(&mut self, direction: CompletionDirection) {
230 if self.completion.is_empty() {
257 if self.completion.is_empty() {
@@ -468,9 +495,40 @@ impl Component for Prompt {
468 self.exit_selection();
495 self.exit_selection();
470 (self.callback_fn)(cx, &self.line, PromptEvent::Validate);
497 (self.callback_fn)(cx, &self.line, PromptEvent::Validate);
499+ if let Some(register) = self.history_register {
501+ let register = cx.editor.registers.get_mut(register);
502+ register.push(self.line.clone());
471 return close_fn;
504 return close_fn;
508+ code: KeyCode::Char('p'),
509+ modifiers: KeyModifiers::CONTROL,
512+ code: KeyCode::Up, ..
514+ if let Some(register) = self.history_register {
515+ let register = cx.editor.registers.get_mut(register);
516+ self.change_history(register.read(), CompletionDirection::Backward);
520+ code: KeyCode::Char('n'),
521+ modifiers: KeyModifiers::CONTROL,
524+ code: KeyCode::Down,
527+ if let Some(register) = self.history_register {
528+ let register = cx.editor.registers.get_mut(register);
529+ self.change_history(register.read(), CompletionDirection::Forward);
474 KeyEvent {
532 KeyEvent {
475 code: KeyCode::Tab, ..
533 code: KeyCode::Tab, ..
476 } => self.change_completion_selection(CompletionDirection::Forward),
534 } => self.change_completion_selection(CompletionDirection::Forward),