From 104036bd7feed66b6f5d2467d5dbf1fbaa5ff9c3 Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Sun, 18 Jun 2023 11:40:30 -0500 Subject: [PATCH] Copy struct fields and new() from Picker to FilePicker --- helix-term/src/ui/picker.rs | 167 ++++++++++++++++++++---------------- 1 file changed, 93 insertions(+), 74 deletions(-) diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index c357c6d63..d1f6d45e7 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -78,6 +78,26 @@ fn from(v: DocumentId) -> Self { pub type FileLocation = (PathOrId, Option<(usize, usize)>); pub struct FilePicker { + options: Vec, + editor_data: T::Data, + // filter: String, + matcher: Box, + matches: Vec, + + /// 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, + + callback_fn: PickerCallback, + picker: Picker, pub truncate_start: bool, /// Caches paths to documents @@ -131,17 +151,49 @@ pub fn new( callback_fn: impl Fn(&mut Context, &T, Action) + 'static, preview_fn: impl Fn(&Editor, &T) -> Option + '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 @@ pub fn truncate_start(mut self, truncate_start: bool) -> Self { self } + pub fn set_options(&mut self, new_options: Vec) { + 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 { self.picker .selection() @@ -477,76 +561,11 @@ pub fn new( 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) { - 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) {