From e5c7aaed91c0b3d533a04840fedb88ecabc8f6a8 Mon Sep 17 00:00:00 2001 From: Bob Date: Mon, 18 Jul 2022 09:17:13 +0800 Subject: support prefilling prompt (#2459) * support prefilling prompt * introduce with_line builder method in Prompt * extract show_prompt * use textobject_word as fallback input--- helix-term/src/ui/mod.rs | 22 +++++++++++++++++++++- helix-term/src/ui/prompt.rs | 7 +++++++ 2 files changed, 28 insertions(+), 1 deletion(-) (limited to 'helix-term/src/ui') diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index c7d409e9..88a226e9 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -34,7 +34,27 @@ pub fn prompt( completion_fn: impl FnMut(&Editor, &str) -> Vec + 'static, callback_fn: impl FnMut(&mut crate::compositor::Context, &str, PromptEvent) + 'static, ) { - let mut prompt = Prompt::new(prompt, history_register, completion_fn, callback_fn); + show_prompt( + cx, + Prompt::new(prompt, history_register, completion_fn, callback_fn), + ); +} + +pub fn prompt_with_input( + cx: &mut crate::commands::Context, + prompt: std::borrow::Cow<'static, str>, + input: String, + history_register: Option, + completion_fn: impl FnMut(&Editor, &str) -> Vec + 'static, + callback_fn: impl FnMut(&mut crate::compositor::Context, &str, PromptEvent) + 'static, +) { + show_prompt( + cx, + Prompt::new(prompt, history_register, completion_fn, callback_fn).with_line(input), + ); +} + +fn show_prompt(cx: &mut crate::commands::Context, mut prompt: Prompt) { // Calculate initial completion prompt.recalculate_completion(cx.editor); cx.push_layer(Box::new(prompt)); diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index 36ee62c3..6e7df907 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -84,6 +84,13 @@ impl Prompt { } } + pub fn with_line(mut self, line: String) -> Self { + let cursor = line.len(); + self.line = line; + self.cursor = cursor; + self + } + pub fn line(&self) -> &String { &self.line } -- cgit v1.2.3-70-g09d2