aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/ui/picker.rs
diff options
context:
space:
mode:
authorMichael Davis2023-06-18 17:23:15 +0000
committerMichael Davis2023-06-18 17:28:16 +0000
commit545acfda8884c890b78e586c86e4f7c5f9a15477 (patch)
treea2ff7204be3cda93bd558aedc0022b2b99e6e707 /helix-term/src/ui/picker.rs
parentfc111213b5b5a3130399cda8a1964fa89acf153a (diff)
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 <gokulps15@gmail.com>
Diffstat (limited to 'helix-term/src/ui/picker.rs')
-rw-r--r--helix-term/src/ui/picker.rs15
1 files changed, 11 insertions, 4 deletions
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<T: Item> {
preview_cache: HashMap<PathBuf, CachedPreview>,
read_buffer: Vec<u8>,
/// Given an item in the picker, return the file path and line number to display.
- file_fn: FileCallback<T>,
+ file_fn: Option<FileCallback<T>>,
}
impl<T: Item + 'static> FilePicker<T> {
@@ -149,7 +149,6 @@ impl<T: Item + 'static> FilePicker<T> {
options: Vec<T>,
editor_data: T::Data,
callback_fn: impl Fn(&mut Context, &T, Action) + 'static,
- preview_fn: impl Fn(&Editor, &T) -> Option<FileLocation> + 'static,
) -> Self {
let prompt = Prompt::new(
"".into(),
@@ -173,7 +172,7 @@ impl<T: Item + 'static> FilePicker<T> {
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<T: Item + 'static> FilePicker<T> {
self
}
+ pub fn with_preview(
+ mut self,
+ preview_fn: impl Fn(&Editor, &T) -> Option<FileLocation> + 'static,
+ ) -> Self {
+ self.file_fn = Some(Box::new(preview_fn));
+ self
+ }
+
pub fn set_options(&mut self, new_options: Vec<T>) {
self.options = new_options;
self.cursor = 0;
@@ -372,7 +379,7 @@ impl<T: Item + 'static> FilePicker<T> {
fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
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)))
}