on master help sign in
Copy struct fields and new() from Picker to FilePicker
commit104036bd7feed66b6f5d2467d5dbf1fbaa5ff9c3 authorGokul Soumya <gokulps15@gmail.com> dateSun 18 Jun 2023, 11:40 (3 years ago) parent40916df
1 file changed+93−74
helix-term/src/ui/picker.rs+93−74
helix-term/src/ui/picker.rs+93−74
@@ -78,6 +78,26 @@ type FileCallback<T> = Box<dyn Fn(&Editor, &T) -> Option<FileLocation>>;
7878pub type FileLocation = (PathOrId, Option<(usize, usize)>);
7979
8080pub struct FilePicker<T: Item> {
81+ options: Vec<T>,
82+ editor_data: T::Data,
83+ // filter: String,
84+ matcher: Box<Matcher>,
85+ matches: Vec<PickerMatch>,
86+
87+ /// Current height of the completions box
88+ completion_height: u16,
89+
90+ cursor: usize,
91+ // pattern: String,
92+ prompt: Prompt,
93+ previous_pattern: (String, FuzzyQuery),
94+ /// Whether to show the preview panel (default true)
95+ show_preview: bool,
96+ /// Constraints for tabular formatting
97+ widths: Vec<Constraint>,
98+
99+ callback_fn: PickerCallback<T>,
100+
81101 picker: Picker<T>,
82102 pub truncate_start: bool,
83103 /// Caches paths to documents
@@ -131,17 +151,49 @@ impl<T: Item + 'static> FilePicker<T> {
131151 callback_fn: impl Fn(&mut Context, &T, Action) + 'static,
132152 preview_fn: impl Fn(&Editor, &T) -> Option<FileLocation> + 'static,
133153 ) -> Self {
134− let truncate_start = true;
135− let mut picker = Picker::new(options, editor_data, callback_fn);
136− picker.truncate_start = truncate_start;
154+ let prompt = Prompt::new(
155+ "".into(),
156+ None,
157+ ui::completers::none,
158+ |_editor: &mut Context, _pattern: &str, _event: PromptEvent| {},
159+ );
137160
138− Self {
139− picker,
140− truncate_start,
161+ let mut picker = Self {
162+ options,
163+ editor_data,
164+ matcher: Box::default(),
165+ matches: Vec::new(),
166+ cursor: 0,
167+ prompt,
168+ previous_pattern: (String::new(), FuzzyQuery::default()),
169+ truncate_start: true,
170+ show_preview: true,
171+ callback_fn: Box::new(callback_fn),
172+ completion_height: 0,
173+ widths: Vec::new(),
141174 preview_cache: HashMap::new(),
142175 read_buffer: Vec::with_capacity(1024),
143176 file_fn: Box::new(preview_fn),
144− }
177+
178+ picker: unimplemented!(),
179+ };
180+
181+ picker.calculate_column_widths();
182+
183+ // scoring on empty input
184+ // TODO: just reuse score()
185+ picker
186+ .matches
187+ .extend(picker.options.iter().enumerate().map(|(index, option)| {
188+ let text = option.filter_text(&picker.editor_data);
189+ PickerMatch {
190+ index,
191+ score: 0,
192+ len: text.chars().count(),
193+ }
194+ }));
195+
196+ picker
145197 }
146198
147199 pub fn truncate_start(mut self, truncate_start: bool) -> Self {
@@ -150,6 +202,38 @@ impl<T: Item + 'static> FilePicker<T> {
150202 self
151203 }
152204
205+ pub fn set_options(&mut self, new_options: Vec<T>) {
206+ self.options = new_options;
207+ self.cursor = 0;
208+ self.force_score();
209+ self.calculate_column_widths();
210+ }
211+
212+ /// Calculate the width constraints using the maximum widths of each column
213+ /// for the current options.
214+ fn calculate_column_widths(&mut self) {
215+ let n = self
216+ .options
217+ .first()
218+ .map(|option| option.format(&self.editor_data).cells.len())
219+ .unwrap_or_default();
220+ let max_lens = self.options.iter().fold(vec![0; n], |mut acc, option| {
221+ let row = option.format(&self.editor_data);
222+ // maintain max for each column
223+ for (acc, cell) in acc.iter_mut().zip(row.cells.iter()) {
224+ let width = cell.content.width();
225+ if width > *acc {
226+ *acc = width;
227+ }
228+ }
229+ acc
230+ });
231+ self.widths = max_lens
232+ .into_iter()
233+ .map(|len| Constraint::Length(len as u16))
234+ .collect();
235+ }
236+
153237 fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
154238 self.picker
155239 .selection()
@@ -477,76 +561,11 @@ impl<T: Item> Picker<T> {
477561 editor_data: T::Data,
478562 callback_fn: impl Fn(&mut Context, &T, Action) + 'static,
479563 ) -> Self {
480− let prompt = Prompt::new(
481− "".into(),
482− None,
483− ui::completers::none,
484− |_editor: &mut Context, _pattern: &str, _event: PromptEvent| {},
485− );
486−
487− let mut picker = Self {
488− options,
489− editor_data,
490− matcher: Box::default(),
491− matches: Vec::new(),
492− cursor: 0,
493− prompt,
494− previous_pattern: (String::new(), FuzzyQuery::default()),
495− truncate_start: true,
496− show_preview: true,
497− callback_fn: Box::new(callback_fn),
498− completion_height: 0,
499− widths: Vec::new(),
500− };
501−
502− picker.calculate_column_widths();
503−
504− // scoring on empty input
505− // TODO: just reuse score()
506− picker
507− .matches
508− .extend(picker.options.iter().enumerate().map(|(index, option)| {
509− let text = option.filter_text(&picker.editor_data);
510− PickerMatch {
511− index,
512− score: 0,
513− len: text.chars().count(),
514− }
515− }));
516−
517− picker
564+ unimplemented!()
518565 }
519566
520567 pub fn set_options(&mut self, new_options: Vec<T>) {
521− self.options = new_options;
522− self.cursor = 0;
523− self.force_score();
524− self.calculate_column_widths();
525− }
526−
527− /// Calculate the width constraints using the maximum widths of each column
528− /// for the current options.
529− fn calculate_column_widths(&mut self) {
530− let n = self
531− .options
532− .first()
533− .map(|option| option.format(&self.editor_data).cells.len())
534− .unwrap_or_default();
535− let max_lens = self.options.iter().fold(vec![0; n], |mut acc, option| {
536− let row = option.format(&self.editor_data);
537− // maintain max for each column
538− for (acc, cell) in acc.iter_mut().zip(row.cells.iter()) {
539− let width = cell.content.width();
540− if width > *acc {
541− *acc = width;
542− }
543− }
544− acc
545− });
546− self.widths = max_lens
547− .into_iter()
548− .map(|len| Constraint::Length(len as u16))
549− .collect();
568+ unimplemented!()
550569 }
551570
552571 pub fn score(&mut self) {