refactor: extract into a function

This commit is contained in:
Nikita Revenco 2025-02-18 15:53:56 +00:00
parent f193705ca7
commit 67ca955baa

View file

@ -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<u32>,
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(&copy_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