on master help sign in
Calculate completion popup sizing

Fixes #220

commit5292fe0f7df9f1a420744007aa9dd67e7a5a6610 authorBlaž Hrastnik <blaz@mxxn.io> dateMon 19 Jul 2021, 11:28 (5 years ago) parentbf43fab
2 files changed+33−5
helix-term/src/ui/menu.rs+31−3 helix-tui/src/widgets/table.rs+2−2
helix-term/src/ui/menu.rs+31−3
@@ -8,6 +8,7 @@ use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
88use fuzzy_matcher::FuzzyMatcher;
99
1010use helix_view::{graphics::Rect, Editor};
11+use tui::layout::Constraint;
1112
1213pub trait Item {
1314 // TODO: sort_text
@@ -26,6 +27,8 @@ pub struct Menu<T: Item> {
2627 /// (index, score)
2728 matches: Vec<(usize, i64)>,
2829
30+ widths: Vec<Constraint>,
31+
2932 callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
3033
3134 scroll: usize,
@@ -44,6 +47,7 @@ impl<T: Item> Menu<T> {
4447 matcher: Box::new(Matcher::default()),
4548 matches: Vec::new(),
4649 cursor: None,
50+ widths: Vec::new(),
4751 callback_fn: Box::new(callback_fn),
4852 scroll: 0,
4953 size: (0, 0),
@@ -218,8 +222,33 @@ impl<T: Item + 'static> Component for Menu<T> {
218222 EventResult::Ignored
219223 }
220224
225+ // TODO: completion sorting
226+
221227 fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
222− let width = std::cmp::min(30, viewport.0);
228+ let n = self
229+ .options
230+ .first()
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();
238+ if width > acc[i] {
239+ acc[i] = width;
240+ }
241+ }
242+
243+ acc
244+ });
245+ let len = (max_lens.iter().sum::<usize>()) + n + 1; // +1: reserve some space for scrollbar
246+ let width = len.min(viewport.0 as usize);
247+
248+ self.widths = max_lens
249+ .into_iter()
250+ .map(|len| Constraint::Length(len as u16))
251+ .collect();
223252
224253 const MAX: usize = 10;
225254 let height = std::cmp::min(self.options.len(), MAX);
@@ -263,13 +292,12 @@ impl<T: Item + 'static> Component for Menu<T> {
263292 let scroll_line = (win_height - scroll_height) * scroll
264293 / std::cmp::max(1, len.saturating_sub(win_height));
265294
266− use tui::layout::Constraint;
267295 let rows = options.iter().map(|option| option.row());
268296 let table = Table::new(rows)
269297 .style(style)
270298 .highlight_style(selected)
271299 .column_spacing(1)
272− .widths(&[Constraint::Percentage(50), Constraint::Percentage(50)]);
300+ .widths(&self.widths);
273301
274302 use tui::widgets::TableState;
275303
helix-tui/src/widgets/table.rs+2−2
@@ -36,7 +36,7 @@ use std::collections::HashMap;
3636/// capabilities of [`Text`].
3737#[derive(Debug, Clone, PartialEq, Default)]
3838pub struct Cell<'a> {
39− content: Text<'a>,
39+ pub content: Text<'a>,
4040 style: Style,
4141}
4242
@@ -81,7 +81,7 @@ where
8181/// By default, a row has a height of 1 but you can change this using [`Row::height`].
8282#[derive(Debug, Clone, PartialEq, Default)]
8383pub struct Row<'a> {
84− cells: Vec<Cell<'a>>,
84+ pub cells: Vec<Cell<'a>>,
8585 height: u16,
8686 style: Style,
8787 bottom_margin: u16,