From 7fdf2ba92a387fb5ae2a43cb3ad8e695eb39c7a0 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Tue, 18 Feb 2025 15:20:34 +0000 Subject: [PATCH] refactor: simplify function --- helix-term/src/ui/mod.rs | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 46c2e32c..bd1bffdb 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -382,18 +382,12 @@ fn create_file_operation_prompt( cx.jobs.callback(callback); } -fn refresh_file_explorer( - remove_previous: bool, - cursor: Option, - cx: &mut Context, - root: PathBuf, -) { +fn refresh_file_explorer(cursor: u32, cx: &mut Context, root: PathBuf) { let callback = Box::pin(async move { let call: Callback = Callback::EditorCompositor(Box::new(move |editor, compositor| { - if remove_previous { - compositor.pop(); - } - if let Ok(picker) = file_explorer(cursor, root, editor) { + // replace the old file explorer with the new one + compositor.pop(); + if let Ok(picker) = file_explorer(Some(cursor), root, editor) { compositor.push(Box::new(overlay::overlaid(picker))); } })); @@ -462,7 +456,7 @@ pub fn file_explorer( }) { return Some(Err(err)); } - refresh_file_explorer(true, Some(cursor), cx, root); + refresh_file_explorer(cursor, cx, root); Some(Ok(format!("Created directory: {}", to_create.display()))) } else { @@ -471,7 +465,7 @@ pub fn file_explorer( }) { return Some(Err(err)); }; - refresh_file_explorer(true, Some(cursor), cx, root); + refresh_file_explorer(cursor, cx, root); Some(Ok(format!("Created file: {}", to_create.display()))) } @@ -533,7 +527,7 @@ pub fn file_explorer( }) { return Some(Err(err)); }; - refresh_file_explorer(true, Some(cursor), cx, root); + refresh_file_explorer(cursor, cx, root); None }; @@ -587,7 +581,7 @@ pub fn file_explorer( }) { return Some(Err(err)); }; - refresh_file_explorer(true, Some(cursor), cx, root); + refresh_file_explorer(cursor, cx, root); Some(Ok(format!("Deleted directory: {}", to_delete.display()))) } else { @@ -596,7 +590,7 @@ pub fn file_explorer( }) { return Some(Err(err)); }; - refresh_file_explorer(true, Some(cursor), cx, root); + refresh_file_explorer(cursor, cx, root); Some(Ok(format!("Deleted file: {}", to_delete.display()))) } @@ -636,7 +630,7 @@ pub fn file_explorer( }) { return Some(Err(err)); }; - refresh_file_explorer(true, Some(cursor), cx, root); + refresh_file_explorer(cursor, cx, root); Some(Ok(format!( "Copied contents of file {} to {}", @@ -687,7 +681,16 @@ pub fn file_explorer( move |cx, (path, is_dir): &(PathBuf, bool), action| { if *is_dir { let new_root = helix_stdx::path::normalize(path); - refresh_file_explorer(false, None, cx, new_root); + let callback = Box::pin(async move { + let call: Callback = + Callback::EditorCompositor(Box::new(move |editor, compositor| { + if let Ok(picker) = file_explorer(None, new_root, editor) { + compositor.push(Box::new(overlay::overlaid(picker))); + } + })); + Ok(call) + }); + cx.jobs.callback(callback); } else if let Err(e) = cx.editor.open(path, action) { let err = if let Some(err) = e.source() { format!("{}", err)