2021-11-10 16:58:46 +01:00
|
|
|
use crate::{
|
|
|
|
compositor::{Component, Compositor, Context, EventResult},
|
2021-11-12 08:21:03 +01:00
|
|
|
ctrl, key, shift,
|
2021-11-10 16:58:46 +01:00
|
|
|
};
|
|
|
|
use crossterm::event::Event;
|
2021-06-25 05:58:15 +02:00
|
|
|
use tui::{buffer::Buffer as Surface, widgets::Table};
|
2021-02-09 07:40:30 +01:00
|
|
|
|
2021-05-22 10:33:42 +02:00
|
|
|
pub use tui::widgets::{Cell, Row};
|
|
|
|
|
2021-03-27 04:06:40 +01:00
|
|
|
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
|
|
|
|
use fuzzy_matcher::FuzzyMatcher;
|
|
|
|
|
2021-07-01 19:41:20 +02:00
|
|
|
use helix_view::{graphics::Rect, Editor};
|
2021-07-19 04:28:29 +02:00
|
|
|
use tui::layout::Constraint;
|
2021-02-09 07:40:30 +01:00
|
|
|
|
2021-05-22 10:33:42 +02:00
|
|
|
pub trait Item {
|
|
|
|
fn label(&self) -> &str;
|
2022-01-31 05:42:32 +01:00
|
|
|
|
|
|
|
fn sort_text(&self) -> &str {
|
|
|
|
self.label()
|
|
|
|
}
|
|
|
|
fn filter_text(&self) -> &str {
|
|
|
|
self.label()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn row(&self) -> Row {
|
|
|
|
Row::new(vec![Cell::from(self.label())])
|
|
|
|
}
|
2021-05-22 10:33:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Menu<T: Item> {
|
2021-02-09 07:40:30 +01:00
|
|
|
options: Vec<T>,
|
|
|
|
|
2021-03-03 09:28:50 +01:00
|
|
|
cursor: Option<usize>,
|
2021-02-09 07:40:30 +01:00
|
|
|
|
2021-03-27 04:06:40 +01:00
|
|
|
matcher: Box<Matcher>,
|
|
|
|
/// (index, score)
|
|
|
|
matches: Vec<(usize, i64)>,
|
|
|
|
|
2021-07-19 04:28:29 +02:00
|
|
|
widths: Vec<Constraint>,
|
|
|
|
|
2021-02-09 07:40:30 +01:00
|
|
|
callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
|
2021-03-03 09:28:50 +01:00
|
|
|
|
2021-03-08 09:13:45 +01:00
|
|
|
scroll: usize,
|
|
|
|
size: (u16, u16),
|
2021-09-09 05:35:14 +02:00
|
|
|
viewport: (u16, u16),
|
|
|
|
recalculate: bool,
|
2021-02-09 07:40:30 +01:00
|
|
|
}
|
|
|
|
|
2021-05-22 10:33:42 +02:00
|
|
|
impl<T: Item> Menu<T> {
|
2021-02-09 07:40:30 +01:00
|
|
|
// TODO: it's like a slimmed down picker, share code? (picker = menu + prompt with different
|
|
|
|
// rendering)
|
|
|
|
pub fn new(
|
|
|
|
options: Vec<T>,
|
|
|
|
callback_fn: impl Fn(&mut Editor, Option<&T>, MenuEvent) + 'static,
|
|
|
|
) -> Self {
|
2021-03-27 04:06:40 +01:00
|
|
|
let mut menu = Self {
|
2021-02-09 07:40:30 +01:00
|
|
|
options,
|
2021-03-27 04:06:40 +01:00
|
|
|
matcher: Box::new(Matcher::default()),
|
|
|
|
matches: Vec::new(),
|
2021-03-03 09:28:50 +01:00
|
|
|
cursor: None,
|
2021-07-19 04:28:29 +02:00
|
|
|
widths: Vec::new(),
|
2021-02-09 07:40:30 +01:00
|
|
|
callback_fn: Box::new(callback_fn),
|
2021-03-08 09:13:45 +01:00
|
|
|
scroll: 0,
|
|
|
|
size: (0, 0),
|
2021-09-09 05:35:14 +02:00
|
|
|
viewport: (0, 0),
|
|
|
|
recalculate: true,
|
2021-03-27 04:06:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: scoring on empty input should just use a fastpath
|
|
|
|
menu.score("");
|
|
|
|
|
|
|
|
menu
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn score(&mut self, pattern: &str) {
|
|
|
|
// reuse the matches allocation
|
2021-10-22 05:07:41 +02:00
|
|
|
self.matches.clear();
|
|
|
|
self.matches.extend(
|
|
|
|
self.options
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(index, option)| {
|
|
|
|
let text = option.filter_text();
|
|
|
|
// TODO: using fuzzy_indices could give us the char idx for match highlighting
|
|
|
|
self.matcher
|
|
|
|
.fuzzy_match(text, pattern)
|
|
|
|
.map(|score| (index, score))
|
|
|
|
}),
|
|
|
|
);
|
2021-08-10 04:19:07 +02:00
|
|
|
// matches.sort_unstable_by_key(|(_, score)| -score);
|
2021-10-22 05:07:41 +02:00
|
|
|
self.matches
|
|
|
|
.sort_unstable_by_key(|(index, _score)| self.options[*index].sort_text());
|
2021-03-27 04:06:40 +01:00
|
|
|
|
|
|
|
// reset cursor position
|
|
|
|
self.cursor = None;
|
|
|
|
self.scroll = 0;
|
2021-09-09 05:35:14 +02:00
|
|
|
self.recalculate = true;
|
2021-02-09 07:40:30 +01:00
|
|
|
}
|
|
|
|
|
2021-10-16 11:43:07 +02:00
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.matches.clear();
|
|
|
|
|
|
|
|
// reset cursor position
|
|
|
|
self.cursor = None;
|
|
|
|
self.scroll = 0;
|
|
|
|
}
|
|
|
|
|
2021-02-09 07:40:30 +01:00
|
|
|
pub fn move_up(&mut self) {
|
2021-08-06 04:22:23 +02:00
|
|
|
let len = self.matches.len();
|
2021-10-26 02:42:23 +02:00
|
|
|
let max_index = len.saturating_sub(1);
|
|
|
|
let pos = self.cursor.map_or(max_index, |i| (i + max_index) % len) % len;
|
2021-03-03 09:28:50 +01:00
|
|
|
self.cursor = Some(pos);
|
2021-03-08 09:13:45 +01:00
|
|
|
self.adjust_scroll();
|
2021-02-09 07:40:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn move_down(&mut self) {
|
2021-08-06 04:22:23 +02:00
|
|
|
let len = self.matches.len();
|
|
|
|
let pos = self.cursor.map_or(0, |i| i + 1) % len;
|
2021-03-03 09:28:50 +01:00
|
|
|
self.cursor = Some(pos);
|
2021-03-08 09:13:45 +01:00
|
|
|
self.adjust_scroll();
|
|
|
|
}
|
|
|
|
|
2021-09-09 05:35:14 +02:00
|
|
|
fn recalculate_size(&mut self, viewport: (u16, u16)) {
|
|
|
|
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 (acc, cell) in acc.iter_mut().zip(row.cells.iter()) {
|
|
|
|
let width = cell.content.width();
|
|
|
|
if width > *acc {
|
|
|
|
*acc = width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acc
|
|
|
|
});
|
2022-01-31 17:25:59 +01:00
|
|
|
|
|
|
|
let height = self.matches.len().min(10).min(viewport.1 as usize);
|
|
|
|
// do all the matches fit on a single screen?
|
|
|
|
let fits = self.matches.len() <= height;
|
|
|
|
|
|
|
|
let mut len = max_lens.iter().sum::<usize>() + n;
|
|
|
|
|
|
|
|
if !fits {
|
|
|
|
len += 1; // +1: reserve some space for scrollbar
|
|
|
|
}
|
|
|
|
|
2021-09-09 05:35:14 +02:00
|
|
|
let width = len.min(viewport.0 as usize);
|
|
|
|
|
|
|
|
self.widths = max_lens
|
|
|
|
.into_iter()
|
|
|
|
.map(|len| Constraint::Length(len as u16))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
self.size = (width as u16, height as u16);
|
|
|
|
|
|
|
|
// adjust scroll offsets if size changed
|
|
|
|
self.adjust_scroll();
|
|
|
|
self.recalculate = false;
|
|
|
|
}
|
|
|
|
|
2021-03-08 09:13:45 +01:00
|
|
|
fn adjust_scroll(&mut self) {
|
|
|
|
let win_height = self.size.1 as usize;
|
|
|
|
if let Some(cursor) = self.cursor {
|
|
|
|
let mut scroll = self.scroll;
|
|
|
|
if cursor > (win_height + scroll).saturating_sub(1) {
|
|
|
|
// scroll down
|
|
|
|
scroll += cursor - (win_height + scroll).saturating_sub(1)
|
|
|
|
} else if cursor < scroll {
|
|
|
|
// scroll up
|
|
|
|
scroll = cursor
|
|
|
|
}
|
|
|
|
self.scroll = scroll;
|
|
|
|
}
|
2021-02-09 07:40:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn selection(&self) -> Option<&T> {
|
2021-03-27 04:06:40 +01:00
|
|
|
self.cursor.and_then(|cursor| {
|
|
|
|
self.matches
|
|
|
|
.get(cursor)
|
|
|
|
.map(|(index, _score)| &self.options[*index])
|
|
|
|
})
|
2021-02-09 07:40:30 +01:00
|
|
|
}
|
2021-04-05 11:23:37 +02:00
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.matches.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.matches.len()
|
|
|
|
}
|
2021-02-09 07:40:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
use super::PromptEvent as MenuEvent;
|
|
|
|
|
2021-05-22 10:33:42 +02:00
|
|
|
impl<T: Item + 'static> Component for Menu<T> {
|
2021-02-09 07:40:30 +01:00
|
|
|
fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
|
|
|
|
let event = match event {
|
|
|
|
Event::Key(event) => event,
|
2022-02-23 04:46:12 +01:00
|
|
|
_ => return EventResult::Ignored(None),
|
2021-02-09 07:40:30 +01:00
|
|
|
};
|
|
|
|
|
2021-12-12 13:16:48 +01:00
|
|
|
let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _| {
|
2021-05-09 11:02:31 +02:00
|
|
|
// remove the layer
|
|
|
|
compositor.pop();
|
|
|
|
})));
|
2021-02-09 07:40:30 +01:00
|
|
|
|
2021-11-10 16:58:46 +01:00
|
|
|
match event.into() {
|
2021-02-09 07:40:30 +01:00
|
|
|
// esc or ctrl-c aborts the completion and closes the menu
|
2021-11-10 16:58:46 +01:00
|
|
|
key!(Esc) | ctrl!('c') => {
|
2021-02-09 07:40:30 +01:00
|
|
|
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Abort);
|
|
|
|
return close_fn;
|
|
|
|
}
|
|
|
|
// arrow up/ctrl-p/shift-tab prev completion choice (including updating the doc)
|
2021-12-06 04:24:25 +01:00
|
|
|
shift!(Tab) | key!(Up) | ctrl!('p') | ctrl!('k') => {
|
2021-02-09 07:40:30 +01:00
|
|
|
self.move_up();
|
|
|
|
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Update);
|
|
|
|
return EventResult::Consumed(None);
|
|
|
|
}
|
2021-11-10 16:58:46 +01:00
|
|
|
key!(Tab) | key!(Down) | ctrl!('n') | ctrl!('j') => {
|
|
|
|
// arrow down/ctrl-n/tab advances completion choice (including updating the doc)
|
2021-02-09 07:40:30 +01:00
|
|
|
self.move_down();
|
|
|
|
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Update);
|
|
|
|
return EventResult::Consumed(None);
|
|
|
|
}
|
2021-11-10 16:58:46 +01:00
|
|
|
key!(Enter) => {
|
2021-03-24 10:15:42 +01:00
|
|
|
if let Some(selection) = self.selection() {
|
|
|
|
(self.callback_fn)(cx.editor, Some(selection), MenuEvent::Validate);
|
|
|
|
}
|
2021-02-09 07:40:30 +01:00
|
|
|
return close_fn;
|
|
|
|
}
|
|
|
|
// KeyEvent {
|
|
|
|
// code: KeyCode::Char(c),
|
|
|
|
// modifiers: KeyModifiers::NONE,
|
|
|
|
// } => {
|
|
|
|
// self.insert_char(c);
|
|
|
|
// (self.callback_fn)(cx.editor, &self.line, MenuEvent::Update);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// / -> edit_filter?
|
|
|
|
//
|
|
|
|
// enter confirms the match and closes the menu
|
|
|
|
// typing filters the menu
|
|
|
|
// if we run out of options the menu closes itself
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
// for some events, we want to process them but send ignore, specifically all input except
|
|
|
|
// tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
|
|
|
|
// EventResult::Consumed(None)
|
2022-02-23 04:46:12 +01:00
|
|
|
EventResult::Ignored(None)
|
2021-02-09 07:40:30 +01:00
|
|
|
}
|
|
|
|
|
2021-03-08 09:00:32 +01:00
|
|
|
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
|
2021-09-09 05:35:14 +02:00
|
|
|
if viewport != self.viewport || self.recalculate {
|
|
|
|
self.recalculate_size(viewport);
|
|
|
|
}
|
2021-03-08 09:13:45 +01:00
|
|
|
|
|
|
|
Some(self.size)
|
2021-03-02 10:24:24 +01:00
|
|
|
}
|
2021-02-09 07:40:30 +01:00
|
|
|
|
2021-08-12 09:00:42 +02:00
|
|
|
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
|
2021-08-28 06:54:24 +02:00
|
|
|
let theme = &cx.editor.theme;
|
|
|
|
let style = theme
|
|
|
|
.try_get("ui.menu")
|
|
|
|
.unwrap_or_else(|| theme.get("ui.text"));
|
|
|
|
let selected = theme.get("ui.menu.selected");
|
2021-02-09 07:40:30 +01:00
|
|
|
|
2021-03-08 09:13:45 +01:00
|
|
|
let scroll = self.scroll;
|
2021-03-27 04:06:40 +01:00
|
|
|
|
|
|
|
let options: Vec<_> = self
|
|
|
|
.matches
|
|
|
|
.iter()
|
|
|
|
.map(|(index, _score)| {
|
|
|
|
// (index, self.options.get(*index).unwrap()) // get_unchecked
|
|
|
|
&self.options[*index] // get_unchecked
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let len = options.len();
|
2021-03-03 09:28:50 +01:00
|
|
|
|
|
|
|
let win_height = area.height as usize;
|
|
|
|
|
2021-05-06 10:20:00 +02:00
|
|
|
const fn div_ceil(a: usize, b: usize) -> usize {
|
2021-03-03 09:28:50 +01:00
|
|
|
(a + b - 1) / a
|
|
|
|
}
|
|
|
|
|
|
|
|
let scroll_height = std::cmp::min(div_ceil(win_height.pow(2), len), win_height as usize);
|
|
|
|
|
|
|
|
let scroll_line = (win_height - scroll_height) * scroll
|
|
|
|
/ std::cmp::max(1, len.saturating_sub(win_height));
|
|
|
|
|
2021-05-22 10:33:42 +02:00
|
|
|
let rows = options.iter().map(|option| option.row());
|
|
|
|
let table = Table::new(rows)
|
|
|
|
.style(style)
|
|
|
|
.highlight_style(selected)
|
|
|
|
.column_spacing(1)
|
2021-07-19 04:28:29 +02:00
|
|
|
.widths(&self.widths);
|
2021-05-22 10:33:42 +02:00
|
|
|
|
|
|
|
use tui::widgets::TableState;
|
|
|
|
|
|
|
|
table.render_table(
|
|
|
|
area,
|
|
|
|
surface,
|
|
|
|
&mut TableState {
|
|
|
|
offset: scroll,
|
|
|
|
selected: self.cursor,
|
|
|
|
},
|
|
|
|
);
|
2021-03-03 09:28:50 +01:00
|
|
|
|
2022-01-31 05:46:25 +01:00
|
|
|
let fits = len <= win_height;
|
|
|
|
|
2021-07-01 20:57:12 +02:00
|
|
|
for (i, _) in (scroll..(scroll + win_height).min(len)).enumerate() {
|
2021-03-03 09:28:50 +01:00
|
|
|
let is_marked = i >= scroll_line && i < scroll_line + scroll_height;
|
|
|
|
|
2022-01-31 05:46:25 +01:00
|
|
|
if !fits && is_marked {
|
2022-01-16 02:55:28 +01:00
|
|
|
let cell = &mut surface[(area.x + area.width - 2, area.y + i as u16)];
|
2022-01-31 05:46:25 +01:00
|
|
|
cell.set_symbol("▐");
|
2021-05-27 17:01:17 +02:00
|
|
|
// cell.set_style(selected);
|
2021-03-03 09:28:50 +01:00
|
|
|
// cell.set_style(if is_marked { selected } else { style });
|
|
|
|
}
|
2021-02-09 07:40:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|