2021-08-12 09:00:42 +02:00
|
|
|
use crate::{
|
|
|
|
compositor::{Component, Compositor, Context, EventResult},
|
|
|
|
ui::EditorView,
|
|
|
|
};
|
2020-12-17 10:08:16 +01:00
|
|
|
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;
|
2021-08-12 09:00:42 +02:00
|
|
|
use tui::widgets::Widget;
|
2020-12-17 10:08:16 +01:00
|
|
|
|
2021-08-12 09:00:42 +02:00
|
|
|
use std::{borrow::Cow, collections::HashMap, path::PathBuf};
|
2020-12-21 09:58:54 +01:00
|
|
|
|
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,
|
2021-08-21 07:21:20 +02:00
|
|
|
graphics::{Color, CursorKind, Margin, Rect, Style},
|
2021-08-12 09:00:42 +02:00
|
|
|
Document, Editor,
|
2021-06-25 05:58:15 +02:00
|
|
|
};
|
2020-12-17 10:08:16 +01:00
|
|
|
|
2021-08-12 09:00:42 +02:00
|
|
|
pub const MIN_SCREEN_WIDTH_FOR_PREVIEW: u16 = 80;
|
|
|
|
|
|
|
|
/// File path and line number (used to align and highlight a line)
|
2021-08-24 06:21:06 +02:00
|
|
|
type FileLocation = (PathBuf, Option<(usize, usize)>);
|
2021-08-12 09:00:42 +02:00
|
|
|
|
|
|
|
pub struct FilePicker<T> {
|
|
|
|
picker: Picker<T>,
|
|
|
|
/// Caches paths to documents
|
|
|
|
preview_cache: HashMap<PathBuf, Document>,
|
|
|
|
/// Given an item in the picker, return the file path and line number to display.
|
|
|
|
file_fn: Box<dyn Fn(&Editor, &T) -> Option<FileLocation>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> FilePicker<T> {
|
|
|
|
pub fn new(
|
|
|
|
options: Vec<T>,
|
|
|
|
format_fn: impl Fn(&T) -> Cow<str> + 'static,
|
|
|
|
callback_fn: impl Fn(&mut Editor, &T, Action) + 'static,
|
|
|
|
preview_fn: impl Fn(&Editor, &T) -> Option<FileLocation> + 'static,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
picker: Picker::new(false, options, format_fn, callback_fn),
|
|
|
|
preview_cache: HashMap::new(),
|
|
|
|
file_fn: Box::new(preview_fn),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
|
|
|
|
self.picker
|
|
|
|
.selection()
|
|
|
|
.and_then(|current| (self.file_fn)(editor, current))
|
2021-08-25 03:04:05 +02:00
|
|
|
.and_then(|(path, line)| {
|
|
|
|
helix_core::path::get_canonicalized_path(&path)
|
|
|
|
.ok()
|
|
|
|
.zip(Some(line))
|
|
|
|
})
|
2021-08-12 09:00:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn calculate_preview(&mut self, editor: &Editor) {
|
|
|
|
if let Some((path, _line)) = self.current_file(editor) {
|
|
|
|
if !self.preview_cache.contains_key(&path) && editor.document_by_path(&path).is_none() {
|
|
|
|
// TODO: enable syntax highlighting; blocked by async rendering
|
|
|
|
let doc = Document::open(&path, None, Some(&editor.theme), None).unwrap();
|
|
|
|
self.preview_cache.insert(path, doc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: 'static> Component for FilePicker<T> {
|
|
|
|
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
|
|
|
|
// +---------+ +---------+
|
|
|
|
// |prompt | |preview |
|
|
|
|
// +---------+ | |
|
|
|
|
// |picker | | |
|
|
|
|
// | | | |
|
|
|
|
// +---------+ +---------+
|
|
|
|
self.calculate_preview(cx.editor);
|
|
|
|
let render_preview = area.width > MIN_SCREEN_WIDTH_FOR_PREVIEW;
|
|
|
|
let area = inner_rect(area);
|
|
|
|
// -- Render the frame:
|
|
|
|
// clear area
|
|
|
|
let background = cx.editor.theme.get("ui.background");
|
|
|
|
surface.clear_with(area, background);
|
|
|
|
|
|
|
|
let picker_width = if render_preview {
|
|
|
|
area.width / 2
|
|
|
|
} else {
|
|
|
|
area.width
|
|
|
|
};
|
|
|
|
|
2021-08-21 07:21:20 +02:00
|
|
|
let picker_area = area.with_width(picker_width);
|
2021-08-12 09:00:42 +02:00
|
|
|
self.picker.render(picker_area, surface, cx);
|
|
|
|
|
|
|
|
if !render_preview {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-21 07:21:20 +02:00
|
|
|
let preview_area = area.clip_left(picker_width);
|
2021-08-12 09:00:42 +02:00
|
|
|
|
|
|
|
// don't like this but the lifetime sucks
|
|
|
|
let block = Block::default().borders(Borders::ALL);
|
|
|
|
|
|
|
|
// calculate the inner area inside the box
|
2021-08-21 07:21:20 +02:00
|
|
|
let inner = block.inner(preview_area);
|
2021-08-12 09:00:42 +02:00
|
|
|
// 1 column gap on either side
|
2021-08-21 07:21:20 +02:00
|
|
|
let margin = Margin {
|
2021-08-24 06:21:49 +02:00
|
|
|
vertical: 0,
|
|
|
|
horizontal: 1,
|
2021-08-21 07:21:20 +02:00
|
|
|
};
|
|
|
|
let inner = inner.inner(&margin);
|
2021-08-12 09:00:42 +02:00
|
|
|
|
|
|
|
block.render(preview_area, surface);
|
|
|
|
|
2021-08-24 06:21:06 +02:00
|
|
|
if let Some((doc, line)) = self.current_file(cx.editor).and_then(|(path, range)| {
|
2021-08-12 09:00:42 +02:00
|
|
|
cx.editor
|
|
|
|
.document_by_path(&path)
|
|
|
|
.or_else(|| self.preview_cache.get(&path))
|
2021-08-24 06:21:06 +02:00
|
|
|
.zip(Some(range))
|
2021-08-12 09:00:42 +02:00
|
|
|
}) {
|
|
|
|
// align to middle
|
2021-08-24 06:21:06 +02:00
|
|
|
let first_line = line
|
2021-09-08 07:19:25 +02:00
|
|
|
.map(|(start, end)| {
|
|
|
|
let height = end.saturating_sub(start) + 1;
|
|
|
|
let middle = start + (height.saturating_sub(1) / 2);
|
|
|
|
middle.saturating_sub(inner.height as usize / 2).min(start)
|
|
|
|
})
|
|
|
|
.unwrap_or(0);
|
|
|
|
|
2021-08-12 09:00:42 +02:00
|
|
|
let offset = Position::new(first_line, 0);
|
|
|
|
|
|
|
|
let highlights = EditorView::doc_syntax_highlights(
|
|
|
|
doc,
|
|
|
|
offset,
|
|
|
|
area.height,
|
|
|
|
&cx.editor.theme,
|
|
|
|
&cx.editor.syn_loader,
|
|
|
|
);
|
|
|
|
EditorView::render_text_highlights(
|
|
|
|
doc,
|
|
|
|
offset,
|
|
|
|
inner,
|
|
|
|
surface,
|
|
|
|
&cx.editor.theme,
|
|
|
|
highlights,
|
|
|
|
);
|
|
|
|
|
|
|
|
// highlight the line
|
2021-08-24 06:21:06 +02:00
|
|
|
if let Some((start, end)) = line {
|
|
|
|
let offset = start.saturating_sub(first_line) as u16;
|
|
|
|
surface.set_style(
|
|
|
|
Rect::new(
|
|
|
|
inner.x,
|
|
|
|
inner.y + offset,
|
|
|
|
inner.width,
|
|
|
|
(end.saturating_sub(start) as u16 + 1)
|
|
|
|
.min(inner.height.saturating_sub(offset)),
|
|
|
|
),
|
2021-08-24 06:24:36 +02:00
|
|
|
cx.editor
|
|
|
|
.theme
|
|
|
|
.try_get("ui.highlight")
|
|
|
|
.unwrap_or_else(|| cx.editor.theme.get("ui.selection")),
|
2021-08-24 06:21:06 +02:00
|
|
|
);
|
2021-08-12 09:00:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_event(&mut self, event: Event, ctx: &mut Context) -> EventResult {
|
|
|
|
// TODO: keybinds for scrolling preview
|
|
|
|
self.picker.handle_event(event, ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cursor(&self, area: Rect, ctx: &Editor) -> (Option<Position>, CursorKind) {
|
|
|
|
self.picker.cursor(area, ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2021-08-12 09:00:42 +02:00
|
|
|
/// Whether to render in the middle of the area
|
|
|
|
render_centered: bool,
|
2020-12-17 10:08:16 +01:00
|
|
|
|
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(
|
2021-08-12 09:00:42 +02:00
|
|
|
render_centered: bool,
|
2020-12-18 11:19:50 +01:00
|
|
|
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(
|
2021-08-31 11:29:24 +02:00
|
|
|
"".into(),
|
2021-07-24 10:48:45 +02:00
|
|
|
None,
|
2021-07-01 20:57:12 +02:00
|
|
|
|_pattern: &str| Vec::new(),
|
|
|
|
|_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,
|
2021-08-12 09:00:42 +02:00
|
|
|
render_centered,
|
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) {
|
|
|
|
let pattern = &self.prompt.line;
|
|
|
|
|
|
|
|
// reuse the matches allocation
|
2021-10-22 05:07:41 +02:00
|
|
|
self.matches.clear();
|
|
|
|
self.matches.extend(
|
2020-12-18 11:19:50 +01:00
|
|
|
self.options
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(index, option)| {
|
2021-06-12 12:46:05 +02:00
|
|
|
// filter options first before matching
|
2021-10-22 05:07:41 +02:00
|
|
|
if !self.filters.is_empty() {
|
|
|
|
self.filters.binary_search(&index).ok()?;
|
2021-06-12 12:46:05 +02:00
|
|
|
}
|
2020-12-18 11:19:50 +01:00
|
|
|
// TODO: maybe using format_fn isn't the best idea here
|
2021-10-22 05:07:41 +02:00
|
|
|
let text = (self.format_fn)(option);
|
2020-12-18 11:19:50 +01:00
|
|
|
// TODO: using fuzzy_indices could give us the char idx for match highlighting
|
2021-10-22 05:07:41 +02:00
|
|
|
self.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))
|
|
|
|
}),
|
|
|
|
);
|
2021-10-22 05:07:41 +02:00
|
|
|
self.matches.sort_unstable_by_key(|(_, score)| -score);
|
2020-12-18 08:43:15 +01:00
|
|
|
|
|
|
|
// reset cursor position
|
|
|
|
self.cursor = 0;
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn move_up(&mut self) {
|
2021-10-09 13:34:10 +02:00
|
|
|
if self.matches.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-17 07:34:59 +02:00
|
|
|
let len = self.matches.len();
|
|
|
|
let pos = ((self.cursor + len.saturating_sub(1)) % len) % len;
|
|
|
|
self.cursor = pos;
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn move_down(&mut self) {
|
2021-10-09 13:34:10 +02:00
|
|
|
if self.matches.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-17 07:34:59 +02:00
|
|
|
let len = self.matches.len();
|
|
|
|
let pos = (self.cursor + 1) % len;
|
|
|
|
self.cursor = pos;
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
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 {
|
2021-08-21 07:21:20 +02:00
|
|
|
let margin = Margin {
|
|
|
|
vertical: area.height * 10 / 100,
|
|
|
|
horizontal: area.width * 10 / 100,
|
|
|
|
};
|
|
|
|
area.inner(&margin)
|
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
|
2021-07-18 06:24:07 +02:00
|
|
|
compositor.last_picker = compositor.pop();
|
2021-05-09 11:02:31 +02:00
|
|
|
})));
|
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,
|
|
|
|
..
|
|
|
|
}
|
2021-10-19 11:37:38 +02:00
|
|
|
| KeyEvent {
|
|
|
|
code: KeyCode::Char('k'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
}
|
2021-06-08 20:50:23 +02:00
|
|
|
| KeyEvent {
|
|
|
|
code: KeyCode::Char('p'),
|
2020-12-17 10:08:16 +01:00
|
|
|
modifiers: KeyModifiers::CONTROL,
|
2021-08-12 09:00:42 +02:00
|
|
|
} => {
|
|
|
|
self.move_up();
|
|
|
|
}
|
2020-12-17 10:08:16 +01:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Down,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
| KeyEvent {
|
2021-06-08 20:50:23 +02:00
|
|
|
code: KeyCode::Tab, ..
|
|
|
|
}
|
2021-10-19 11:37:38 +02:00
|
|
|
| KeyEvent {
|
|
|
|
code: KeyCode::Char('j'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
}
|
2021-06-08 20:50:23 +02:00
|
|
|
| KeyEvent {
|
|
|
|
code: KeyCode::Char('n'),
|
2020-12-17 10:08:16 +01:00
|
|
|
modifiers: KeyModifiers::CONTROL,
|
2021-08-12 09:00:42 +02:00
|
|
|
} => {
|
|
|
|
self.move_down();
|
|
|
|
}
|
2020-12-17 10:08:16 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:00:42 +02:00
|
|
|
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
|
|
|
|
let area = if self.render_centered {
|
|
|
|
inner_rect(area)
|
|
|
|
} else {
|
|
|
|
area
|
|
|
|
};
|
2020-12-17 10:08:16 +01:00
|
|
|
|
2021-08-13 10:56:37 +02:00
|
|
|
let text_style = cx.editor.theme.get("ui.text");
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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:
|
|
|
|
|
2021-08-21 07:21:20 +02:00
|
|
|
let area = inner.clip_left(1).with_height(1);
|
2021-08-13 11:00:04 +02:00
|
|
|
|
2021-08-13 10:56:37 +02:00
|
|
|
let count = format!("{}/{}", self.matches.len(), self.options.len());
|
|
|
|
surface.set_stringn(
|
|
|
|
(area.x + area.width).saturating_sub(count.len() as u16 + 1),
|
|
|
|
area.y,
|
|
|
|
&count,
|
|
|
|
(count.len()).min(area.width as usize),
|
|
|
|
text_style,
|
|
|
|
);
|
|
|
|
|
2020-12-17 10:08:16 +01:00
|
|
|
self.prompt.render(area, surface, cx);
|
|
|
|
|
|
|
|
// -- Separator
|
2021-08-12 09:00:42 +02:00
|
|
|
let sep_style = Style::default().fg(Color::Rgb(90, 89, 119));
|
|
|
|
let borders = BorderType::line_symbols(BorderType::Plain);
|
2020-12-17 10:08:16 +01:00
|
|
|
for x in inner.left()..inner.right() {
|
|
|
|
surface
|
|
|
|
.get_mut(x, inner.y + 1)
|
2021-08-12 09:00:42 +02:00
|
|
|
.set_symbol(borders.horizontal)
|
|
|
|
.set_style(sep_style);
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Render the contents:
|
2021-08-21 07:21:20 +02:00
|
|
|
// subtract area of prompt from top and current item marker " > " from left
|
|
|
|
let inner = inner.clip_top(2).clip_left(3);
|
2020-12-17 10:08:16 +01:00
|
|
|
|
2021-08-19 18:35:14 +02:00
|
|
|
let selected = cx.editor.theme.get("ui.text.focus");
|
2020-12-17 10:08:16 +01:00
|
|
|
|
2021-08-12 09:00:42 +02:00
|
|
|
let rows = inner.height;
|
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) {
|
2021-08-12 09:00:42 +02:00
|
|
|
surface.set_string(inner.x - 2, inner.y + i as u16, ">", selected);
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
|
|
|
|
2021-06-24 17:54:34 +02:00
|
|
|
surface.set_string_truncated(
|
2021-08-12 09:00:42 +02:00
|
|
|
inner.x,
|
|
|
|
inner.y + i as u16,
|
2020-12-18 11:19:50 +01:00
|
|
|
(self.format_fn)(option),
|
2021-08-12 09:00:42 +02:00
|
|
|
inner.width as usize,
|
2021-06-06 10:41:21 +02:00
|
|
|
if i == (self.cursor - offset) {
|
|
|
|
selected
|
|
|
|
} else {
|
2021-08-13 10:56:37 +02:00
|
|
|
text_style
|
2021-06-06 10:41:21 +02:00
|
|
|
},
|
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
|
2021-08-21 07:21:20 +02:00
|
|
|
let area = inner.clip_left(1).with_height(1);
|
2021-05-28 17:06:23 +02:00
|
|
|
|
2021-06-15 07:03:56 +02:00
|
|
|
self.prompt.cursor(area, editor)
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
|
|
|
}
|