From 759374d3f6eb10ad8a684609679c55e51fadb2e9 Mon Sep 17 00:00:00 2001 From: Bob Qi Date: Wed, 11 May 2022 16:49:44 +0800 Subject: [PATCH 1/4] support prefilling prompt --- helix-term/src/commands/lsp.rs | 13 ++++++++++++- helix-term/src/ui/mod.rs | 21 ++++++++++++++++++++- helix-term/src/ui/prompt.rs | 21 +++++++++++++++++++-- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index 90a1ad7f834d..2e5c76f21c30 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -657,9 +657,20 @@ pub fn hover(cx: &mut Context) { ); } pub fn rename_symbol(cx: &mut Context) { - ui::prompt( + let (view, doc) = current_ref!(cx.editor); + let primary_selection = doc + .selection(view.id) + .primary() + .fragment(doc.text().slice(..)) + .to_string(); + ui::prompt_with_input( cx, "rename-to:".into(), + if primary_selection.len() > 1 { + primary_selection + } else { + String::new() + }, None, ui::completers::none, move |cx: &mut compositor::Context, input: &str, event: PromptEvent| { diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 2dca870baa42..b9d38752f5ee 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -33,7 +33,26 @@ 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); + prompt_with_input( + cx, + prompt, + String::new(), + 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, +) { + let mut prompt = + Prompt::new_with_input(prompt, input, history_register, completion_fn, callback_fn); // 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 ef08edf2d836..0e275924d36c 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -67,10 +67,27 @@ impl Prompt { completion_fn: impl FnMut(&Editor, &str) -> Vec + 'static, callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static, ) -> Self { + Self::new_with_input( + prompt, + String::new(), + history_register, + completion_fn, + callback_fn, + ) + } + pub fn new_with_input( + prompt: Cow<'static, str>, + input: String, + history_register: Option, + completion_fn: impl FnMut(&Editor, &str) -> Vec + 'static, + callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static, + ) -> Self { + let line = input; + let cursor = line.len(); Self { prompt, - line: String::new(), - cursor: 0, + line, + cursor, completion: Vec::new(), selection: None, history_register, From d05e84fab17b63dc1b398ab2be379f260d56ad4c Mon Sep 17 00:00:00 2001 From: Bob Qi Date: Thu, 12 May 2022 09:37:12 +0800 Subject: [PATCH 2/4] introduce with_line builder method in Prompt --- helix-term/src/ui/mod.rs | 2 +- helix-term/src/ui/prompt.rs | 28 +++++++++------------------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index b9d38752f5ee..6f101f0071fc 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -52,7 +52,7 @@ pub fn prompt_with_input( callback_fn: impl FnMut(&mut crate::compositor::Context, &str, PromptEvent) + 'static, ) { let mut prompt = - Prompt::new_with_input(prompt, input, history_register, completion_fn, callback_fn); + Prompt::new(prompt, history_register, completion_fn, callback_fn).with_line(input); // 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 0e275924d36c..430ed3955f20 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -67,27 +67,10 @@ impl Prompt { completion_fn: impl FnMut(&Editor, &str) -> Vec + 'static, callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static, ) -> Self { - Self::new_with_input( - prompt, - String::new(), - history_register, - completion_fn, - callback_fn, - ) - } - pub fn new_with_input( - prompt: Cow<'static, str>, - input: String, - history_register: Option, - completion_fn: impl FnMut(&Editor, &str) -> Vec + 'static, - callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static, - ) -> Self { - let line = input; - let cursor = line.len(); Self { prompt, - line, - cursor, + line: String::new(), + cursor: 0, completion: Vec::new(), selection: None, history_register, @@ -98,6 +81,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 } From ccade2b9cfe4a37702008a1059d3637748c517b8 Mon Sep 17 00:00:00 2001 From: Bob Qi Date: Mon, 16 May 2022 09:38:38 +0800 Subject: [PATCH 3/4] extract show_prompt --- helix-term/src/ui/mod.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 6f101f0071fc..5c48c67716b9 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -33,14 +33,10 @@ pub fn prompt( completion_fn: impl FnMut(&Editor, &str) -> Vec + 'static, callback_fn: impl FnMut(&mut crate::compositor::Context, &str, PromptEvent) + 'static, ) { - prompt_with_input( + show_prompt( cx, - prompt, - String::new(), - history_register, - completion_fn, - callback_fn, - ) + Prompt::new(prompt, history_register, completion_fn, callback_fn), + ); } pub fn prompt_with_input( @@ -51,8 +47,13 @@ pub fn prompt_with_input( 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).with_line(input); + 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)); From 1eec2280f0d641eddf41ddd8e0fd738107d9bcdb Mon Sep 17 00:00:00 2001 From: Bob Qi Date: Sun, 17 Jul 2022 08:18:57 +0800 Subject: [PATCH 4/4] use textobject_word as fallback input --- helix-term/src/commands/lsp.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index 9401559f6156..a91e379221b4 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -886,19 +886,20 @@ pub fn hover(cx: &mut Context) { pub fn rename_symbol(cx: &mut Context) { let (view, doc) = current_ref!(cx.editor); - let primary_selection = doc - .selection(view.id) - .primary() - .fragment(doc.text().slice(..)) - .to_string(); + let text = doc.text().slice(..); + let primary_selection = doc.selection(view.id).primary(); + let prefill = if primary_selection.len() > 1 { + primary_selection + } else { + use helix_core::textobject::{textobject_word, TextObject}; + textobject_word(text, primary_selection, TextObject::Inside, 1, false) + } + .fragment(text) + .into(); ui::prompt_with_input( cx, "rename-to:".into(), - if primary_selection.len() > 1 { - primary_selection - } else { - String::new() - }, + prefill, None, ui::completers::none, move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {