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>
pull/7264/head
Michael Davis 1 year ago
parent fc111213b5
commit 545acfda88
No known key found for this signature in database

@ -2184,11 +2184,9 @@ fn global_search(cx: &mut Context) {
doc.set_selection(view.id, Selection::single(start, end)); doc.set_selection(view.id, Selection::single(start, end));
align_view(doc, view, Align::Center); align_view(doc, view, Align::Center);
}, }).with_preview(|_editor, FileResult { path, line_num }| {
|_editor, FileResult { path, line_num }| {
Some((path.clone().into(), Some((*line_num, *line_num)))) Some((path.clone().into(), Some((*line_num, *line_num))))
}, });
);
compositor.push(Box::new(overlaid(picker))); compositor.push(Box::new(overlaid(picker)));
}, },
)); ));
@ -2579,13 +2577,10 @@ fn buffer_picker(cx: &mut Context) {
// mru // mru
items.sort_unstable_by_key(|item| std::cmp::Reverse(item.focused_at)); items.sort_unstable_by_key(|item| std::cmp::Reverse(item.focused_at));
let picker = FilePicker::new( let picker = FilePicker::new(items, (), |cx, meta, action| {
items,
(),
|cx, meta, action| {
cx.editor.switch(meta.id, action); cx.editor.switch(meta.id, action);
}, })
|editor, meta| { .with_preview(|editor, meta| {
let doc = &editor.documents.get(&meta.id)?; let doc = &editor.documents.get(&meta.id)?;
let &view_id = doc.selections().keys().next()?; let &view_id = doc.selections().keys().next()?;
let line = doc let line = doc
@ -2593,8 +2588,7 @@ fn buffer_picker(cx: &mut Context) {
.primary() .primary()
.cursor_line(doc.text().slice(..)); .cursor_line(doc.text().slice(..));
Some((meta.id.into(), Some((line, line)))) Some((meta.id.into(), Some((line, line))))
}, });
);
cx.push_layer(Box::new(overlaid(picker))); cx.push_layer(Box::new(overlaid(picker)));
} }
@ -2678,12 +2672,12 @@ fn jumplist_picker(cx: &mut Context) {
doc.set_selection(view.id, meta.selection.clone()); doc.set_selection(view.id, meta.selection.clone());
view.ensure_cursor_in_view_center(doc, config.scrolloff); view.ensure_cursor_in_view_center(doc, config.scrolloff);
}, },
|editor, meta| { )
.with_preview(|editor, meta| {
let doc = &editor.documents.get(&meta.id)?; let doc = &editor.documents.get(&meta.id)?;
let line = meta.selection.primary().cursor_line(doc.text().slice(..)); let line = meta.selection.primary().cursor_line(doc.text().slice(..));
Some((meta.id.into(), Some((line, line)))) Some((meta.id.into(), Some((line, line))))
}, });
);
cx.push_layer(Box::new(overlaid(picker))); cx.push_layer(Box::new(overlaid(picker)));
} }

@ -73,11 +73,10 @@ fn thread_picker(
let debugger = debugger!(editor); let debugger = debugger!(editor);
let thread_states = debugger.thread_states.clone(); let thread_states = debugger.thread_states.clone();
let picker = FilePicker::new( let picker = FilePicker::new(threads, thread_states, move |cx, thread, _action| {
threads, callback_fn(cx.editor, thread)
thread_states, })
move |cx, thread, _action| callback_fn(cx.editor, thread), .with_preview(move |editor, thread| {
move |editor, thread| {
let frames = editor.debugger.as_ref()?.stack_frames.get(&thread.id)?; let frames = editor.debugger.as_ref()?.stack_frames.get(&thread.id)?;
let frame = frames.get(0)?; let frame = frames.get(0)?;
let path = frame.source.as_ref()?.path.clone()?; let path = frame.source.as_ref()?.path.clone()?;
@ -86,8 +85,7 @@ fn thread_picker(
frame.end_line.unwrap_or(frame.line).saturating_sub(1), frame.end_line.unwrap_or(frame.line).saturating_sub(1),
)); ));
Some((path.into(), pos)) Some((path.into(), pos))
}, });
);
compositor.push(Box::new(picker)); compositor.push(Box::new(picker));
}, },
); );
@ -728,10 +726,7 @@ pub fn dap_switch_stack_frame(cx: &mut Context) {
let frames = debugger.stack_frames[&thread_id].clone(); let frames = debugger.stack_frames[&thread_id].clone();
let picker = FilePicker::new( let picker = FilePicker::new(frames, (), move |cx, frame, _action| {
frames,
(),
move |cx, frame, _action| {
let debugger = debugger!(cx.editor); let debugger = debugger!(cx.editor);
// TODO: this should be simpler to find // TODO: this should be simpler to find
let pos = debugger.stack_frames[&thread_id] let pos = debugger.stack_frames[&thread_id]
@ -745,8 +740,8 @@ pub fn dap_switch_stack_frame(cx: &mut Context) {
if let Some(frame) = &frame { if let Some(frame) = &frame {
jump_to_stack_frame(cx.editor, frame); jump_to_stack_frame(cx.editor, frame);
} }
}, })
move |_editor, frame| { .with_preview(move |_editor, frame| {
frame frame
.source .source
.as_ref() .as_ref()
@ -760,7 +755,6 @@ pub fn dap_switch_stack_frame(cx: &mut Context) {
)), )),
) )
}) })
}, });
);
cx.push_layer(Box::new(picker)) cx.push_layer(Box::new(picker))
} }

@ -240,10 +240,7 @@ type SymbolPicker = FilePicker<SymbolInformationItem>;
fn sym_picker(symbols: Vec<SymbolInformationItem>, current_path: Option<lsp::Url>) -> SymbolPicker { fn sym_picker(symbols: Vec<SymbolInformationItem>, current_path: Option<lsp::Url>) -> SymbolPicker {
// TODO: drop current_path comparison and instead use workspace: bool flag? // TODO: drop current_path comparison and instead use workspace: bool flag?
FilePicker::new( FilePicker::new(symbols, current_path.clone(), move |cx, item, action| {
symbols,
current_path.clone(),
move |cx, item, action| {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
push_jump(view, doc); push_jump(view, doc);
@ -275,9 +272,8 @@ fn sym_picker(symbols: Vec<SymbolInformationItem>, current_path: Option<lsp::Url
doc.set_selection(view.id, Selection::single(range.head, range.anchor)); doc.set_selection(view.id, Selection::single(range.head, range.anchor));
align_view(doc, view, Align::Center); align_view(doc, view, Align::Center);
} }
}, })
move |_editor, item| Some(location_to_file_location(&item.symbol.location)), .with_preview(move |_editor, item| Some(location_to_file_location(&item.symbol.location)))
)
.truncate_start(false) .truncate_start(false)
} }
@ -345,11 +341,11 @@ fn diag_picker(
align_view(doc, view, Align::Center); align_view(doc, view, Align::Center);
} }
}, },
move |_editor, PickerDiagnostic { url, diag, .. }| { )
.with_preview(move |_editor, PickerDiagnostic { url, diag, .. }| {
let location = lsp::Location::new(url.clone(), diag.range); let location = lsp::Location::new(url.clone(), diag.range);
Some(location_to_file_location(&location)) Some(location_to_file_location(&location))
}, })
)
.truncate_start(false) .truncate_start(false)
} }
@ -1047,14 +1043,10 @@ fn goto_impl(
editor.set_error("No definition found."); editor.set_error("No definition found.");
} }
_locations => { _locations => {
let picker = FilePicker::new( let picker = FilePicker::new(locations, cwdir, move |cx, location, action| {
locations,
cwdir,
move |cx, location, action| {
jump_to_location(cx.editor, location, offset_encoding, action) jump_to_location(cx.editor, location, offset_encoding, action)
}, })
move |_editor, location| Some(location_to_file_location(location)), .with_preview(move |_editor, location| Some(location_to_file_location(location)));
);
compositor.push(Box::new(overlaid(picker))); compositor.push(Box::new(overlaid(picker)));
} }
} }

@ -217,10 +217,7 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
log::debug!("file_picker init {:?}", Instant::now().duration_since(now)); log::debug!("file_picker init {:?}", Instant::now().duration_since(now));
FilePicker::new( FilePicker::new(files, root, move |cx, path: &PathBuf, action| {
files,
root,
move |cx, path: &PathBuf, action| {
if let Err(e) = cx.editor.open(path, action) { if let Err(e) = cx.editor.open(path, action) {
let err = if let Some(err) = e.source() { let err = if let Some(err) = e.source() {
format!("{}", err) format!("{}", err)
@ -229,9 +226,8 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
}; };
cx.editor.set_error(err); cx.editor.set_error(err);
} }
}, })
|_editor, path| Some((path.clone().into(), None)), .with_preview(|_editor, path| Some((path.clone().into(), None)))
)
} }
pub mod completers { pub mod completers {

@ -141,7 +141,7 @@ pub struct FilePicker<T: Item> {
preview_cache: HashMap<PathBuf, CachedPreview>, preview_cache: HashMap<PathBuf, CachedPreview>,
read_buffer: Vec<u8>, read_buffer: Vec<u8>,
/// Given an item in the picker, return the file path and line number to display. /// 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> { impl<T: Item + 'static> FilePicker<T> {
@ -149,7 +149,6 @@ impl<T: Item + 'static> FilePicker<T> {
options: Vec<T>, options: Vec<T>,
editor_data: T::Data, editor_data: T::Data,
callback_fn: impl Fn(&mut Context, &T, Action) + 'static, callback_fn: impl Fn(&mut Context, &T, Action) + 'static,
preview_fn: impl Fn(&Editor, &T) -> Option<FileLocation> + 'static,
) -> Self { ) -> Self {
let prompt = Prompt::new( let prompt = Prompt::new(
"".into(), "".into(),
@ -173,7 +172,7 @@ impl<T: Item + 'static> FilePicker<T> {
widths: Vec::new(), widths: Vec::new(),
preview_cache: HashMap::new(), preview_cache: HashMap::new(),
read_buffer: Vec::with_capacity(1024), read_buffer: Vec::with_capacity(1024),
file_fn: Box::new(preview_fn), file_fn: None,
picker: unimplemented!(), picker: unimplemented!(),
}; };
@ -202,6 +201,14 @@ impl<T: Item + 'static> FilePicker<T> {
self 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>) { pub fn set_options(&mut self, new_options: Vec<T>) {
self.options = new_options; self.options = new_options;
self.cursor = 0; self.cursor = 0;
@ -372,7 +379,7 @@ impl<T: Item + 'static> FilePicker<T> {
fn current_file(&self, editor: &Editor) -> Option<FileLocation> { fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
self.picker self.picker
.selection() .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))) .and_then(|(path_or_id, line)| path_or_id.get_canonicalized().ok().zip(Some(line)))
} }

Loading…
Cancel
Save