diff options
author | Gokul Soumya | 2023-06-18 16:40:30 +0000 |
---|---|---|
committer | Michael Davis | 2023-06-18 17:01:16 +0000 |
commit | 104036bd7feed66b6f5d2467d5dbf1fbaa5ff9c3 (patch) | |
tree | b3c24cc15742903c5ae4bfc42fdf345be8aaefd1 | |
parent | 40916dff6355a741f91bc47ca0a3c6443ac2d5a3 (diff) |
Copy struct fields and new() from Picker to FilePicker
-rw-r--r-- | helix-term/src/ui/picker.rs | 167 |
1 files changed, 93 insertions, 74 deletions
diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index c357c6d6..d1f6d45e 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -78,6 +78,26 @@ type FileCallback<T> = Box<dyn Fn(&Editor, &T) -> Option<FileLocation>>; pub type FileLocation = (PathOrId, Option<(usize, usize)>); 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>, pub truncate_start: bool, /// Caches paths to documents @@ -131,17 +151,49 @@ impl<T: Item + 'static> FilePicker<T> { callback_fn: impl Fn(&mut Context, &T, Action) + 'static, preview_fn: impl Fn(&Editor, &T) -> Option<FileLocation> + 'static, ) -> Self { - let truncate_start = true; - let mut picker = Picker::new(options, editor_data, callback_fn); - picker.truncate_start = truncate_start; + let prompt = Prompt::new( + "".into(), + None, + ui::completers::none, + |_editor: &mut Context, _pattern: &str, _event: PromptEvent| {}, + ); - Self { - picker, - truncate_start, + 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(), preview_cache: HashMap::new(), read_buffer: Vec::with_capacity(1024), 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 { @@ -150,6 +202,38 @@ impl<T: Item + 'static> FilePicker<T> { 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> { self.picker .selection() @@ -477,76 +561,11 @@ impl<T: Item> Picker<T> { editor_data: T::Data, callback_fn: impl Fn(&mut Context, &T, Action) + 'static, ) -> Self { - let prompt = Prompt::new( - "".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 + unimplemented!() } 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(); + unimplemented!() } pub fn score(&mut self) { |