From 67ca955baa5b716bdc05dac2d87ebd11e5244f8d Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Tue, 18 Feb 2025 15:53:56 +0000 Subject: [PATCH] refactor: extract into a function --- helix-term/src/ui/mod.rs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 675c1c5b3..bf9bbb0c6 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -396,6 +396,16 @@ fn refresh_file_explorer(cursor: u32, cx: &mut Context, root: PathBuf) { cx.jobs.callback(callback); } +/// We don't have access to the file explorer's current directory directly, +/// but we can get it by taking any of the children of the explorer +/// and obtaining their parents. +fn root_from_child(child: &Path) -> PathBuf { + child + .parent() + .map(|p| p.to_path_buf()) + .unwrap_or(helix_stdx::env::current_working_dir()) +} + pub fn file_explorer( cursor: Option, root: PathBuf, @@ -471,10 +481,7 @@ pub fn file_explorer( } }; - let root = path - .parent() - .map(|p| p.to_path_buf()) - .unwrap_or(helix_stdx::env::current_working_dir()); + let root = root_from_child(path); if to_create.exists() { create_confirmation_prompt( @@ -531,10 +538,7 @@ pub fn file_explorer( None }; - let root = move_from - .parent() - .map(|p| p.to_path_buf()) - .unwrap_or(helix_stdx::env::current_working_dir()); + let root = root_from_child(move_from); if move_to.exists() { create_confirmation_prompt( @@ -570,10 +574,7 @@ pub fn file_explorer( return Some(Err(format!("Path {} does not exist", to_delete.display()))); }; - let root = to_delete - .parent() - .map(|p| p.to_path_buf()) - .unwrap_or(helix_stdx::env::current_working_dir()); + let root = root_from_child(to_delete); if confirmation.ends_with(std::path::MAIN_SEPARATOR) { if let Err(err) = fs::remove_dir_all(to_delete).map_err(|err| { @@ -639,10 +640,7 @@ pub fn file_explorer( ))) }; - let root = copy_to - .parent() - .map(|p| p.to_path_buf()) - .unwrap_or(helix_stdx::env::current_working_dir()); + let root = root_from_child(©_to); if copy_from.is_dir() || copy_to_str.ends_with(std::path::MAIN_SEPARATOR) { // TODO: support copying directories (recursively)?. This isn't built-in to the standard library