Copy struct fields and new() from Picker to FilePicker

pull/7264/head
Gokul Soumya 1 year ago committed by Michael Davis
parent 40916dff63
commit 104036bd7f
No known key found for this signature in database

@ -78,6 +78,26 @@ type FileCallback<T> = Box<dyn Fn(&Editor, &T) -> Option<FileLocation>>;
pub type FileLocation = (PathOrId, Option<(usize, usize)>); pub type FileLocation = (PathOrId, Option<(usize, usize)>);
pub struct FilePicker<T: Item> { pub struct FilePicker<T: Item> {
options: Vec<T>,
editor_data: T::Data,
// filter: String,
matcher: Box<Matcher>,
matches: Vec<PickerMatch>,
/// Current height of the completions box
completion_height: u16,
cursor: usize,
// pattern: String,
prompt: Prompt,
previous_pattern: (String, FuzzyQuery),
/// Whether to show the preview panel (default true)
show_preview: bool,
/// Constraints for tabular formatting
widths: Vec<Constraint>,
callback_fn: PickerCallback<T>,
picker: Picker<T>, picker: Picker<T>,
pub truncate_start: bool, pub truncate_start: bool,
/// Caches paths to documents /// Caches paths to documents
@ -131,17 +151,49 @@ impl<T: Item + 'static> FilePicker<T> {
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, preview_fn: impl Fn(&Editor, &T) -> Option<FileLocation> + 'static,
) -> Self { ) -> Self {
let truncate_start = true; let prompt = Prompt::new(
let mut picker = Picker::new(options, editor_data, callback_fn); "".into(),
picker.truncate_start = truncate_start; None,
ui::completers::none,
|_editor: &mut Context, _pattern: &str, _event: PromptEvent| {},
);
Self { let mut picker = Self {
picker, options,
truncate_start, editor_data,
matcher: Box::default(),
matches: Vec::new(),
cursor: 0,
prompt,
previous_pattern: (String::new(), FuzzyQuery::default()),
truncate_start: true,
show_preview: true,
callback_fn: Box::new(callback_fn),
completion_height: 0,
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: Box::new(preview_fn),
picker: unimplemented!(),
};
picker.calculate_column_widths();
// scoring on empty input
// TODO: just reuse score()
picker
.matches
.extend(picker.options.iter().enumerate().map(|(index, option)| {
let text = option.filter_text(&picker.editor_data);
PickerMatch {
index,
score: 0,
len: text.chars().count(),
} }
}));
picker
} }
pub fn truncate_start(mut self, truncate_start: bool) -> Self { pub fn truncate_start(mut self, truncate_start: bool) -> Self {
@ -150,6 +202,38 @@ impl<T: Item + 'static> FilePicker<T> {
self self
} }
pub fn set_options(&mut self, new_options: Vec<T>) {
self.options = new_options;
self.cursor = 0;
self.force_score();
self.calculate_column_widths();
}
/// Calculate the width constraints using the maximum widths of each column
/// for the current options.
fn calculate_column_widths(&mut self) {
let n = self
.options
.first()
.map(|option| option.format(&self.editor_data).cells.len())
.unwrap_or_default();
let max_lens = self.options.iter().fold(vec![0; n], |mut acc, option| {
let row = option.format(&self.editor_data);
// maintain max for each column
for (acc, cell) in acc.iter_mut().zip(row.cells.iter()) {
let width = cell.content.width();
if width > *acc {
*acc = width;
}
}
acc
});
self.widths = max_lens
.into_iter()
.map(|len| Constraint::Length(len as u16))
.collect();
}
fn current_file(&self, editor: &Editor) -> Option<FileLocation> { fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
self.picker self.picker
.selection() .selection()
@ -477,76 +561,11 @@ impl<T: Item> Picker<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,
) -> Self { ) -> Self {
let prompt = Prompt::new( unimplemented!()
"".into(),
None,
ui::completers::none,
|_editor: &mut Context, _pattern: &str, _event: PromptEvent| {},
);
let mut picker = Self {
options,
editor_data,
matcher: Box::default(),
matches: Vec::new(),
cursor: 0,
prompt,
previous_pattern: (String::new(), FuzzyQuery::default()),
truncate_start: true,
show_preview: true,
callback_fn: Box::new(callback_fn),
completion_height: 0,
widths: Vec::new(),
};
picker.calculate_column_widths();
// scoring on empty input
// TODO: just reuse score()
picker
.matches
.extend(picker.options.iter().enumerate().map(|(index, option)| {
let text = option.filter_text(&picker.editor_data);
PickerMatch {
index,
score: 0,
len: text.chars().count(),
}
}));
picker
} }
pub fn set_options(&mut self, new_options: Vec<T>) { pub fn set_options(&mut self, new_options: Vec<T>) {
self.options = new_options; unimplemented!()
self.cursor = 0;
self.force_score();
self.calculate_column_widths();
}
/// Calculate the width constraints using the maximum widths of each column
/// for the current options.
fn calculate_column_widths(&mut self) {
let n = self
.options
.first()
.map(|option| option.format(&self.editor_data).cells.len())
.unwrap_or_default();
let max_lens = self.options.iter().fold(vec![0; n], |mut acc, option| {
let row = option.format(&self.editor_data);
// maintain max for each column
for (acc, cell) in acc.iter_mut().zip(row.cells.iter()) {
let width = cell.content.width();
if width > *acc {
*acc = width;
}
}
acc
});
self.widths = max_lens
.into_iter()
.map(|len| Constraint::Length(len as u16))
.collect();
} }
pub fn score(&mut self) { pub fn score(&mut self) {

Loading…
Cancel
Save