Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support prefilling prompt #2459

Merged
merged 5 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
pickfire marked this conversation as resolved.
Show resolved Hide resolved
QiBaobin marked this conversation as resolved.
Show resolved Hide resolved
None,
ui::completers::none,
move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
Expand Down
21 changes: 20 additions & 1 deletion helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,26 @@ pub fn prompt(
completion_fn: impl FnMut(&Editor, &str) -> Vec<prompt::Completion> + '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,
)
pickfire marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn prompt_with_input(
cx: &mut crate::commands::Context,
prompt: std::borrow::Cow<'static, str>,
input: String,
history_register: Option<char>,
completion_fn: impl FnMut(&Editor, &str) -> Vec<prompt::Completion> + '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);
// Calculate initial completion
prompt.recalculate_completion(cx.editor);
cx.push_layer(Box::new(prompt));
Expand Down
7 changes: 7 additions & 0 deletions helix-term/src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,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
}
Expand Down