Skip to content

Commit 3536462

Browse files
QiBaobinthomasskk
authored andcommitted
support prefilling prompt (helix-editor#2459)
* support prefilling prompt * introduce with_line builder method in Prompt * extract show_prompt * use textobject_word as fallback input
1 parent d9021f9 commit 3536462

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

helix-term/src/commands/lsp.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,9 +885,21 @@ pub fn hover(cx: &mut Context) {
885885
}
886886

887887
pub fn rename_symbol(cx: &mut Context) {
888-
ui::prompt(
888+
let (view, doc) = current_ref!(cx.editor);
889+
let text = doc.text().slice(..);
890+
let primary_selection = doc.selection(view.id).primary();
891+
let prefill = if primary_selection.len() > 1 {
892+
primary_selection
893+
} else {
894+
use helix_core::textobject::{textobject_word, TextObject};
895+
textobject_word(text, primary_selection, TextObject::Inside, 1, false)
896+
}
897+
.fragment(text)
898+
.into();
899+
ui::prompt_with_input(
889900
cx,
890901
"rename-to:".into(),
902+
prefill,
891903
None,
892904
ui::completers::none,
893905
move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {

helix-term/src/ui/mod.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,27 @@ pub fn prompt(
3838
completion_fn: impl FnMut(&Editor, &str) -> Vec<prompt::Completion> + 'static,
3939
callback_fn: impl FnMut(&mut crate::compositor::Context, &str, PromptEvent) + 'static,
4040
) {
41-
let mut prompt = Prompt::new(prompt, history_register, completion_fn, callback_fn);
41+
show_prompt(
42+
cx,
43+
Prompt::new(prompt, history_register, completion_fn, callback_fn),
44+
);
45+
}
46+
47+
pub fn prompt_with_input(
48+
cx: &mut crate::commands::Context,
49+
prompt: std::borrow::Cow<'static, str>,
50+
input: String,
51+
history_register: Option<char>,
52+
completion_fn: impl FnMut(&Editor, &str) -> Vec<prompt::Completion> + 'static,
53+
callback_fn: impl FnMut(&mut crate::compositor::Context, &str, PromptEvent) + 'static,
54+
) {
55+
show_prompt(
56+
cx,
57+
Prompt::new(prompt, history_register, completion_fn, callback_fn).with_line(input),
58+
);
59+
}
60+
61+
fn show_prompt(cx: &mut crate::commands::Context, mut prompt: Prompt) {
4262
// Calculate initial completion
4363
prompt.recalculate_completion(cx.editor);
4464
cx.push_layer(Box::new(prompt));

helix-term/src/ui/prompt.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ impl Prompt {
8484
}
8585
}
8686

87+
pub fn with_line(mut self, line: String) -> Self {
88+
let cursor = line.len();
89+
self.line = line;
90+
self.cursor = cursor;
91+
self
92+
}
93+
8794
pub fn line(&self) -> &String {
8895
&self.line
8996
}

0 commit comments

Comments
 (0)