feat: pass context to all callbacks in file operations

pull/12902/head
Nikita Revenco 2025-02-18 11:16:15 +00:00
parent 7fd7b7274a
commit eecabdbeb5
2 changed files with 10 additions and 8 deletions

View File

@ -290,12 +290,11 @@ fn create_file_operation_prompt(
prompt: &'static str, prompt: &'static str,
cx: &mut Context, cx: &mut Context,
path: &Path, path: &Path,
callback: fn(&Path, &str) -> Result<String, String>, callback: fn(&mut Context, &Path, &str) -> Result<String, String>,
) { ) {
cx.editor.path_editing = Some(path.to_path_buf()); cx.editor.path_editing = Some(path.to_path_buf());
let callback = Box::pin(async move { let callback = Box::pin(async move {
let call: Callback = Callback::EditorCompositor(Box::new(move |editor, compositor| { let call: Callback = Callback::EditorCompositor(Box::new(move |editor, compositor| {
// let path = path.clone();
let mut prompt = Prompt::new( let mut prompt = Prompt::new(
prompt.into(), prompt.into(),
None, None,
@ -305,8 +304,10 @@ fn create_file_operation_prompt(
return; return;
}; };
if let Some(path) = &cx.editor.path_editing { let path = cx.editor.path_editing.clone();
match callback(path, input) {
if let Some(path) = path {
match callback(cx, &path, input) {
Ok(msg) => cx.editor.set_status(msg), Ok(msg) => cx.editor.set_status(msg),
Err(msg) => cx.editor.set_error(msg), Err(msg) => cx.editor.set_error(msg),
}; };
@ -379,7 +380,7 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
|cx, (path, _is_dir): &(PathBuf, bool)|, |cx, (path, _is_dir): &(PathBuf, bool)|,
// create // create
alt!('n') => { alt!('n') => {
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)); let to_create = helix_stdx::path::expand_tilde(PathBuf::from(to_create_str));
if to_create.exists() { if to_create.exists() {
@ -399,7 +400,7 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
}, },
// move // move
alt!('m') => { alt!('m') => {
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)); let move_to = helix_stdx::path::expand_tilde(PathBuf::from(move_to_str));
if move_to.exists() { if move_to.exists() {
@ -424,7 +425,7 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
}, },
// delete // delete
alt!('d') => { alt!('d') => {
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)); let to_delete = helix_stdx::path::expand_tilde(PathBuf::from(to_delete_str));
if to_delete_str == "y" { if to_delete_str == "y" {
if !to_delete.exists() { if !to_delete.exists() {
@ -447,7 +448,7 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
}, },
// copy contents // copy contents
alt!('c') => { alt!('c') => {
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)); let copy_to = helix_stdx::path::expand_tilde(PathBuf::from(copy_to_str));
if copy_from.is_dir() || copy_to_str.ends_with('/') { if copy_from.is_dir() || copy_to_str.ends_with('/') {
// TODO: support copying directories (recursively)?. This isn't built-in to the standard library // TODO: support copying directories (recursively)?. This isn't built-in to the standard library

View File

@ -123,6 +123,7 @@ impl Prompt {
} }
pub fn set_line_no_recalculate(&mut self, line: String) { pub fn set_line_no_recalculate(&mut self, line: String) {
assert!(self.completion.is_empty());
let cursor = line.len(); let cursor = line.len();
self.line = line; self.line = line;
self.cursor = cursor; self.cursor = cursor;