@@ -8,6 +8,7 @@ use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
8use fuzzy_matcher::FuzzyMatcher;
8use fuzzy_matcher::FuzzyMatcher;
10use helix_view::{graphics::Rect, Editor};
10use helix_view::{graphics::Rect, Editor};
11+use tui::layout::Constraint;
12pub trait Item {
13pub trait Item {
13 // TODO: sort_text
14 // TODO: sort_text
@@ -26,6 +27,8 @@ pub struct Menu<T: Item> {
26 /// (index, score)
27 /// (index, score)
27 matches: Vec<(usize, i64)>,
28 matches: Vec<(usize, i64)>,
30+ widths: Vec<Constraint>,
29 callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
32 callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
31 scroll: usize,
34 scroll: usize,
@@ -44,6 +47,7 @@ impl<T: Item> Menu<T> {
44 matcher: Box::new(Matcher::default()),
47 matcher: Box::new(Matcher::default()),
45 matches: Vec::new(),
48 matches: Vec::new(),
46 cursor: None,
49 cursor: None,
47 callback_fn: Box::new(callback_fn),
51 callback_fn: Box::new(callback_fn),
48 scroll: 0,
52 scroll: 0,
49 size: (0, 0),
53 size: (0, 0),
@@ -218,8 +222,33 @@ impl<T: Item + 'static> Component for Menu<T> {
218 EventResult::Ignored
222 EventResult::Ignored
225+ // TODO: completion sorting
221 fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
227 fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
231+ .map(|option| option.row().cells.len())
232+ .unwrap_or_default();
233+ let max_lens = self.options.iter().fold(vec![0; n], |mut acc, option| {
234+ let row = option.row();
235+ // maintain max for each column
236+ for (i, cell) in row.cells.iter().enumerate() {
237+ let width = cell.content.width();
245+ let len = (max_lens.iter().sum::<usize>()) + n + 1; // +1: reserve some space for scrollbar
222− let width = std::cmp::min(30, viewport.0);
246+ let width = len.min(viewport.0 as usize);
248+ self.widths = max_lens
250+ .map(|len| Constraint::Length(len as u16))
224 const MAX: usize = 10;
253 const MAX: usize = 10;
225 let height = std::cmp::min(self.options.len(), MAX);
254 let height = std::cmp::min(self.options.len(), MAX);
@@ -263,13 +292,12 @@ impl<T: Item + 'static> Component for Menu<T> {
263 let scroll_line = (win_height - scroll_height) * scroll
292 let scroll_line = (win_height - scroll_height) * scroll
264 / std::cmp::max(1, len.saturating_sub(win_height));
293 / std::cmp::max(1, len.saturating_sub(win_height));
266− use tui::layout::Constraint;
267 let rows = options.iter().map(|option| option.row());
295 let rows = options.iter().map(|option| option.row());
268 let table = Table::new(rows)
296 let table = Table::new(rows)
269 .style(style)
297 .style(style)
270 .highlight_style(selected)
298 .highlight_style(selected)
271 .column_spacing(1)
299 .column_spacing(1)
272− .widths(&[Constraint::Percentage(50), Constraint::Percentage(50)]);
300+ .widths(&self.widths);
274 use tui::widgets::TableState;
302 use tui::widgets::TableState;