From b14c258a2c447b892c89d3e68ef4c9a74effca85 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Tue, 7 Jun 2022 00:08:47 +0900 Subject: prompt: If submitting empty prompt, use default (last used) --- helix-term/src/ui/mod.rs | 19 +++++++------------ helix-term/src/ui/prompt.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 14 deletions(-) (limited to 'helix-term/src/ui') diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 2dca870b..6dea2192 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -63,18 +63,8 @@ pub fn regex_prompt( doc.set_selection(view.id, snapshot.clone()); view.offset = offset_snapshot; } - PromptEvent::Validate => match Regex::new(input) { - Ok(regex) => { - let (view, doc) = current!(cx.editor); - // Equivalent to push_jump to store selection just before jump - view.jumps.push((doc_id, snapshot.clone())); - fun(view, doc, regex, event); - } - Err(_err) => (), // TODO: mark command line as error - }, - - PromptEvent::Update => { - // skip empty input, TODO: trigger default + PromptEvent::Update | PromptEvent::Validate => { + // skip empty input if input.is_empty() { return; } @@ -96,6 +86,11 @@ pub fn regex_prompt( // revert state to what it was before the last update doc.set_selection(view.id, snapshot.clone()); + if event == PromptEvent::Validate { + // Equivalent to push_jump to store selection just before jump + view.jumps.push((doc_id, snapshot.clone())); + } + fun(view, doc, regex, event); view.ensure_cursor_in_view(doc, config.scrolloff); diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index a5be33ff..64154bae 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -442,10 +442,21 @@ impl Prompt { let line = area.height - 1; // render buffer text surface.set_string(area.x, area.y + line, &self.prompt, prompt_color); + + let input: Cow = if self.line.is_empty() { + // latest value in the register list + self.history_register + .and_then(|reg| cx.editor.registers.first(reg).cloned()) // TODO: can we avoid cloning? + .map(|entry| entry.into()) + .unwrap_or_else(|| Cow::from("")) + } else { + self.line.as_str().into() + }; + surface.set_string( area.x + self.prompt.len() as u16, area.y + line, - &self.line, + &input, prompt_color, ); } @@ -510,7 +521,18 @@ impl Component for Prompt { self.recalculate_completion(cx.editor); self.exit_selection(); } else { - (self.callback_fn)(cx, &self.line, PromptEvent::Validate); + // handle executing with last command in history if nothing entered + let input: Cow = if self.line.is_empty() { + // latest value in the register list + self.history_register + .and_then(|reg| cx.editor.registers.first(reg).cloned()) + .map(|entry| entry.into()) + .unwrap_or_else(|| Cow::from("")) + } else { + self.line.as_str().into() + }; + + (self.callback_fn)(cx, &input, PromptEvent::Validate); if let Some(register) = self.history_register { // store in history -- cgit v1.2.3-70-g09d2