on master help sign in
Calculate completion popup sizing
5292fe0Blaž Hrastnik
helix-term/src/ui/menu.rs+31−3
@@ -8,6 +8,7 @@ use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
8use fuzzy_matcher::FuzzyMatcher;
8use fuzzy_matcher::FuzzyMatcher;
9
9
10use helix_view::{graphics::Rect, Editor};
10use helix_view::{graphics::Rect, Editor};
11+use tui::layout::Constraint;
11
12
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)>,
28
29
30+ widths: Vec<Constraint>,
31+
29 callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
32 callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
30
33
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,
50+ widths: Vec::new(),
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
219 }
223 }
220
224
225+ // TODO: completion sorting
226+
221 fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
227 fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
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
222− let width = std::cmp::min(30, viewport.0);
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();
223
252
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));
265
294
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);
273
301
274 use tui::widgets::TableState;
302 use tui::widgets::TableState;
275
303
helix-tui/src/widgets/table.rs+2−2
@@ -36,7 +36,7 @@ use std::collections::HashMap;
36/// capabilities of [`Text`].
36/// capabilities of [`Text`].
37#[derive(Debug, Clone, PartialEq, Default)]
37#[derive(Debug, Clone, PartialEq, Default)]
38pub struct Cell<'a> {
38pub struct Cell<'a> {
39− content: Text<'a>,
39+ pub content: Text<'a>,
40 style: Style,
40 style: Style,
41}
41}
42
42
@@ -81,7 +81,7 @@ where
81/// By default, a row has a height of 1 but you can change this using [`Row::height`].
81/// By default, a row has a height of 1 but you can change this using [`Row::height`].
82#[derive(Debug, Clone, PartialEq, Default)]
82#[derive(Debug, Clone, PartialEq, Default)]
83pub struct Row<'a> {
83pub struct Row<'a> {
84− cells: Vec<Cell<'a>>,
84+ pub cells: Vec<Cell<'a>>,
85 height: u16,
85 height: u16,
86 style: Style,
86 style: Style,
87 bottom_margin: u16,
87 bottom_margin: u16,