From eecabdbeb565d82fb7ace279a107bcf37ce2b3cf Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Tue, 18 Feb 2025 11:16:15 +0000 Subject: [PATCH] feat: pass context to all callbacks in file operations --- helix-term/src/ui/mod.rs | 17 +++++++++-------- helix-term/src/ui/prompt.rs | 1 + 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index e0ab20eb0..ebd4137e5 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -290,12 +290,11 @@ fn create_file_operation_prompt( prompt: &'static str, cx: &mut Context, path: &Path, - callback: fn(&Path, &str) -> Result, + callback: fn(&mut Context, &Path, &str) -> Result, ) { cx.editor.path_editing = Some(path.to_path_buf()); let callback = Box::pin(async move { let call: Callback = Callback::EditorCompositor(Box::new(move |editor, compositor| { - // let path = path.clone(); let mut prompt = Prompt::new( prompt.into(), None, @@ -305,8 +304,10 @@ fn create_file_operation_prompt( return; }; - if let Some(path) = &cx.editor.path_editing { - match callback(path, input) { + let path = cx.editor.path_editing.clone(); + + if let Some(path) = path { + match callback(cx, &path, input) { Ok(msg) => cx.editor.set_status(msg), Err(msg) => cx.editor.set_error(msg), }; @@ -379,7 +380,7 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result { - create_file_operation_prompt("create:", cx, path, |_, to_create_str| { + create_file_operation_prompt("create:", cx, path, |_, _, to_create_str| { let to_create = helix_stdx::path::expand_tilde(PathBuf::from(to_create_str)); if to_create.exists() { @@ -399,7 +400,7 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result { - create_file_operation_prompt("move:", cx, path, |move_from, move_to_str| { + create_file_operation_prompt("move:", cx, path, |_, move_from, move_to_str| { let move_to = helix_stdx::path::expand_tilde(PathBuf::from(move_to_str)); if move_to.exists() { @@ -424,7 +425,7 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result { - create_file_operation_prompt("delete? (y/n):", cx, path, |_, to_delete_str| { + create_file_operation_prompt("delete? (y/n):", cx, path, |_, _, to_delete_str| { let to_delete = helix_stdx::path::expand_tilde(PathBuf::from(to_delete_str)); if to_delete_str == "y" { if !to_delete.exists() { @@ -447,7 +448,7 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result { - create_file_operation_prompt("copy-to:", cx, path, |copy_from, copy_to_str| { + create_file_operation_prompt("copy-to:", cx, path, |_, copy_from, copy_to_str| { let copy_to = helix_stdx::path::expand_tilde(PathBuf::from(copy_to_str)); if copy_from.is_dir() || copy_to_str.ends_with('/') { // TODO: support copying directories (recursively)?. This isn't built-in to the standard library diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index 5444a78fd..e887415d9 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -123,6 +123,7 @@ impl Prompt { } pub fn set_line_no_recalculate(&mut self, line: String) { + assert!(self.completion.is_empty()); let cursor = line.len(); self.line = line; self.cursor = cursor;