2020-12-17 10:08:16 +01:00
|
|
|
use crate::compositor::{Component, Compositor, Context, EventResult};
|
|
|
|
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
|
|
|
use tui::{
|
2021-03-22 04:40:07 +01:00
|
|
|
buffer::Buffer as Surface,
|
2021-05-09 11:32:40 +02:00
|
|
|
widgets::{Block, BorderType, Borders},
|
2020-12-17 10:08:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
|
|
|
|
use fuzzy_matcher::FuzzyMatcher;
|
|
|
|
|
2020-12-21 09:58:54 +01:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2020-12-17 10:08:16 +01:00
|
|
|
use crate::ui::{Prompt, PromptEvent};
|
|
|
|
use helix_core::Position;
|
2021-06-25 05:58:15 +02:00
|
|
|
use helix_view::{
|
|
|
|
editor::Action,
|
|
|
|
graphics::{Color, CursorKind, Rect, Style},
|
|
|
|
Editor,
|
|
|
|
};
|
2020-12-17 10:08:16 +01:00
|
|
|
|
2020-12-18 11:19:50 +01:00
|
|
|
pub struct Picker<T> {
|
|
|
|
options: Vec<T>,
|
2020-12-17 10:08:16 +01:00
|
|
|
// filter: String,
|
|
|
|
matcher: Box<Matcher>,
|
2020-12-18 08:43:15 +01:00
|
|
|
/// (index, score)
|
|
|
|
matches: Vec<(usize, i64)>,
|
2021-06-12 12:46:05 +02:00
|
|
|
/// Filter over original options.
|
|
|
|
filters: Vec<usize>, // could be optimized into bit but not worth it now
|
2020-12-17 10:08:16 +01:00
|
|
|
|
|
|
|
cursor: usize,
|
|
|
|
// pattern: String,
|
|
|
|
prompt: Prompt,
|
|
|
|
|
2020-12-21 09:58:54 +01:00
|
|
|
format_fn: Box<dyn Fn(&T) -> Cow<str>>,
|
2021-03-29 08:21:48 +02:00
|
|
|
callback_fn: Box<dyn Fn(&mut Editor, &T, Action)>,
|
2020-12-18 11:19:50 +01:00
|
|
|
}
|
2020-12-17 10:08:16 +01:00
|
|
|
|
2020-12-18 11:19:50 +01:00
|
|
|
impl<T> Picker<T> {
|
|
|
|
pub fn new(
|
|
|
|
options: Vec<T>,
|
2020-12-21 09:58:54 +01:00
|
|
|
format_fn: impl Fn(&T) -> Cow<str> + 'static,
|
2021-03-29 08:21:48 +02:00
|
|
|
callback_fn: impl Fn(&mut Editor, &T, Action) + 'static,
|
2020-12-18 11:19:50 +01:00
|
|
|
) -> Self {
|
2020-12-17 10:08:16 +01:00
|
|
|
let prompt = Prompt::new(
|
|
|
|
"".to_string(),
|
|
|
|
|pattern: &str| Vec::new(),
|
2021-06-22 21:49:55 +02:00
|
|
|
|editor: &mut Context, pattern: &str, event: PromptEvent| {
|
2020-12-17 10:08:16 +01:00
|
|
|
//
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-12-18 08:43:15 +01:00
|
|
|
let mut picker = Self {
|
2020-12-18 11:19:50 +01:00
|
|
|
options,
|
2020-12-17 10:08:16 +01:00
|
|
|
matcher: Box::new(Matcher::default()),
|
2020-12-18 08:43:15 +01:00
|
|
|
matches: Vec::new(),
|
2021-06-12 12:46:05 +02:00
|
|
|
filters: Vec::new(),
|
2020-12-17 10:08:16 +01:00
|
|
|
cursor: 0,
|
|
|
|
prompt,
|
2020-12-18 11:19:50 +01:00
|
|
|
format_fn: Box::new(format_fn),
|
|
|
|
callback_fn: Box::new(callback_fn),
|
2020-12-18 08:43:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: scoring on empty input should just use a fastpath
|
|
|
|
picker.score();
|
|
|
|
|
|
|
|
picker
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:43:15 +01:00
|
|
|
pub fn score(&mut self) {
|
|
|
|
// need to borrow via pattern match otherwise it complains about simultaneous borrow
|
|
|
|
let Self {
|
2020-12-18 11:19:50 +01:00
|
|
|
ref mut options,
|
2020-12-18 08:43:15 +01:00
|
|
|
ref mut matcher,
|
|
|
|
ref mut matches,
|
2021-06-12 12:46:05 +02:00
|
|
|
ref filters,
|
2020-12-18 11:19:50 +01:00
|
|
|
ref format_fn,
|
2020-12-18 08:43:15 +01:00
|
|
|
..
|
|
|
|
} = *self;
|
|
|
|
|
|
|
|
let pattern = &self.prompt.line;
|
|
|
|
|
|
|
|
// reuse the matches allocation
|
|
|
|
matches.clear();
|
2020-12-18 11:19:50 +01:00
|
|
|
matches.extend(
|
|
|
|
self.options
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(index, option)| {
|
2021-06-12 12:46:05 +02:00
|
|
|
// filter options first before matching
|
|
|
|
if !filters.is_empty() {
|
|
|
|
filters.binary_search(&index).ok()?;
|
|
|
|
}
|
2020-12-18 11:19:50 +01:00
|
|
|
// TODO: maybe using format_fn isn't the best idea here
|
|
|
|
let text = (format_fn)(option);
|
|
|
|
// TODO: using fuzzy_indices could give us the char idx for match highlighting
|
|
|
|
matcher
|
2020-12-21 09:58:54 +01:00
|
|
|
.fuzzy_match(&text, pattern)
|
2020-12-18 11:19:50 +01:00
|
|
|
.map(|score| (index, score))
|
|
|
|
}),
|
|
|
|
);
|
2020-12-18 08:43:15 +01:00
|
|
|
matches.sort_unstable_by_key(|(_, score)| -score);
|
|
|
|
|
|
|
|
// reset cursor position
|
|
|
|
self.cursor = 0;
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn move_up(&mut self) {
|
|
|
|
self.cursor = self.cursor.saturating_sub(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn move_down(&mut self) {
|
2021-06-06 10:06:47 +02:00
|
|
|
if self.matches.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.cursor < self.matches.len() - 1 {
|
2020-12-17 10:08:16 +01:00
|
|
|
self.cursor += 1;
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 08:43:15 +01:00
|
|
|
|
2020-12-18 11:19:50 +01:00
|
|
|
pub fn selection(&self) -> Option<&T> {
|
2020-12-18 08:43:15 +01:00
|
|
|
self.matches
|
|
|
|
.get(self.cursor)
|
2020-12-18 11:19:50 +01:00
|
|
|
.map(|(index, _score)| &self.options[*index])
|
2020-12-18 08:43:15 +01:00
|
|
|
}
|
2021-06-12 12:46:05 +02:00
|
|
|
|
|
|
|
pub fn save_filter(&mut self) {
|
|
|
|
self.filters.clear();
|
|
|
|
self.filters
|
|
|
|
.extend(self.matches.iter().map(|(index, _)| *index));
|
|
|
|
self.filters.sort_unstable(); // used for binary search later
|
|
|
|
self.prompt.clear();
|
|
|
|
}
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// process:
|
|
|
|
// - read all the files into a list, maxed out at a large value
|
|
|
|
// - on input change:
|
|
|
|
// - score all the names in relation to input
|
|
|
|
|
2021-05-28 17:06:23 +02:00
|
|
|
fn inner_rect(area: Rect) -> Rect {
|
|
|
|
let padding_vertical = area.height * 20 / 100;
|
|
|
|
let padding_horizontal = area.width * 20 / 100;
|
|
|
|
|
2021-05-29 03:37:47 +02:00
|
|
|
Rect::new(
|
2021-05-28 17:06:23 +02:00
|
|
|
area.x + padding_horizontal,
|
|
|
|
area.y + padding_vertical,
|
|
|
|
area.width - padding_horizontal * 2,
|
|
|
|
area.height - padding_vertical * 2,
|
2021-05-29 03:37:47 +02:00
|
|
|
)
|
2021-05-28 17:06:23 +02:00
|
|
|
}
|
|
|
|
|
2021-04-05 11:23:37 +02:00
|
|
|
impl<T: 'static> Component for Picker<T> {
|
2020-12-17 10:08:16 +01:00
|
|
|
fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
|
|
|
|
let key_event = match event {
|
|
|
|
Event::Key(event) => event,
|
|
|
|
Event::Resize(..) => return EventResult::Consumed(None),
|
|
|
|
_ => return EventResult::Ignored,
|
|
|
|
};
|
|
|
|
|
2021-05-09 11:02:31 +02:00
|
|
|
let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor| {
|
|
|
|
// remove the layer
|
|
|
|
compositor.pop();
|
|
|
|
})));
|
2020-12-17 10:08:16 +01:00
|
|
|
|
|
|
|
match key_event {
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Up, ..
|
|
|
|
}
|
|
|
|
| KeyEvent {
|
2021-06-08 20:50:23 +02:00
|
|
|
code: KeyCode::BackTab,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
| KeyEvent {
|
|
|
|
code: KeyCode::Char('p'),
|
2020-12-17 10:08:16 +01:00
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => self.move_up(),
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Down,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
| KeyEvent {
|
2021-06-08 20:50:23 +02:00
|
|
|
code: KeyCode::Tab, ..
|
|
|
|
}
|
|
|
|
| KeyEvent {
|
|
|
|
code: KeyCode::Char('n'),
|
2020-12-17 10:08:16 +01:00
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => self.move_down(),
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Esc, ..
|
2021-06-08 20:50:23 +02:00
|
|
|
}
|
|
|
|
| KeyEvent {
|
|
|
|
code: KeyCode::Char('c'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
2020-12-17 10:08:16 +01:00
|
|
|
} => {
|
|
|
|
return close_fn;
|
2020-12-18 09:16:04 +01:00
|
|
|
}
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Enter,
|
|
|
|
..
|
|
|
|
} => {
|
2020-12-18 11:19:50 +01:00
|
|
|
if let Some(option) = self.selection() {
|
2021-03-29 08:21:48 +02:00
|
|
|
(self.callback_fn)(&mut cx.editor, option, Action::Replace);
|
|
|
|
}
|
|
|
|
return close_fn;
|
|
|
|
}
|
|
|
|
KeyEvent {
|
2021-06-12 12:39:27 +02:00
|
|
|
code: KeyCode::Char('h'),
|
2021-03-29 08:21:48 +02:00
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => {
|
|
|
|
if let Some(option) = self.selection() {
|
2021-04-08 08:52:04 +02:00
|
|
|
(self.callback_fn)(&mut cx.editor, option, Action::HorizontalSplit);
|
2021-03-29 08:21:48 +02:00
|
|
|
}
|
|
|
|
return close_fn;
|
|
|
|
}
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char('v'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => {
|
|
|
|
if let Some(option) = self.selection() {
|
2021-04-08 08:52:04 +02:00
|
|
|
(self.callback_fn)(&mut cx.editor, option, Action::VerticalSplit);
|
2020-12-18 09:16:04 +01:00
|
|
|
}
|
|
|
|
return close_fn;
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
2021-06-12 12:46:05 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char(' '),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => {
|
|
|
|
self.save_filter();
|
|
|
|
}
|
2020-12-18 08:43:15 +01:00
|
|
|
_ => {
|
2021-01-08 08:31:19 +01:00
|
|
|
if let EventResult::Consumed(_) = self.prompt.handle_event(event, cx) {
|
|
|
|
// TODO: recalculate only if pattern changed
|
|
|
|
self.score();
|
2020-12-18 08:43:15 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
|
2021-05-28 17:06:23 +02:00
|
|
|
let area = inner_rect(area);
|
2020-12-17 10:08:16 +01:00
|
|
|
|
|
|
|
// -- Render the frame:
|
|
|
|
|
2020-12-23 08:20:49 +01:00
|
|
|
// clear area
|
2020-12-22 08:48:34 +01:00
|
|
|
let background = cx.editor.theme.get("ui.background");
|
2021-05-09 11:13:50 +02:00
|
|
|
surface.clear_with(area, background);
|
2020-12-17 10:08:16 +01:00
|
|
|
|
|
|
|
use tui::widgets::Widget;
|
|
|
|
// don't like this but the lifetime sucks
|
|
|
|
let block = Block::default().borders(Borders::ALL);
|
|
|
|
|
|
|
|
// calculate the inner area inside the box
|
|
|
|
let inner = block.inner(area);
|
|
|
|
|
|
|
|
block.render(area, surface);
|
|
|
|
|
|
|
|
// -- Render the input bar:
|
|
|
|
|
|
|
|
let area = Rect::new(inner.x + 1, inner.y, inner.width - 1, 1);
|
|
|
|
self.prompt.render(area, surface, cx);
|
|
|
|
|
|
|
|
// -- Separator
|
|
|
|
let style = Style::default().fg(Color::Rgb(90, 89, 119));
|
|
|
|
let symbols = BorderType::line_symbols(BorderType::Plain);
|
|
|
|
for x in inner.left()..inner.right() {
|
|
|
|
surface
|
|
|
|
.get_mut(x, inner.y + 1)
|
|
|
|
.set_symbol(symbols.horizontal)
|
|
|
|
.set_style(style);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Render the contents:
|
|
|
|
|
2021-05-07 10:38:25 +02:00
|
|
|
let style = cx.editor.theme.get("ui.text");
|
2020-12-17 10:08:16 +01:00
|
|
|
let selected = Style::default().fg(Color::Rgb(255, 255, 255));
|
|
|
|
|
|
|
|
let rows = inner.height - 2; // -1 for search bar
|
2021-06-06 10:41:21 +02:00
|
|
|
let offset = self.cursor / (rows as usize) * (rows as usize);
|
2020-12-18 08:43:15 +01:00
|
|
|
|
2021-06-06 10:41:21 +02:00
|
|
|
let files = self.matches.iter().skip(offset).map(|(index, _score)| {
|
2020-12-18 11:19:50 +01:00
|
|
|
(index, self.options.get(*index).unwrap()) // get_unchecked
|
2020-12-18 08:43:15 +01:00
|
|
|
});
|
|
|
|
|
2020-12-18 11:19:50 +01:00
|
|
|
for (i, (_index, option)) in files.take(rows as usize).enumerate() {
|
2021-06-06 10:41:21 +02:00
|
|
|
if i == (self.cursor - offset) {
|
2020-12-17 10:08:16 +01:00
|
|
|
surface.set_string(inner.x + 1, inner.y + 2 + i as u16, ">", selected);
|
|
|
|
}
|
|
|
|
|
2021-06-24 17:54:34 +02:00
|
|
|
surface.set_string_truncated(
|
2020-12-17 10:08:16 +01:00
|
|
|
inner.x + 3,
|
|
|
|
inner.y + 2 + i as u16,
|
2020-12-18 11:19:50 +01:00
|
|
|
(self.format_fn)(option),
|
2021-06-23 17:08:08 +02:00
|
|
|
(inner.width as usize).saturating_sub(3), // account for the " > "
|
2021-06-06 10:41:21 +02:00
|
|
|
if i == (self.cursor - offset) {
|
|
|
|
selected
|
|
|
|
} else {
|
|
|
|
style
|
|
|
|
},
|
2021-06-24 17:54:34 +02:00
|
|
|
true,
|
2020-12-17 10:08:16 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 07:03:56 +02:00
|
|
|
fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) {
|
2021-05-28 17:06:23 +02:00
|
|
|
// TODO: this is mostly duplicate code
|
|
|
|
let area = inner_rect(area);
|
|
|
|
let block = Block::default().borders(Borders::ALL);
|
|
|
|
// calculate the inner area inside the box
|
|
|
|
let inner = block.inner(area);
|
|
|
|
|
|
|
|
// prompt area
|
|
|
|
let area = Rect::new(inner.x + 1, inner.y, inner.width - 1, 1);
|
|
|
|
|
2021-06-15 07:03:56 +02:00
|
|
|
self.prompt.cursor(area, editor)
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
|
|
|
}
|