From 8f5e4dff29516f2776e94b2617abcfe1f4c6408a Mon Sep 17 00:00:00 2001 From: Denys Rybalka Date: Fri, 9 Aug 2024 20:58:46 +0200 Subject: [PATCH] Pass directory content into picker directly --- helix-term/src/commands.rs | 7 +++-- helix-term/src/ui/mod.rs | 63 +++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 9bd1a561e..8685a47d2 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -375,7 +375,7 @@ impl MappableCommand { file_picker, "Open file picker", file_picker_in_current_buffer_directory, "Open file picker at current buffer's directory", file_picker_in_current_directory, "Open file picker at current working directory", - file_browser, "Open file browser at current buffer's directory", + file_browser, "Open file browser at current working directory", code_action, "Perform code action", buffer_picker, "Open buffer picker", jumplist_picker, "Open jumplist picker", @@ -2956,8 +2956,9 @@ fn file_browser(cx: &mut Context) { .set_error("Current working directory does not exist"); return; } - let picker = ui::file_browser(cwd); - cx.push_layer(Box::new(overlaid(picker))); + if let Ok(picker) = ui::file_browser(cwd) { + cx.push_layer(Box::new(overlaid(picker))); + } } fn buffer_picker(cx: &mut Context) { diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index c9814ca46..ae06013b5 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -266,8 +266,8 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi picker } -pub fn file_browser(root: PathBuf) -> FilePicker { - let directory_content = directory_content(&root); +pub fn file_browser(root: PathBuf) -> Result { + let directory_content = directory_content(&root)?; let columns = [PickerColumn::new( "path", @@ -278,38 +278,37 @@ pub fn file_browser(root: PathBuf) -> FilePicker { .into() }, )]; - let picker = Picker::new(columns, 0, [], root, move |cx, path: &PathBuf, action| { - if path.is_dir() { - let owned_path = path.clone(); - let callback = Box::pin(async move { - let call: Callback = - Callback::EditorCompositor(Box::new(move |_editor, compositor| { - let picker = file_browser(owned_path); - 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) - } else { - format!("unable to open \"{}\"", path.display()) - }; - cx.editor.set_error(err); - } - }) + let picker = Picker::new( + columns, + 0, + directory_content, + root, + move |cx, path: &PathBuf, action| { + if path.is_dir() { + let owned_path = path.clone(); + let callback = Box::pin(async move { + let call: Callback = + Callback::EditorCompositor(Box::new(move |_editor, compositor| { + if let Ok(picker) = file_browser(owned_path) { + 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) + } else { + format!("unable to open \"{}\"", path.display()) + }; + cx.editor.set_error(err); + } + }, + ) .with_preview(|_editor, path| Some((path.as_path().into(), None))); - let injector = picker.injector(); - if let Ok(files) = directory_content { - for file in files { - if injector.push(file).is_err() { - break; - } - } - } - picker + Ok(picker) } fn directory_content(path: &Path) -> Result, std::io::Error> {