2021-08-12 09:00:42 +02:00
|
|
|
use crate::{
|
2023-01-16 08:18:13 +01:00
|
|
|
alt,
|
2022-08-09 03:31:26 +02:00
|
|
|
compositor::{Component, Compositor, Context, Event, EventResult},
|
2021-11-12 08:21:03 +01:00
|
|
|
ctrl, key, shift,
|
2023-01-31 18:03:19 +01:00
|
|
|
ui::{
|
|
|
|
self,
|
|
|
|
document::{render_document, LineDecoration, LinePos, TextRenderer},
|
|
|
|
fuzzy_match::FuzzyQuery,
|
|
|
|
EditorView,
|
|
|
|
},
|
2021-08-12 09:00:42 +02:00
|
|
|
};
|
2022-07-19 18:19:02 +02:00
|
|
|
use futures_util::future::BoxFuture;
|
2020-12-17 10:08:16 +01:00
|
|
|
use tui::{
|
2021-03-22 04:40:07 +01:00
|
|
|
buffer::Buffer as Surface,
|
2022-07-08 20:46:09 +02:00
|
|
|
layout::Constraint,
|
|
|
|
text::{Span, Spans},
|
|
|
|
widgets::{Block, BorderType, Borders, Cell, Table},
|
2020-12-17 10:08:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
|
2021-08-12 09:00:42 +02:00
|
|
|
use tui::widgets::Widget;
|
2020-12-17 10:08:16 +01:00
|
|
|
|
2022-12-18 13:42:25 +01:00
|
|
|
use std::cmp::{self, Ordering};
|
2022-11-21 02:58:35 +01:00
|
|
|
use std::{collections::HashMap, io::Read, path::PathBuf};
|
2020-12-21 09:58:54 +01:00
|
|
|
|
2020-12-17 10:08:16 +01:00
|
|
|
use crate::ui::{Prompt, PromptEvent};
|
2023-01-31 18:03:19 +01:00
|
|
|
use helix_core::{
|
|
|
|
movement::Direction, text_annotations::TextAnnotations,
|
|
|
|
unicode::segmentation::UnicodeSegmentation, Position,
|
|
|
|
};
|
2021-06-25 05:58:15 +02:00
|
|
|
use helix_view::{
|
|
|
|
editor::Action,
|
2022-05-22 03:24:51 +02:00
|
|
|
graphics::{CursorKind, Margin, Modifier, Rect},
|
2022-07-08 20:46:09 +02:00
|
|
|
theme::Style,
|
2023-01-31 18:03:19 +01:00
|
|
|
view::ViewPosition,
|
2022-11-21 02:58:35 +01:00
|
|
|
Document, DocumentId, Editor,
|
2021-06-25 05:58:15 +02:00
|
|
|
};
|
2020-12-17 10:08:16 +01:00
|
|
|
|
2022-07-19 18:19:02 +02:00
|
|
|
use super::{menu::Item, overlay::Overlay};
|
2022-07-02 13:21:27 +02:00
|
|
|
|
Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker (#1612)
* Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker
* Refactor file picker paging logic
* change key mapping
* Add overlay component
* Use closure instead of margin to calculate size
* Don't wrap file picker in `Overlay` automatically
2022-02-15 02:24:03 +01:00
|
|
|
pub const MIN_AREA_WIDTH_FOR_PREVIEW: u16 = 72;
|
2021-11-04 04:24:52 +01:00
|
|
|
/// Biggest file size to preview in bytes
|
|
|
|
pub const MAX_FILE_SIZE_FOR_PREVIEW: u64 = 10 * 1024 * 1024;
|
2021-08-12 09:00:42 +02:00
|
|
|
|
2022-11-21 02:58:35 +01:00
|
|
|
#[derive(PartialEq, Eq, Hash)]
|
|
|
|
pub enum PathOrId {
|
|
|
|
Id(DocumentId),
|
|
|
|
Path(PathBuf),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PathOrId {
|
|
|
|
fn get_canonicalized(self) -> std::io::Result<Self> {
|
|
|
|
use PathOrId::*;
|
|
|
|
Ok(match self {
|
|
|
|
Path(path) => Path(helix_core::path::get_canonicalized_path(&path)?),
|
|
|
|
Id(id) => Id(id),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PathBuf> for PathOrId {
|
|
|
|
fn from(v: PathBuf) -> Self {
|
|
|
|
Self::Path(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<DocumentId> for PathOrId {
|
|
|
|
fn from(v: DocumentId) -> Self {
|
|
|
|
Self::Id(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 03:24:31 +01:00
|
|
|
type FileCallback<T> = Box<dyn Fn(&Editor, &T) -> Option<FileLocation>>;
|
|
|
|
|
2021-11-04 04:24:52 +01:00
|
|
|
/// File path and range of lines (used to align and highlight lines)
|
2022-11-21 02:58:35 +01:00
|
|
|
pub type FileLocation = (PathOrId, Option<(usize, usize)>);
|
2021-08-12 09:00:42 +02:00
|
|
|
|
2022-07-02 13:21:27 +02:00
|
|
|
pub struct FilePicker<T: Item> {
|
2021-08-12 09:00:42 +02:00
|
|
|
picker: Picker<T>,
|
2021-11-14 16:12:56 +01:00
|
|
|
pub truncate_start: bool,
|
2021-08-12 09:00:42 +02:00
|
|
|
/// Caches paths to documents
|
2021-11-04 04:24:52 +01:00
|
|
|
preview_cache: HashMap<PathBuf, CachedPreview>,
|
|
|
|
read_buffer: Vec<u8>,
|
2021-08-12 09:00:42 +02:00
|
|
|
/// Given an item in the picker, return the file path and line number to display.
|
2023-02-09 03:24:31 +01:00
|
|
|
file_fn: FileCallback<T>,
|
2021-08-12 09:00:42 +02:00
|
|
|
}
|
|
|
|
|
2021-11-04 04:24:52 +01:00
|
|
|
pub enum CachedPreview {
|
2021-12-03 04:48:07 +01:00
|
|
|
Document(Box<Document>),
|
2021-11-04 04:24:52 +01:00
|
|
|
Binary,
|
|
|
|
LargeFile,
|
|
|
|
NotFound,
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't store this enum in the cache so as to avoid lifetime constraints
|
|
|
|
// from borrowing a document already opened in the editor.
|
|
|
|
pub enum Preview<'picker, 'editor> {
|
|
|
|
Cached(&'picker CachedPreview),
|
|
|
|
EditorDocument(&'editor Document),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Preview<'_, '_> {
|
|
|
|
fn document(&self) -> Option<&Document> {
|
|
|
|
match self {
|
|
|
|
Preview::EditorDocument(doc) => Some(doc),
|
|
|
|
Preview::Cached(CachedPreview::Document(doc)) => Some(doc),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Alternate text to show for the preview.
|
|
|
|
fn placeholder(&self) -> &str {
|
|
|
|
match *self {
|
|
|
|
Self::EditorDocument(_) => "<File preview>",
|
|
|
|
Self::Cached(preview) => match preview {
|
|
|
|
CachedPreview::Document(_) => "<File preview>",
|
|
|
|
CachedPreview::Binary => "<Binary file>",
|
|
|
|
CachedPreview::LargeFile => "<File too large to preview>",
|
|
|
|
CachedPreview::NotFound => "<File not found>",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 13:21:27 +02:00
|
|
|
impl<T: Item> FilePicker<T> {
|
2021-08-12 09:00:42 +02:00
|
|
|
pub fn new(
|
|
|
|
options: Vec<T>,
|
2022-07-02 13:21:27 +02:00
|
|
|
editor_data: T::Data,
|
2021-11-07 10:03:04 +01:00
|
|
|
callback_fn: impl Fn(&mut Context, &T, Action) + 'static,
|
2021-08-12 09:00:42 +02:00
|
|
|
preview_fn: impl Fn(&Editor, &T) -> Option<FileLocation> + 'static,
|
|
|
|
) -> Self {
|
2022-03-27 06:59:49 +02:00
|
|
|
let truncate_start = true;
|
2022-07-02 13:21:27 +02:00
|
|
|
let mut picker = Picker::new(options, editor_data, callback_fn);
|
2022-03-27 06:59:49 +02:00
|
|
|
picker.truncate_start = truncate_start;
|
|
|
|
|
2021-08-12 09:00:42 +02:00
|
|
|
Self {
|
2022-03-27 06:59:49 +02:00
|
|
|
picker,
|
|
|
|
truncate_start,
|
2021-08-12 09:00:42 +02:00
|
|
|
preview_cache: HashMap::new(),
|
2021-11-04 04:24:52 +01:00
|
|
|
read_buffer: Vec::with_capacity(1024),
|
2021-08-12 09:00:42 +02:00
|
|
|
file_fn: Box::new(preview_fn),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 06:59:49 +02:00
|
|
|
pub fn truncate_start(mut self, truncate_start: bool) -> Self {
|
|
|
|
self.truncate_start = truncate_start;
|
|
|
|
self.picker.truncate_start = truncate_start;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:00:42 +02:00
|
|
|
fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
|
|
|
|
self.picker
|
|
|
|
.selection()
|
|
|
|
.and_then(|current| (self.file_fn)(editor, current))
|
2022-11-21 02:58:35 +01:00
|
|
|
.and_then(|(path_or_id, line)| path_or_id.get_canonicalized().ok().zip(Some(line)))
|
2021-08-12 09:00:42 +02:00
|
|
|
}
|
|
|
|
|
2021-11-04 04:24:52 +01:00
|
|
|
/// Get (cached) preview for a given path. If a document corresponding
|
|
|
|
/// to the path is already open in the editor, it is used instead.
|
|
|
|
fn get_preview<'picker, 'editor>(
|
|
|
|
&'picker mut self,
|
2022-11-21 02:58:35 +01:00
|
|
|
path_or_id: PathOrId,
|
2021-11-04 04:24:52 +01:00
|
|
|
editor: &'editor Editor,
|
|
|
|
) -> Preview<'picker, 'editor> {
|
2022-11-21 02:58:35 +01:00
|
|
|
match path_or_id {
|
|
|
|
PathOrId::Path(path) => {
|
|
|
|
let path = &path;
|
|
|
|
if let Some(doc) = editor.document_by_path(path) {
|
|
|
|
return Preview::EditorDocument(doc);
|
|
|
|
}
|
2021-11-04 04:24:52 +01:00
|
|
|
|
2022-11-21 02:58:35 +01:00
|
|
|
if self.preview_cache.contains_key(path) {
|
|
|
|
return Preview::Cached(&self.preview_cache[path]);
|
|
|
|
}
|
2021-11-04 04:24:52 +01:00
|
|
|
|
2022-11-21 02:58:35 +01:00
|
|
|
let data = std::fs::File::open(path).and_then(|file| {
|
|
|
|
let metadata = file.metadata()?;
|
|
|
|
// Read up to 1kb to detect the content type
|
|
|
|
let n = file.take(1024).read_to_end(&mut self.read_buffer)?;
|
|
|
|
let content_type = content_inspector::inspect(&self.read_buffer[..n]);
|
|
|
|
self.read_buffer.clear();
|
|
|
|
Ok((metadata, content_type))
|
|
|
|
});
|
|
|
|
let preview = data
|
|
|
|
.map(
|
|
|
|
|(metadata, content_type)| match (metadata.len(), content_type) {
|
|
|
|
(_, content_inspector::ContentType::BINARY) => CachedPreview::Binary,
|
|
|
|
(size, _) if size > MAX_FILE_SIZE_FOR_PREVIEW => {
|
|
|
|
CachedPreview::LargeFile
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// TODO: enable syntax highlighting; blocked by async rendering
|
2023-01-31 18:03:19 +01:00
|
|
|
Document::open(path, None, None, editor.config.clone())
|
2022-11-21 02:58:35 +01:00
|
|
|
.map(|doc| CachedPreview::Document(Box::new(doc)))
|
|
|
|
.unwrap_or(CachedPreview::NotFound)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap_or(CachedPreview::NotFound);
|
|
|
|
self.preview_cache.insert(path.to_owned(), preview);
|
|
|
|
Preview::Cached(&self.preview_cache[path])
|
|
|
|
}
|
|
|
|
PathOrId::Id(id) => {
|
|
|
|
let doc = editor.documents.get(&id).unwrap();
|
|
|
|
Preview::EditorDocument(doc)
|
|
|
|
}
|
|
|
|
}
|
2021-08-12 09:00:42 +02:00
|
|
|
}
|
2022-10-11 02:53:55 +02:00
|
|
|
|
|
|
|
fn handle_idle_timeout(&mut self, cx: &mut Context) -> EventResult {
|
|
|
|
// Try to find a document in the cache
|
|
|
|
let doc = self
|
|
|
|
.current_file(cx.editor)
|
2022-11-21 02:58:35 +01:00
|
|
|
.and_then(|(path, _range)| match path {
|
|
|
|
PathOrId::Id(doc_id) => Some(doc_mut!(cx.editor, &doc_id)),
|
|
|
|
PathOrId::Path(path) => match self.preview_cache.get_mut(&path) {
|
|
|
|
Some(CachedPreview::Document(doc)) => Some(doc),
|
|
|
|
_ => None,
|
|
|
|
},
|
2022-10-11 02:53:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Then attempt to highlight it if it has no language set
|
|
|
|
if let Some(doc) = doc {
|
|
|
|
if doc.language_config().is_none() {
|
|
|
|
let loader = cx.editor.syn_loader.clone();
|
|
|
|
doc.detect_language(loader);
|
|
|
|
}
|
2023-03-11 03:32:14 +01:00
|
|
|
|
|
|
|
// QUESTION: do we want to compute inlay hints in pickers too ? Probably not for now
|
|
|
|
// but it could be interesting in the future
|
2022-10-11 02:53:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
2021-08-12 09:00:42 +02:00
|
|
|
}
|
|
|
|
|
2022-07-02 13:21:27 +02:00
|
|
|
impl<T: Item + 'static> Component for FilePicker<T> {
|
2021-08-12 09:00:42 +02:00
|
|
|
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
|
|
|
|
// +---------+ +---------+
|
|
|
|
// |prompt | |preview |
|
|
|
|
// +---------+ | |
|
|
|
|
// |picker | | |
|
|
|
|
// | | | |
|
|
|
|
// +---------+ +---------+
|
2022-01-16 02:55:28 +01:00
|
|
|
|
2022-07-18 03:11:25 +02:00
|
|
|
let render_preview = self.picker.show_preview && area.width > MIN_AREA_WIDTH_FOR_PREVIEW;
|
2021-08-12 09:00:42 +02:00
|
|
|
// -- Render the frame:
|
|
|
|
// clear area
|
|
|
|
let background = cx.editor.theme.get("ui.background");
|
2021-11-04 04:24:52 +01:00
|
|
|
let text = cx.editor.theme.get("ui.text");
|
2021-08-12 09:00:42 +02:00
|
|
|
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
|
2022-06-21 18:52:08 +02:00
|
|
|
let margin = Margin::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-11-04 04:24:52 +01:00
|
|
|
if let Some((path, range)) = self.current_file(cx.editor) {
|
2022-11-21 02:58:35 +01:00
|
|
|
let preview = self.get_preview(path, cx.editor);
|
2021-11-04 04:24:52 +01:00
|
|
|
let doc = match preview.document() {
|
|
|
|
Some(doc) => doc,
|
|
|
|
None => {
|
|
|
|
let alt_text = preview.placeholder();
|
|
|
|
let x = inner.x + inner.width.saturating_sub(alt_text.len() as u16) / 2;
|
|
|
|
let y = inner.y + inner.height / 2;
|
|
|
|
surface.set_stringn(x, y, alt_text, inner.width as usize, text);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-12 09:00:42 +02:00
|
|
|
// align to middle
|
2021-11-04 04:24:52 +01:00
|
|
|
let first_line = range
|
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);
|
|
|
|
|
2023-01-31 18:03:19 +01:00
|
|
|
let offset = ViewPosition {
|
|
|
|
anchor: doc.text().line_to_char(first_line),
|
|
|
|
horizontal_offset: 0,
|
|
|
|
vertical_offset: 0,
|
|
|
|
};
|
2021-08-12 09:00:42 +02:00
|
|
|
|
2023-01-31 18:03:19 +01:00
|
|
|
let mut highlights = EditorView::doc_syntax_highlights(
|
|
|
|
doc,
|
|
|
|
offset.anchor,
|
|
|
|
area.height,
|
|
|
|
&cx.editor.theme,
|
|
|
|
);
|
2022-10-25 14:03:35 +02:00
|
|
|
for spans in EditorView::doc_diagnostics_highlights(doc, &cx.editor.theme) {
|
|
|
|
if spans.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
highlights = Box::new(helix_core::syntax::merge(highlights, spans));
|
|
|
|
}
|
2023-01-31 18:03:19 +01:00
|
|
|
let mut decorations: Vec<Box<dyn LineDecoration>> = Vec::new();
|
|
|
|
|
|
|
|
if let Some((start, end)) = range {
|
|
|
|
let style = cx
|
|
|
|
.editor
|
|
|
|
.theme
|
|
|
|
.try_get("ui.highlight")
|
|
|
|
.unwrap_or_else(|| cx.editor.theme.get("ui.selection"));
|
|
|
|
let draw_highlight = move |renderer: &mut TextRenderer, pos: LinePos| {
|
|
|
|
if (start..=end).contains(&pos.doc_line) {
|
|
|
|
let area = Rect::new(
|
|
|
|
renderer.viewport.x,
|
|
|
|
renderer.viewport.y + pos.visual_line,
|
|
|
|
renderer.viewport.width,
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
renderer.surface.set_style(area, style)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
decorations.push(Box::new(draw_highlight))
|
|
|
|
}
|
|
|
|
|
|
|
|
render_document(
|
|
|
|
surface,
|
|
|
|
inner,
|
2021-08-12 09:00:42 +02:00
|
|
|
doc,
|
|
|
|
offset,
|
2023-03-11 03:32:14 +01:00
|
|
|
// TODO: compute text annotations asynchronously here (like inlay hints)
|
2023-01-31 18:03:19 +01:00
|
|
|
&TextAnnotations::default(),
|
2021-08-12 09:00:42 +02:00
|
|
|
highlights,
|
2023-01-31 18:03:19 +01:00
|
|
|
&cx.editor.theme,
|
|
|
|
&mut decorations,
|
|
|
|
&mut [],
|
2021-08-12 09:00:42 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 02:48:49 +02:00
|
|
|
fn handle_event(&mut self, event: &Event, ctx: &mut Context) -> EventResult {
|
2022-10-11 02:53:55 +02:00
|
|
|
if let Event::IdleTimeout = event {
|
|
|
|
return self.handle_idle_timeout(ctx);
|
|
|
|
}
|
2021-08-12 09:00:42 +02:00
|
|
|
// 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)
|
|
|
|
}
|
Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker (#1612)
* Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker
* Refactor file picker paging logic
* change key mapping
* Add overlay component
* Use closure instead of margin to calculate size
* Don't wrap file picker in `Overlay` automatically
2022-02-15 02:24:03 +01:00
|
|
|
|
|
|
|
fn required_size(&mut self, (width, height): (u16, u16)) -> Option<(u16, u16)> {
|
|
|
|
let picker_width = if width > MIN_AREA_WIDTH_FOR_PREVIEW {
|
|
|
|
width / 2
|
|
|
|
} else {
|
|
|
|
width
|
|
|
|
};
|
|
|
|
self.picker.required_size((picker_width, height))?;
|
|
|
|
Some((width, height))
|
|
|
|
}
|
2021-08-12 09:00:42 +02:00
|
|
|
}
|
|
|
|
|
2022-11-17 00:28:20 +01:00
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
|
|
|
struct PickerMatch {
|
|
|
|
score: i64,
|
2022-12-17 20:30:43 +01:00
|
|
|
index: usize,
|
2022-11-17 00:28:20 +01:00
|
|
|
len: usize,
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:30:43 +01:00
|
|
|
impl PickerMatch {
|
|
|
|
fn key(&self) -> impl Ord {
|
|
|
|
(cmp::Reverse(self.score), self.len, self.index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 00:28:20 +01:00
|
|
|
impl PartialOrd for PickerMatch {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for PickerMatch {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
2022-12-17 20:30:43 +01:00
|
|
|
self.key().cmp(&other.key())
|
2022-11-17 00:28:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 03:24:31 +01:00
|
|
|
type PickerCallback<T> = Box<dyn Fn(&mut Context, &T, Action)>;
|
|
|
|
|
2022-07-02 13:21:27 +02:00
|
|
|
pub struct Picker<T: Item> {
|
2020-12-18 11:19:50 +01:00
|
|
|
options: Vec<T>,
|
2022-07-02 13:21:27 +02:00
|
|
|
editor_data: T::Data,
|
2020-12-17 10:08:16 +01:00
|
|
|
// filter: String,
|
|
|
|
matcher: Box<Matcher>,
|
2022-11-17 00:28:20 +01:00
|
|
|
matches: Vec<PickerMatch>,
|
2020-12-17 10:08:16 +01:00
|
|
|
|
Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker (#1612)
* Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker
* Refactor file picker paging logic
* change key mapping
* Add overlay component
* Use closure instead of margin to calculate size
* Don't wrap file picker in `Overlay` automatically
2022-02-15 02:24:03 +01:00
|
|
|
/// Current height of the completions box
|
|
|
|
completion_height: u16,
|
|
|
|
|
2020-12-17 10:08:16 +01:00
|
|
|
cursor: usize,
|
|
|
|
// pattern: String,
|
|
|
|
prompt: Prompt,
|
2023-02-02 20:48:16 +01:00
|
|
|
previous_pattern: (String, FuzzyQuery),
|
2022-02-28 10:15:34 +01:00
|
|
|
/// Whether to truncate the start (default true)
|
2021-11-14 16:12:56 +01:00
|
|
|
pub truncate_start: bool,
|
2022-07-18 03:11:25 +02:00
|
|
|
/// Whether to show the preview panel (default true)
|
|
|
|
show_preview: bool,
|
2022-07-08 20:46:09 +02:00
|
|
|
/// Constraints for tabular formatting
|
|
|
|
widths: Vec<Constraint>,
|
2020-12-17 10:08:16 +01:00
|
|
|
|
2023-02-09 03:24:31 +01:00
|
|
|
callback_fn: PickerCallback<T>,
|
2020-12-18 11:19:50 +01:00
|
|
|
}
|
2020-12-17 10:08:16 +01:00
|
|
|
|
2022-07-02 13:21:27 +02:00
|
|
|
impl<T: Item> Picker<T> {
|
2020-12-18 11:19:50 +01:00
|
|
|
pub fn new(
|
|
|
|
options: Vec<T>,
|
2022-07-02 13:21:27 +02:00
|
|
|
editor_data: T::Data,
|
2021-11-07 10:03:04 +01:00
|
|
|
callback_fn: impl Fn(&mut Context, &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,
|
2022-02-17 05:55:46 +01:00
|
|
|
ui::completers::none,
|
2022-02-28 10:15:34 +01: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,
|
2022-07-02 13:21:27 +02:00
|
|
|
editor_data,
|
2023-01-27 16:43:46 +01:00
|
|
|
matcher: Box::default(),
|
2020-12-18 08:43:15 +01:00
|
|
|
matches: Vec::new(),
|
2020-12-17 10:08:16 +01:00
|
|
|
cursor: 0,
|
|
|
|
prompt,
|
2023-02-02 20:48:16 +01:00
|
|
|
previous_pattern: (String::new(), FuzzyQuery::default()),
|
2021-11-14 16:12:56 +01:00
|
|
|
truncate_start: true,
|
2022-07-18 03:11:25 +02:00
|
|
|
show_preview: true,
|
2020-12-18 11:19:50 +01:00
|
|
|
callback_fn: Box::new(callback_fn),
|
Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker (#1612)
* Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker
* Refactor file picker paging logic
* change key mapping
* Add overlay component
* Use closure instead of margin to calculate size
* Don't wrap file picker in `Overlay` automatically
2022-02-15 02:24:03 +01:00
|
|
|
completion_height: 0,
|
2023-03-08 02:51:52 +01:00
|
|
|
widths: Vec::new(),
|
2020-12-18 08:43:15 +01:00
|
|
|
};
|
|
|
|
|
2023-03-08 02:51:52 +01:00
|
|
|
picker.calculate_column_widths();
|
|
|
|
|
|
|
|
// scoring on empty input
|
2022-02-28 10:15:34 +01:00
|
|
|
// TODO: just reuse score()
|
2022-11-17 00:28:20 +01:00
|
|
|
picker
|
|
|
|
.matches
|
|
|
|
.extend(picker.options.iter().enumerate().map(|(index, option)| {
|
|
|
|
let text = option.filter_text(&picker.editor_data);
|
|
|
|
PickerMatch {
|
|
|
|
index,
|
|
|
|
score: 0,
|
|
|
|
len: text.chars().count(),
|
|
|
|
}
|
|
|
|
}));
|
2020-12-18 08:43:15 +01:00
|
|
|
|
|
|
|
picker
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 02:51:52 +01:00
|
|
|
pub fn set_options(&mut self, new_options: Vec<T>) {
|
|
|
|
self.options = new_options;
|
|
|
|
self.cursor = 0;
|
|
|
|
self.force_score();
|
|
|
|
self.calculate_column_widths();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculate the width constraints using the maximum widths of each column
|
|
|
|
/// for the current options.
|
|
|
|
fn calculate_column_widths(&mut self) {
|
|
|
|
let n = self
|
|
|
|
.options
|
|
|
|
.first()
|
|
|
|
.map(|option| option.format(&self.editor_data).cells.len())
|
|
|
|
.unwrap_or_default();
|
|
|
|
let max_lens = self.options.iter().fold(vec![0; n], |mut acc, option| {
|
|
|
|
let row = option.format(&self.editor_data);
|
|
|
|
// 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
|
|
|
|
});
|
|
|
|
self.widths = max_lens
|
|
|
|
.into_iter()
|
|
|
|
.map(|len| Constraint::Length(len as u16))
|
|
|
|
.collect();
|
|
|
|
}
|
|
|
|
|
2020-12-18 08:43:15 +01:00
|
|
|
pub fn score(&mut self) {
|
2022-03-23 02:39:24 +01:00
|
|
|
let pattern = self.prompt.line();
|
2020-12-18 08:43:15 +01:00
|
|
|
|
2023-02-02 20:48:16 +01:00
|
|
|
if pattern == &self.previous_pattern.0 {
|
2022-02-28 10:15:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-02 20:48:16 +01:00
|
|
|
let (query, is_refined) = self
|
|
|
|
.previous_pattern
|
|
|
|
.1
|
|
|
|
.refine(pattern, &self.previous_pattern.0);
|
|
|
|
|
2022-02-28 10:15:34 +01:00
|
|
|
if pattern.is_empty() {
|
|
|
|
// Fast path for no pattern.
|
|
|
|
self.matches.clear();
|
2022-11-17 00:28:20 +01:00
|
|
|
self.matches
|
|
|
|
.extend(self.options.iter().enumerate().map(|(index, option)| {
|
|
|
|
let text = option.filter_text(&self.editor_data);
|
|
|
|
PickerMatch {
|
|
|
|
index,
|
|
|
|
score: 0,
|
|
|
|
len: text.chars().count(),
|
|
|
|
}
|
|
|
|
}));
|
2023-02-02 20:48:16 +01:00
|
|
|
} else if is_refined {
|
2022-02-28 10:15:34 +01:00
|
|
|
// optimization: if the pattern is a more specific version of the previous one
|
|
|
|
// then we can score the filtered set.
|
2022-11-17 00:28:20 +01:00
|
|
|
self.matches.retain_mut(|pmatch| {
|
|
|
|
let option = &self.options[pmatch.index];
|
2022-07-02 13:21:27 +02:00
|
|
|
let text = option.sort_text(&self.editor_data);
|
|
|
|
|
2022-09-25 23:43:24 +02:00
|
|
|
match query.fuzzy_match(&text, &self.matcher) {
|
2022-02-28 10:15:34 +01:00
|
|
|
Some(s) => {
|
|
|
|
// Update the score
|
2022-11-17 00:28:20 +01:00
|
|
|
pmatch.score = s;
|
2022-02-28 10:15:34 +01:00
|
|
|
true
|
2021-06-12 12:46:05 +02:00
|
|
|
}
|
2022-02-28 10:15:34 +01:00
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-17 00:28:20 +01:00
|
|
|
self.matches.sort_unstable();
|
2022-02-28 10:15:34 +01:00
|
|
|
} else {
|
2022-07-19 17:58:14 +02:00
|
|
|
self.force_score();
|
2022-02-28 10:15:34 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:43:15 +01:00
|
|
|
// reset cursor position
|
|
|
|
self.cursor = 0;
|
2022-07-19 17:58:14 +02:00
|
|
|
let pattern = self.prompt.line();
|
2023-02-02 20:48:16 +01:00
|
|
|
self.previous_pattern.0.clone_from(pattern);
|
|
|
|
self.previous_pattern.1 = query;
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 17:58:14 +02:00
|
|
|
pub fn force_score(&mut self) {
|
|
|
|
let pattern = self.prompt.line();
|
|
|
|
|
|
|
|
let query = FuzzyQuery::new(pattern);
|
|
|
|
self.matches.clear();
|
|
|
|
self.matches.extend(
|
|
|
|
self.options
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(index, option)| {
|
|
|
|
let text = option.filter_text(&self.editor_data);
|
|
|
|
|
|
|
|
query
|
|
|
|
.fuzzy_match(&text, &self.matcher)
|
|
|
|
.map(|score| PickerMatch {
|
|
|
|
index,
|
|
|
|
score,
|
|
|
|
len: text.chars().count(),
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
);
|
2022-12-17 20:30:43 +01:00
|
|
|
|
2022-07-19 17:58:14 +02:00
|
|
|
self.matches.sort_unstable();
|
|
|
|
}
|
|
|
|
|
Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker (#1612)
* Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker
* Refactor file picker paging logic
* change key mapping
* Add overlay component
* Use closure instead of margin to calculate size
* Don't wrap file picker in `Overlay` automatically
2022-02-15 02:24:03 +01:00
|
|
|
/// Move the cursor by a number of lines, either down (`Forward`) or up (`Backward`)
|
|
|
|
pub fn move_by(&mut self, amount: usize, direction: Direction) {
|
2021-09-17 07:34:59 +02:00
|
|
|
let len = self.matches.len();
|
2020-12-17 10:08:16 +01:00
|
|
|
|
2022-03-14 03:46:23 +01:00
|
|
|
if len == 0 {
|
|
|
|
// No results, can't move.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker (#1612)
* Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker
* Refactor file picker paging logic
* change key mapping
* Add overlay component
* Use closure instead of margin to calculate size
* Don't wrap file picker in `Overlay` automatically
2022-02-15 02:24:03 +01:00
|
|
|
match direction {
|
|
|
|
Direction::Forward => {
|
|
|
|
self.cursor = self.cursor.saturating_add(amount) % len;
|
|
|
|
}
|
|
|
|
Direction::Backward => {
|
|
|
|
self.cursor = self.cursor.saturating_add(len).saturating_sub(amount) % len;
|
|
|
|
}
|
2021-10-09 13:34:10 +02:00
|
|
|
}
|
Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker (#1612)
* Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker
* Refactor file picker paging logic
* change key mapping
* Add overlay component
* Use closure instead of margin to calculate size
* Don't wrap file picker in `Overlay` automatically
2022-02-15 02:24:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Move the cursor down by exactly one page. After the last page comes the first page.
|
|
|
|
pub fn page_up(&mut self) {
|
|
|
|
self.move_by(self.completion_height as usize, Direction::Backward);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Move the cursor up by exactly one page. After the first page comes the last page.
|
|
|
|
pub fn page_down(&mut self) {
|
|
|
|
self.move_by(self.completion_height as usize, Direction::Forward);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Move the cursor to the first entry
|
|
|
|
pub fn to_start(&mut self) {
|
|
|
|
self.cursor = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Move the cursor to the last entry
|
|
|
|
pub fn to_end(&mut self) {
|
|
|
|
self.cursor = self.matches.len().saturating_sub(1);
|
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)
|
2022-11-17 00:28:20 +01:00
|
|
|
.map(|pmatch| &self.options[pmatch.index])
|
2020-12-18 08:43:15 +01:00
|
|
|
}
|
2021-06-12 12:46:05 +02:00
|
|
|
|
2022-07-18 03:11:25 +02:00
|
|
|
pub fn toggle_preview(&mut self) {
|
|
|
|
self.show_preview = !self.show_preview;
|
|
|
|
}
|
2022-08-29 02:48:49 +02:00
|
|
|
|
|
|
|
fn prompt_handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
|
|
|
|
if let EventResult::Consumed(_) = self.prompt.handle_event(event, cx) {
|
|
|
|
// TODO: recalculate only if pattern changed
|
|
|
|
self.score();
|
|
|
|
}
|
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
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
|
|
|
|
|
2022-07-02 13:21:27 +02:00
|
|
|
impl<T: Item + 'static> Component for Picker<T> {
|
2022-01-31 04:36:36 +01:00
|
|
|
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
|
Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker (#1612)
* Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker
* Refactor file picker paging logic
* change key mapping
* Add overlay component
* Use closure instead of margin to calculate size
* Don't wrap file picker in `Overlay` automatically
2022-02-15 02:24:03 +01:00
|
|
|
self.completion_height = viewport.1.saturating_sub(4);
|
|
|
|
Some(viewport)
|
2022-01-31 04:36:36 +01:00
|
|
|
}
|
|
|
|
|
2022-08-29 02:48:49 +02:00
|
|
|
fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
|
2020-12-17 10:08:16 +01:00
|
|
|
let key_event = match event {
|
2022-08-29 02:48:49 +02:00
|
|
|
Event::Key(event) => *event,
|
|
|
|
Event::Paste(..) => return self.prompt_handle_event(event, cx),
|
2020-12-17 10:08:16 +01:00
|
|
|
Event::Resize(..) => return EventResult::Consumed(None),
|
2022-02-23 04:46:12 +01:00
|
|
|
_ => return EventResult::Ignored(None),
|
2020-12-17 10:08:16 +01:00
|
|
|
};
|
|
|
|
|
2022-07-05 12:44:16 +02:00
|
|
|
let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _cx| {
|
2021-05-09 11:02:31 +02:00
|
|
|
// 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
|
|
|
|
2022-10-11 02:53:55 +02:00
|
|
|
// So that idle timeout retriggers
|
|
|
|
cx.editor.reset_idle_timer();
|
|
|
|
|
2022-08-09 03:31:26 +02:00
|
|
|
match key_event {
|
2022-03-31 09:51:11 +02:00
|
|
|
shift!(Tab) | key!(Up) | ctrl!('p') => {
|
Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker (#1612)
* Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker
* Refactor file picker paging logic
* change key mapping
* Add overlay component
* Use closure instead of margin to calculate size
* Don't wrap file picker in `Overlay` automatically
2022-02-15 02:24:03 +01:00
|
|
|
self.move_by(1, Direction::Backward);
|
2021-08-12 09:00:42 +02:00
|
|
|
}
|
2022-03-31 09:51:11 +02:00
|
|
|
key!(Tab) | key!(Down) | ctrl!('n') => {
|
Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker (#1612)
* Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker
* Refactor file picker paging logic
* change key mapping
* Add overlay component
* Use closure instead of margin to calculate size
* Don't wrap file picker in `Overlay` automatically
2022-02-15 02:24:03 +01:00
|
|
|
self.move_by(1, Direction::Forward);
|
|
|
|
}
|
2022-03-31 09:51:11 +02:00
|
|
|
key!(PageDown) | ctrl!('d') => {
|
Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker (#1612)
* Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker
* Refactor file picker paging logic
* change key mapping
* Add overlay component
* Use closure instead of margin to calculate size
* Don't wrap file picker in `Overlay` automatically
2022-02-15 02:24:03 +01:00
|
|
|
self.page_down();
|
|
|
|
}
|
2022-03-31 09:51:11 +02:00
|
|
|
key!(PageUp) | ctrl!('u') => {
|
Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker (#1612)
* Add `PageUp`, `PageDown`, `Ctrl-u`, `Ctrl-d`, `Home`, `End` keyboard shortcuts to file picker
* Refactor file picker paging logic
* change key mapping
* Add overlay component
* Use closure instead of margin to calculate size
* Don't wrap file picker in `Overlay` automatically
2022-02-15 02:24:03 +01:00
|
|
|
self.page_up();
|
|
|
|
}
|
|
|
|
key!(Home) => {
|
|
|
|
self.to_start();
|
|
|
|
}
|
|
|
|
key!(End) => {
|
|
|
|
self.to_end();
|
2021-08-12 09:00:42 +02:00
|
|
|
}
|
2021-11-10 16:58:46 +01:00
|
|
|
key!(Esc) | ctrl!('c') => {
|
2020-12-17 10:08:16 +01:00
|
|
|
return close_fn;
|
2020-12-18 09:16:04 +01:00
|
|
|
}
|
2023-01-16 08:18:13 +01:00
|
|
|
alt!(Enter) => {
|
|
|
|
if let Some(option) = self.selection() {
|
|
|
|
(self.callback_fn)(cx, option, Action::Load);
|
|
|
|
}
|
|
|
|
}
|
2021-11-10 16:58:46 +01:00
|
|
|
key!(Enter) => {
|
2020-12-18 11:19:50 +01:00
|
|
|
if let Some(option) = self.selection() {
|
2021-11-07 10:03:04 +01:00
|
|
|
(self.callback_fn)(cx, option, Action::Replace);
|
2021-03-29 08:21:48 +02:00
|
|
|
}
|
|
|
|
return close_fn;
|
|
|
|
}
|
2021-11-10 16:58:46 +01:00
|
|
|
ctrl!('s') => {
|
2021-03-29 08:21:48 +02:00
|
|
|
if let Some(option) = self.selection() {
|
2021-11-07 10:03:04 +01:00
|
|
|
(self.callback_fn)(cx, option, Action::HorizontalSplit);
|
2021-03-29 08:21:48 +02:00
|
|
|
}
|
|
|
|
return close_fn;
|
|
|
|
}
|
2021-11-10 16:58:46 +01:00
|
|
|
ctrl!('v') => {
|
2021-03-29 08:21:48 +02:00
|
|
|
if let Some(option) = self.selection() {
|
2021-11-07 10:03:04 +01:00
|
|
|
(self.callback_fn)(cx, option, Action::VerticalSplit);
|
2020-12-18 09:16:04 +01:00
|
|
|
}
|
|
|
|
return close_fn;
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
2022-07-18 03:11:25 +02:00
|
|
|
ctrl!('t') => {
|
|
|
|
self.toggle_preview();
|
|
|
|
}
|
2020-12-18 08:43:15 +01:00
|
|
|
_ => {
|
2022-08-29 02:48:49 +02:00
|
|
|
self.prompt_handle_event(event, cx);
|
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) {
|
2021-08-13 10:56:37 +02:00
|
|
|
let text_style = cx.editor.theme.get("ui.text");
|
2022-03-01 02:30:02 +01:00
|
|
|
let selected = cx.editor.theme.get("ui.text.focus");
|
2022-07-08 20:46:09 +02:00
|
|
|
let highlight_style = cx.editor.theme.get("special").add_modifier(Modifier::BOLD);
|
2021-08-13 10:56:37 +02:00
|
|
|
|
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
|
2022-05-22 03:24:51 +02:00
|
|
|
let sep_style = cx.editor.theme.get("ui.background.separator");
|
2021-08-12 09:00:42 +02:00
|
|
|
let borders = BorderType::line_symbols(BorderType::Plain);
|
2020-12-17 10:08:16 +01:00
|
|
|
for x in inner.left()..inner.right() {
|
2022-01-16 02:55:28 +01:00
|
|
|
if let Some(cell) = surface.get_mut(x, inner.y + 1) {
|
|
|
|
cell.set_symbol(borders.horizontal).set_style(sep_style);
|
|
|
|
}
|
2020-12-17 10:08:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Render the contents:
|
2022-07-08 20:46:09 +02:00
|
|
|
// subtract area of prompt from top
|
|
|
|
let inner = inner.clip_top(2);
|
2020-12-17 10:08:16 +01:00
|
|
|
|
2021-08-12 09:00:42 +02:00
|
|
|
let rows = inner.height;
|
2022-01-23 15:06:28 +01:00
|
|
|
let offset = self.cursor - (self.cursor % std::cmp::max(1, rows as usize));
|
2022-07-08 20:46:09 +02:00
|
|
|
let cursor = self.cursor.saturating_sub(offset);
|
2020-12-18 08:43:15 +01:00
|
|
|
|
2022-07-08 20:46:09 +02:00
|
|
|
let options = self
|
2022-03-01 02:30:02 +01:00
|
|
|
.matches
|
2022-03-22 05:02:46 +01:00
|
|
|
.iter()
|
2022-03-01 02:30:02 +01:00
|
|
|
.skip(offset)
|
2022-07-08 20:46:09 +02:00
|
|
|
.take(rows as usize)
|
|
|
|
.map(|pmatch| &self.options[pmatch.index])
|
2022-12-25 06:54:09 +01:00
|
|
|
.map(|option| option.format(&self.editor_data))
|
2022-07-08 20:46:09 +02:00
|
|
|
.map(|mut row| {
|
|
|
|
const TEMP_CELL_SEP: &str = " ";
|
|
|
|
|
2022-12-24 12:21:38 +01:00
|
|
|
let line = row.cell_text().fold(String::new(), |mut s, frag| {
|
|
|
|
s.push_str(&frag);
|
|
|
|
s.push_str(TEMP_CELL_SEP);
|
|
|
|
s
|
|
|
|
});
|
2022-07-08 20:46:09 +02:00
|
|
|
|
|
|
|
// Items are filtered by using the text returned by menu::Item::filter_text
|
|
|
|
// but we do highlighting here using the text in Row and therefore there
|
|
|
|
// might be inconsistencies. This is the best we can do since only the
|
|
|
|
// text in Row is displayed to the end user.
|
|
|
|
let (_score, highlights) = FuzzyQuery::new(self.prompt.line())
|
|
|
|
.fuzzy_indicies(&line, &self.matcher)
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
let highlight_byte_ranges: Vec<_> = line
|
|
|
|
.char_indices()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(char_idx, (byte_offset, ch))| {
|
|
|
|
highlights
|
|
|
|
.contains(&char_idx)
|
|
|
|
.then(|| byte_offset..byte_offset + ch.len_utf8())
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// The starting byte index of the current (iterating) cell
|
|
|
|
let mut cell_start_byte_offset = 0;
|
|
|
|
for cell in row.cells.iter_mut() {
|
|
|
|
let spans = match cell.content.lines.get(0) {
|
|
|
|
Some(s) => s,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut cell_len = 0;
|
|
|
|
|
|
|
|
let graphemes_with_style: Vec<_> = spans
|
|
|
|
.0
|
|
|
|
.iter()
|
|
|
|
.flat_map(|span| {
|
|
|
|
span.content
|
|
|
|
.grapheme_indices(true)
|
|
|
|
.zip(std::iter::repeat(span.style))
|
|
|
|
})
|
|
|
|
.map(|((grapheme_byte_offset, grapheme), style)| {
|
|
|
|
cell_len += grapheme.len();
|
|
|
|
let start = cell_start_byte_offset;
|
|
|
|
|
|
|
|
let grapheme_byte_range =
|
|
|
|
grapheme_byte_offset..grapheme_byte_offset + grapheme.len();
|
|
|
|
|
|
|
|
if highlight_byte_ranges.iter().any(|hl_rng| {
|
|
|
|
hl_rng.start >= start + grapheme_byte_range.start
|
|
|
|
&& hl_rng.end <= start + grapheme_byte_range.end
|
|
|
|
}) {
|
|
|
|
(grapheme, style.patch(highlight_style))
|
2022-06-30 11:16:18 +02:00
|
|
|
} else {
|
2022-07-08 20:46:09 +02:00
|
|
|
(grapheme, style)
|
2022-06-30 11:16:18 +02:00
|
|
|
}
|
2022-07-08 20:46:09 +02:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let mut span_list: Vec<(String, Style)> = Vec::new();
|
|
|
|
for (grapheme, style) in graphemes_with_style {
|
|
|
|
if span_list.last().map(|(_, sty)| sty) == Some(&style) {
|
|
|
|
let (string, _) = span_list.last_mut().unwrap();
|
|
|
|
string.push_str(grapheme);
|
|
|
|
} else {
|
|
|
|
span_list.push((String::from(grapheme), style))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let spans: Vec<Span> = span_list
|
|
|
|
.into_iter()
|
|
|
|
.map(|(string, style)| Span::styled(string, style))
|
|
|
|
.collect();
|
|
|
|
let spans: Spans = spans.into();
|
|
|
|
*cell = Cell::from(spans);
|
|
|
|
|
|
|
|
cell_start_byte_offset += cell_len + TEMP_CELL_SEP.len();
|
|
|
|
}
|
|
|
|
|
|
|
|
row
|
2022-06-30 11:16:18 +02:00
|
|
|
});
|
2022-07-08 20:46:09 +02:00
|
|
|
|
|
|
|
let table = Table::new(options)
|
|
|
|
.style(text_style)
|
|
|
|
.highlight_style(selected)
|
|
|
|
.highlight_symbol(" > ")
|
|
|
|
.column_spacing(1)
|
|
|
|
.widths(&self.widths);
|
|
|
|
|
|
|
|
use tui::widgets::TableState;
|
|
|
|
|
|
|
|
table.render_table(
|
|
|
|
inner,
|
|
|
|
surface,
|
|
|
|
&mut TableState {
|
|
|
|
offset: 0,
|
|
|
|
selected: Some(cursor),
|
|
|
|
},
|
|
|
|
);
|
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
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2022-07-19 18:19:02 +02:00
|
|
|
|
|
|
|
/// Returns a new list of options to replace the contents of the picker
|
|
|
|
/// when called with the current picker query,
|
|
|
|
pub type DynQueryCallback<T> =
|
|
|
|
Box<dyn Fn(String, &mut Editor) -> BoxFuture<'static, anyhow::Result<Vec<T>>>>;
|
|
|
|
|
|
|
|
/// A picker that updates its contents via a callback whenever the
|
|
|
|
/// query string changes. Useful for live grep, workspace symbols, etc.
|
|
|
|
pub struct DynamicPicker<T: ui::menu::Item + Send> {
|
|
|
|
file_picker: FilePicker<T>,
|
|
|
|
query_callback: DynQueryCallback<T>,
|
2022-12-07 23:27:31 +01:00
|
|
|
query: String,
|
2022-07-19 18:19:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ui::menu::Item + Send> DynamicPicker<T> {
|
|
|
|
pub const ID: &'static str = "dynamic-picker";
|
|
|
|
|
|
|
|
pub fn new(file_picker: FilePicker<T>, query_callback: DynQueryCallback<T>) -> Self {
|
|
|
|
Self {
|
|
|
|
file_picker,
|
|
|
|
query_callback,
|
2022-12-07 23:27:31 +01:00
|
|
|
query: String::new(),
|
2022-07-19 18:19:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Item + Send + 'static> Component for DynamicPicker<T> {
|
|
|
|
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
|
|
|
|
self.file_picker.render(area, surface, cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
|
|
|
|
let event_result = self.file_picker.handle_event(event, cx);
|
|
|
|
let current_query = self.file_picker.picker.prompt.line();
|
|
|
|
|
2022-12-07 23:27:31 +01:00
|
|
|
if !matches!(event, Event::IdleTimeout) || self.query == *current_query {
|
2022-07-19 18:19:02 +02:00
|
|
|
return event_result;
|
|
|
|
}
|
|
|
|
|
2022-12-07 23:27:31 +01:00
|
|
|
self.query.clone_from(current_query);
|
|
|
|
|
2022-07-19 18:19:02 +02:00
|
|
|
let new_options = (self.query_callback)(current_query.to_owned(), cx.editor);
|
|
|
|
|
|
|
|
cx.jobs.callback(async move {
|
|
|
|
let new_options = new_options.await?;
|
|
|
|
let callback =
|
2022-12-07 23:24:32 +01:00
|
|
|
crate::job::Callback::EditorCompositor(Box::new(move |editor, compositor| {
|
2022-07-19 18:19:02 +02:00
|
|
|
// Wrapping of pickers in overlay is done outside the picker code,
|
|
|
|
// so this is fragile and will break if wrapped in some other widget.
|
|
|
|
let picker = match compositor.find_id::<Overlay<DynamicPicker<T>>>(Self::ID) {
|
|
|
|
Some(overlay) => &mut overlay.content.file_picker.picker,
|
|
|
|
None => return,
|
|
|
|
};
|
2023-03-08 02:51:52 +01:00
|
|
|
picker.set_options(new_options);
|
2022-12-07 23:24:32 +01:00
|
|
|
editor.reset_idle_timer();
|
2022-07-19 18:19:02 +02:00
|
|
|
}));
|
|
|
|
anyhow::Ok(callback)
|
|
|
|
});
|
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cursor(&self, area: Rect, ctx: &Editor) -> (Option<Position>, CursorKind) {
|
|
|
|
self.file_picker.cursor(area, ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
|
|
|
|
self.file_picker.required_size(viewport)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn id(&self) -> Option<&'static str> {
|
|
|
|
Some(Self::ID)
|
|
|
|
}
|
|
|
|
}
|