summaryrefslogtreecommitdiff
path: root/helix-term/src/ui
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
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')
-rw-r--r--helix-term/src/ui/mod.rs26
-rw-r--r--helix-term/src/ui/picker.rs15
2 files changed, 22 insertions, 19 deletions
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<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)))
}