|
|
@ -4,8 +4,11 @@ use tui::{
|
|
|
|
buffer::Buffer as Surface,
|
|
|
|
buffer::Buffer as Surface,
|
|
|
|
layout::Rect,
|
|
|
|
layout::Rect,
|
|
|
|
style::{Color, Style},
|
|
|
|
style::{Color, Style},
|
|
|
|
|
|
|
|
widgets::Table,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub use tui::widgets::{Cell, Row};
|
|
|
|
|
|
|
|
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
|
|
|
|
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
|
|
|
|
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
|
|
|
@ -14,7 +17,15 @@ use fuzzy_matcher::FuzzyMatcher;
|
|
|
|
use helix_core::Position;
|
|
|
|
use helix_core::Position;
|
|
|
|
use helix_view::Editor;
|
|
|
|
use helix_view::Editor;
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Menu<T> {
|
|
|
|
pub trait Item {
|
|
|
|
|
|
|
|
// TODO: sort_text
|
|
|
|
|
|
|
|
fn filter_text(&self) -> &str;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn label(&self) -> &str;
|
|
|
|
|
|
|
|
fn row(&self) -> Row;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Menu<T: Item> {
|
|
|
|
options: Vec<T>,
|
|
|
|
options: Vec<T>,
|
|
|
|
|
|
|
|
|
|
|
|
cursor: Option<usize>,
|
|
|
|
cursor: Option<usize>,
|
|
|
@ -23,19 +34,17 @@ pub struct Menu<T> {
|
|
|
|
/// (index, score)
|
|
|
|
/// (index, score)
|
|
|
|
matches: Vec<(usize, i64)>,
|
|
|
|
matches: Vec<(usize, i64)>,
|
|
|
|
|
|
|
|
|
|
|
|
format_fn: Box<dyn Fn(&T) -> Cow<str>>,
|
|
|
|
|
|
|
|
callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
|
|
|
|
callback_fn: Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>,
|
|
|
|
|
|
|
|
|
|
|
|
scroll: usize,
|
|
|
|
scroll: usize,
|
|
|
|
size: (u16, u16),
|
|
|
|
size: (u16, u16),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<T> Menu<T> {
|
|
|
|
impl<T: Item> Menu<T> {
|
|
|
|
// TODO: it's like a slimmed down picker, share code? (picker = menu + prompt with different
|
|
|
|
// TODO: it's like a slimmed down picker, share code? (picker = menu + prompt with different
|
|
|
|
// rendering)
|
|
|
|
// rendering)
|
|
|
|
pub fn new(
|
|
|
|
pub fn new(
|
|
|
|
options: Vec<T>,
|
|
|
|
options: Vec<T>,
|
|
|
|
format_fn: impl Fn(&T) -> Cow<str> + 'static,
|
|
|
|
|
|
|
|
callback_fn: impl Fn(&mut Editor, Option<&T>, MenuEvent) + 'static,
|
|
|
|
callback_fn: impl Fn(&mut Editor, Option<&T>, MenuEvent) + 'static,
|
|
|
|
) -> Self {
|
|
|
|
) -> Self {
|
|
|
|
let mut menu = Self {
|
|
|
|
let mut menu = Self {
|
|
|
@ -43,7 +52,6 @@ impl<T> Menu<T> {
|
|
|
|
matcher: Box::new(Matcher::default()),
|
|
|
|
matcher: Box::new(Matcher::default()),
|
|
|
|
matches: Vec::new(),
|
|
|
|
matches: Vec::new(),
|
|
|
|
cursor: None,
|
|
|
|
cursor: None,
|
|
|
|
format_fn: Box::new(format_fn),
|
|
|
|
|
|
|
|
callback_fn: Box::new(callback_fn),
|
|
|
|
callback_fn: Box::new(callback_fn),
|
|
|
|
scroll: 0,
|
|
|
|
scroll: 0,
|
|
|
|
size: (0, 0),
|
|
|
|
size: (0, 0),
|
|
|
@ -61,7 +69,6 @@ impl<T> Menu<T> {
|
|
|
|
ref mut options,
|
|
|
|
ref mut options,
|
|
|
|
ref mut matcher,
|
|
|
|
ref mut matcher,
|
|
|
|
ref mut matches,
|
|
|
|
ref mut matches,
|
|
|
|
ref format_fn,
|
|
|
|
|
|
|
|
..
|
|
|
|
..
|
|
|
|
} = *self;
|
|
|
|
} = *self;
|
|
|
|
|
|
|
|
|
|
|
@ -72,8 +79,7 @@ impl<T> Menu<T> {
|
|
|
|
.iter()
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(index, option)| {
|
|
|
|
.filter_map(|(index, option)| {
|
|
|
|
// TODO: maybe using format_fn isn't the best idea here
|
|
|
|
let text = option.filter_text();
|
|
|
|
let text = (format_fn)(option);
|
|
|
|
|
|
|
|
// TODO: using fuzzy_indices could give us the char idx for match highlighting
|
|
|
|
// TODO: using fuzzy_indices could give us the char idx for match highlighting
|
|
|
|
matcher
|
|
|
|
matcher
|
|
|
|
.fuzzy_match(&text, pattern)
|
|
|
|
.fuzzy_match(&text, pattern)
|
|
|
@ -134,7 +140,7 @@ impl<T> Menu<T> {
|
|
|
|
|
|
|
|
|
|
|
|
use super::PromptEvent as MenuEvent;
|
|
|
|
use super::PromptEvent as MenuEvent;
|
|
|
|
|
|
|
|
|
|
|
|
impl<T: 'static> Component for Menu<T> {
|
|
|
|
impl<T: Item + 'static> Component for Menu<T> {
|
|
|
|
fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
|
|
|
|
fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
|
|
|
|
let event = match event {
|
|
|
|
let event = match event {
|
|
|
|
Event::Key(event) => event,
|
|
|
|
Event::Key(event) => event,
|
|
|
@ -264,20 +270,26 @@ impl<T: 'static> Component for Menu<T> {
|
|
|
|
let scroll_line = (win_height - scroll_height) * scroll
|
|
|
|
let scroll_line = (win_height - scroll_height) * scroll
|
|
|
|
/ std::cmp::max(1, len.saturating_sub(win_height));
|
|
|
|
/ std::cmp::max(1, len.saturating_sub(win_height));
|
|
|
|
|
|
|
|
|
|
|
|
for (i, option) in options[scroll..(scroll + win_height).min(len)]
|
|
|
|
use tui::layout::Constraint;
|
|
|
|
.iter()
|
|
|
|
let rows = options.iter().map(|option| option.row());
|
|
|
|
.enumerate()
|
|
|
|
let table = Table::new(rows)
|
|
|
|
{
|
|
|
|
.style(style)
|
|
|
|
let line = Some(i + scroll);
|
|
|
|
.highlight_style(selected)
|
|
|
|
// TODO: set bg for the whole row if selected
|
|
|
|
.column_spacing(1)
|
|
|
|
surface.set_stringn(
|
|
|
|
.widths(&[Constraint::Percentage(50), Constraint::Percentage(50)]);
|
|
|
|
area.x,
|
|
|
|
|
|
|
|
area.y + i as u16,
|
|
|
|
use tui::widgets::TableState;
|
|
|
|
(self.format_fn)(option),
|
|
|
|
|
|
|
|
area.width as usize - 1,
|
|
|
|
table.render_table(
|
|
|
|
if line == self.cursor { selected } else { style },
|
|
|
|
area,
|
|
|
|
|
|
|
|
surface,
|
|
|
|
|
|
|
|
&mut TableState {
|
|
|
|
|
|
|
|
offset: scroll,
|
|
|
|
|
|
|
|
selected: self.cursor,
|
|
|
|
|
|
|
|
},
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i, option) in (scroll..(scroll + win_height).min(len)).enumerate() {
|
|
|
|
let is_marked = i >= scroll_line && i < scroll_line + scroll_height;
|
|
|
|
let is_marked = i >= scroll_line && i < scroll_line + scroll_height;
|
|
|
|
|
|
|
|
|
|
|
|
if is_marked {
|
|
|
|
if is_marked {
|
|
|
|