From 545acfda8884c890b78e586c86e4f7c5f9a15477 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Sun, 18 Jun 2023 12:23:15 -0500 Subject: Make file preview callback optional When Picker and FilePicker are merged, not all Pickers will be able to show a preview. Co-authored-by: Gokul Soumya --- helix-term/src/ui/mod.rs | 26 +++++++++++--------------- helix-term/src/ui/picker.rs | 15 +++++++++++---- 2 files changed, 22 insertions(+), 19 deletions(-) (limited to 'helix-term/src/ui') diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index ec328ec5..c5e66d86 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -217,21 +217,17 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi log::debug!("file_picker init {:?}", Instant::now().duration_since(now)); - FilePicker::new( - files, - root, - move |cx, path: &PathBuf, action| { - 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); - } - }, - |_editor, path| Some((path.clone().into(), None)), - ) + FilePicker::new(files, root, move |cx, path: &PathBuf, action| { + 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.clone().into(), None))) } pub mod completers { diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index c06918d4..001526c4 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -141,7 +141,7 @@ pub struct FilePicker { preview_cache: HashMap, read_buffer: Vec, /// Given an item in the picker, return the file path and line number to display. - file_fn: FileCallback, + file_fn: Option>, } impl FilePicker { @@ -149,7 +149,6 @@ impl FilePicker { options: Vec, editor_data: T::Data, callback_fn: impl Fn(&mut Context, &T, Action) + 'static, - preview_fn: impl Fn(&Editor, &T) -> Option + 'static, ) -> Self { let prompt = Prompt::new( "".into(), @@ -173,7 +172,7 @@ impl FilePicker { widths: Vec::new(), preview_cache: HashMap::new(), read_buffer: Vec::with_capacity(1024), - file_fn: Box::new(preview_fn), + file_fn: None, picker: unimplemented!(), }; @@ -202,6 +201,14 @@ impl FilePicker { self } + pub fn with_preview( + mut self, + preview_fn: impl Fn(&Editor, &T) -> Option + 'static, + ) -> Self { + self.file_fn = Some(Box::new(preview_fn)); + self + } + pub fn set_options(&mut self, new_options: Vec) { self.options = new_options; self.cursor = 0; @@ -372,7 +379,7 @@ impl FilePicker { fn current_file(&self, editor: &Editor) -> Option { self.picker .selection() - .and_then(|current| (self.file_fn)(editor, current)) + .and_then(|current| (self.file_fn.as_ref()?)(editor, current)) .and_then(|(path_or_id, line)| path_or_id.get_canonicalized().ok().zip(Some(line))) } -- cgit v1.2.3-70-g09d2