on master help sign in
support prefilling prompt (#2459)
e5c7aaeBob
helix-term/src/commands/lsp.rs+13−1
@@ -885,9 +885,21 @@ pub fn hover(cx: &mut Context) {
885}
885}
886
886
887pub fn rename_symbol(cx: &mut Context) {
887pub 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(
889 cx,
900 cx,
890 "rename-to:".into(),
901 "rename-to:".into(),
902+ prefill,
891 None,
903 None,
892 ui::completers::none,
904 ui::completers::none,
893 move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
905 move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
helix-term/src/ui/mod.rs+21−1
@@ -34,7 +34,27 @@ pub fn prompt(
34 completion_fn: impl FnMut(&Editor, &str) -> Vec<prompt::Completion> + 'static,
34 completion_fn: impl FnMut(&Editor, &str) -> Vec<prompt::Completion> + 'static,
35 callback_fn: impl FnMut(&mut crate::compositor::Context, &str, PromptEvent) + 'static,
35 callback_fn: impl FnMut(&mut crate::compositor::Context, &str, PromptEvent) + 'static,
36) {
36) {
37+ show_prompt(
38+ cx,
39+ Prompt::new(prompt, history_register, completion_fn, callback_fn),
40+ );
41+}
42+
43+pub fn prompt_with_input(
44+ cx: &mut crate::commands::Context,
45+ prompt: std::borrow::Cow<'static, str>,
46+ input: String,
47+ history_register: Option<char>,
48+ completion_fn: impl FnMut(&Editor, &str) -> Vec<prompt::Completion> + 'static,
49+ callback_fn: impl FnMut(&mut crate::compositor::Context, &str, PromptEvent) + 'static,
50+) {
51+ show_prompt(
52+ cx,
37− let mut prompt = Prompt::new(prompt, history_register, completion_fn, callback_fn);
53+ Prompt::new(prompt, history_register, completion_fn, callback_fn).with_line(input),
54+ );
55+}
56+
57+fn show_prompt(cx: &mut crate::commands::Context, mut prompt: Prompt) {
38 // Calculate initial completion
58 // Calculate initial completion
39 prompt.recalculate_completion(cx.editor);
59 prompt.recalculate_completion(cx.editor);
40 cx.push_layer(Box::new(prompt));
60 cx.push_layer(Box::new(prompt));
helix-term/src/ui/prompt.rs+7
@@ -84,6 +84,13 @@ impl Prompt {
84 }
84 }
85 }
85 }
86
86
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+
87 pub fn line(&self) -> &String {
94 pub fn line(&self) -> &String {
88 &self.line
95 &self.line
89 }
96 }