diff options
author | Nathan Vegdahl | 2021-07-19 05:02:12 +0000 |
---|---|---|
committer | Nathan Vegdahl | 2021-07-19 05:02:12 +0000 |
commit | e462f32723bb61899a390f438d7d856d87fb7614 (patch) | |
tree | e97a9afbf07e2735ba96d62dd5e8f71e1a241fe1 /helix-term/src | |
parent | 6c038bb0151c6aeb43fc94bd2dc3d516a71d346c (diff) | |
parent | 5292fe0f7df9f1a420744007aa9dd67e7a5a6610 (diff) |
Merge branch 'master' into great_line_ending_and_cursor_range_cleanup
Diffstat (limited to 'helix-term/src')
-rw-r--r-- | helix-term/src/commands.rs | 6 | ||||
-rw-r--r-- | helix-term/src/ui/menu.rs | 34 |
2 files changed, 36 insertions, 4 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 1f84db6b..cea5a24e 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -3499,7 +3499,11 @@ fn hover(cx: &mut Context) { // comments fn toggle_comments(cx: &mut Context) { let (view, doc) = current!(cx.editor); - let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id)); + let token = doc + .language_config() + .and_then(|lc| lc.comment_token.as_ref()) + .map(|tc| tc.as_ref()); + let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id), token); doc.apply(&transaction, view.id); doc.append_changes_to_history(view.id); diff --git a/helix-term/src/ui/menu.rs b/helix-term/src/ui/menu.rs index 226c1135..8681e5b1 100644 --- a/helix-term/src/ui/menu.rs +++ b/helix-term/src/ui/menu.rs @@ -8,6 +8,7 @@ use fuzzy_matcher::skim::SkimMatcherV2 as Matcher; use fuzzy_matcher::FuzzyMatcher; use helix_view::{graphics::Rect, Editor}; +use tui::layout::Constraint; pub trait Item { // TODO: sort_text @@ -26,6 +27,8 @@ pub struct Menu<T: Item> { /// (index, score) matches: Vec<(usize, i64)>, + widths: Vec<Constraint>, + callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>, scroll: usize, @@ -44,6 +47,7 @@ impl<T: Item> Menu<T> { matcher: Box::new(Matcher::default()), matches: Vec::new(), cursor: None, + widths: Vec::new(), callback_fn: Box::new(callback_fn), scroll: 0, size: (0, 0), @@ -218,8 +222,33 @@ impl<T: Item + 'static> Component for Menu<T> { EventResult::Ignored } + // TODO: completion sorting + fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { - let width = std::cmp::min(30, viewport.0); + let n = self + .options + .first() + .map(|option| option.row().cells.len()) + .unwrap_or_default(); + let max_lens = self.options.iter().fold(vec![0; n], |mut acc, option| { + let row = option.row(); + // maintain max for each column + for (i, cell) in row.cells.iter().enumerate() { + let width = cell.content.width(); + if width > acc[i] { + acc[i] = width; + } + } + + acc + }); + let len = (max_lens.iter().sum::<usize>()) + n + 1; // +1: reserve some space for scrollbar + let width = len.min(viewport.0 as usize); + + self.widths = max_lens + .into_iter() + .map(|len| Constraint::Length(len as u16)) + .collect(); const MAX: usize = 10; let height = std::cmp::min(self.options.len(), MAX); @@ -263,13 +292,12 @@ impl<T: Item + 'static> Component for Menu<T> { let scroll_line = (win_height - scroll_height) * scroll / std::cmp::max(1, len.saturating_sub(win_height)); - use tui::layout::Constraint; let rows = options.iter().map(|option| option.row()); let table = Table::new(rows) .style(style) .highlight_style(selected) .column_spacing(1) - .widths(&[Constraint::Percentage(50), Constraint::Percentage(50)]); + .widths(&self.widths); use tui::widgets::TableState; |