Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-term/src/ui/picker.rs')
| -rw-r--r-- | helix-term/src/ui/picker.rs | 1388 |
1 files changed, 643 insertions, 745 deletions
diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 4f77f8b9..e1131164 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -1,48 +1,34 @@ -mod handlers; -mod query; - use crate::{ alt, compositor::{self, Component, Compositor, Context, Event, EventResult}, - ctrl, key, shift, + ctrl, + job::Callback, + key, shift, ui::{ self, - document::{render_document, LinePos, TextRenderer}, - picker::query::PickerQuery, - text_decorations::DecorationManager, + document::{render_document, LineDecoration, LinePos, TextRenderer}, + fuzzy_match::FuzzyQuery, EditorView, }, }; -use futures_util::future::BoxFuture; -use helix_event::AsyncHook; -use nucleo::pattern::{CaseMatching, Normalization}; -use nucleo::{Config, Nucleo}; -use thiserror::Error; -use tokio::sync::mpsc::Sender; +use futures_util::{future::BoxFuture, FutureExt}; use tui::{ buffer::Buffer as Surface, layout::Constraint, text::{Span, Spans}, - widgets::{Block, BorderType, Cell, Row, Table}, + widgets::{Block, BorderType, Borders, Cell, Table}, }; +use fuzzy_matcher::skim::SkimMatcherV2 as Matcher; use tui::widgets::Widget; -use std::{ - borrow::Cow, - collections::HashMap, - io::Read, - path::Path, - sync::{ - atomic::{self, AtomicUsize}, - Arc, - }, -}; +use std::cmp::{self, Ordering}; +use std::{collections::HashMap, io::Read, path::PathBuf}; use crate::ui::{Prompt, PromptEvent}; use helix_core::{ - char_idx_at_visual_offset, fuzzy::MATCHER, movement::Direction, - text_annotations::TextAnnotations, unicode::segmentation::UnicodeSegmentation, Position, + movement::Direction, text_annotations::TextAnnotations, + unicode::segmentation::UnicodeSegmentation, Position, Syntax, }; use helix_view::{ editor::Action, @@ -52,40 +38,47 @@ use helix_view::{ Document, DocumentId, Editor, }; -use self::handlers::{DynamicQueryChange, DynamicQueryHandler, PreviewHighlightHandler}; - -pub const ID: &str = "picker"; +use super::{menu::Item, overlay::Overlay}; pub const MIN_AREA_WIDTH_FOR_PREVIEW: u16 = 72; /// Biggest file size to preview in bytes pub const MAX_FILE_SIZE_FOR_PREVIEW: u64 = 10 * 1024 * 1024; #[derive(PartialEq, Eq, Hash)] -pub enum PathOrId<'a> { +pub enum PathOrId { Id(DocumentId), - Path(&'a Path), + Path(PathBuf), } -impl<'a> From<&'a Path> for PathOrId<'a> { - fn from(path: &'a Path) -> Self { - Self::Path(path) +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<DocumentId> for PathOrId<'_> { +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) } } -type FileCallback<T> = Box<dyn for<'a> Fn(&'a Editor, &'a T) -> Option<FileLocation<'a>>>; +type FileCallback<T> = Box<dyn Fn(&Editor, &T) -> Option<FileLocation>>; /// File path and range of lines (used to align and highlight lines) -pub type FileLocation<'a> = (PathOrId<'a>, Option<(usize, usize)>); +pub type FileLocation = (PathOrId, Option<(usize, usize)>); pub enum CachedPreview { Document(Box<Document>), - Directory(Vec<(String, bool)>), Binary, LargeFile, NotFound, @@ -107,20 +100,12 @@ impl Preview<'_, '_> { } } - fn dir_content(&self) -> Option<&Vec<(String, bool)>> { - match self { - Preview::Cached(CachedPreview::Directory(dir_content)) => Some(dir_content), - _ => None, - } - } - /// Alternate text to show for the preview. fn placeholder(&self) -> &str { match *self { - Self::EditorDocument(_) => "<Invalid file location>", + Self::EditorDocument(_) => "<File preview>", Self::Cached(preview) => match preview { - CachedPreview::Document(_) => "<Invalid file location>", - CachedPreview::Directory(_) => "<Invalid directory location>", + CachedPreview::Document(_) => "<File preview>", CachedPreview::Binary => "<Binary file>", CachedPreview::LargeFile => "<File too large to preview>", CachedPreview::NotFound => "<File not found>", @@ -129,237 +114,46 @@ impl Preview<'_, '_> { } } -fn inject_nucleo_item<T, D>( - injector: &nucleo::Injector<T>, - columns: &[Column<T, D>], - item: T, - editor_data: &D, -) { - injector.push(item, |item, dst| { - for (column, text) in columns.iter().filter(|column| column.filter).zip(dst) { - *text = column.format_text(item, editor_data).into() - } - }); -} - -pub struct Injector<T, D> { - dst: nucleo::Injector<T>, - columns: Arc<[Column<T, D>]>, - editor_data: Arc<D>, - version: usize, - picker_version: Arc<AtomicUsize>, - /// A marker that requests a redraw when the injector drops. - /// This marker causes the "running" indicator to disappear when a background job - /// providing items is finished and drops. This could be wrapped in an [Arc] to ensure - /// that the redraw is only requested when all Injectors drop for a Picker (which removes - /// the "running" indicator) but the redraw handle is debounced so this is unnecessary. - _redraw: helix_event::RequestRedrawOnDrop, -} - -impl<I, D> Clone for Injector<I, D> { - fn clone(&self) -> Self { - Injector { - dst: self.dst.clone(), - columns: self.columns.clone(), - editor_data: self.editor_data.clone(), - version: self.version, - picker_version: self.picker_version.clone(), - _redraw: helix_event::RequestRedrawOnDrop, - } - } -} - -#[derive(Error, Debug)] -#[error("picker has been shut down")] -pub struct InjectorShutdown; - -impl<T, D> Injector<T, D> { - pub fn push(&self, item: T) -> Result<(), InjectorShutdown> { - if self.version != self.picker_version.load(atomic::Ordering::Relaxed) { - return Err(InjectorShutdown); - } - - inject_nucleo_item(&self.dst, &self.columns, item, &self.editor_data); - Ok(()) - } -} - -type ColumnFormatFn<T, D> = for<'a> fn(&'a T, &'a D) -> Cell<'a>; - -pub struct Column<T, D> { - name: Arc<str>, - format: ColumnFormatFn<T, D>, - /// Whether the column should be passed to nucleo for matching and filtering. - /// `DynamicPicker` uses this so that the dynamic column (for example regex in - /// global search) is not used for filtering twice. - filter: bool, - hidden: bool, -} - -impl<T, D> Column<T, D> { - pub fn new(name: impl Into<Arc<str>>, format: ColumnFormatFn<T, D>) -> Self { - Self { - name: name.into(), - format, - filter: true, - hidden: false, - } - } - - /// A column which does not display any contents - pub fn hidden(name: impl Into<Arc<str>>) -> Self { - let format = |_: &T, _: &D| unreachable!(); - - Self { - name: name.into(), - format, - filter: false, - hidden: true, - } - } - - pub fn without_filtering(mut self) -> Self { - self.filter = false; - self - } - - fn format<'a>(&self, item: &'a T, data: &'a D) -> Cell<'a> { - (self.format)(item, data) - } - - fn format_text<'a>(&self, item: &'a T, data: &'a D) -> Cow<'a, str> { - let text: String = self.format(item, data).content.into(); - text.into() - } -} - -/// Returns a new list of options to replace the contents of the picker -/// when called with the current picker query, -type DynQueryCallback<T, D> = - fn(&str, &mut Editor, Arc<D>, &Injector<T, D>) -> BoxFuture<'static, anyhow::Result<()>>; +pub const PICKER_ID: &'static str = "picker"; -pub struct Picker<T: 'static + Send + Sync, D: 'static> { - columns: Arc<[Column<T, D>]>, - primary_column: usize, - editor_data: Arc<D>, - version: Arc<AtomicUsize>, - matcher: Nucleo<T>, +pub struct Picker<T: Item> { + options: Vec<T>, + editor_data: T::Data, + // filter: String, + matcher: Box<Matcher>, + matches: Vec<PickerMatch>, /// Current height of the completions box completion_height: u16, - cursor: u32, + cursor: usize, + // pattern: String, prompt: Prompt, - query: PickerQuery, - + previous_pattern: (String, FuzzyQuery), /// Whether to show the preview panel (default true) show_preview: bool, /// Constraints for tabular formatting widths: Vec<Constraint>, callback_fn: PickerCallback<T>, - default_action: Action, pub truncate_start: bool, /// Caches paths to documents - preview_cache: HashMap<Arc<Path>, CachedPreview>, + preview_cache: HashMap<PathBuf, CachedPreview>, read_buffer: Vec<u8>, /// Given an item in the picker, return the file path and line number to display. file_fn: Option<FileCallback<T>>, - /// An event handler for syntax highlighting the currently previewed file. - preview_highlight_handler: Sender<Arc<Path>>, - dynamic_query_handler: Option<Sender<DynamicQueryChange>>, -} - -impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { - pub fn stream( - columns: impl IntoIterator<Item = Column<T, D>>, - editor_data: D, - ) -> (Nucleo<T>, Injector<T, D>) { - let columns: Arc<[_]> = columns.into_iter().collect(); - let matcher_columns = columns.iter().filter(|col| col.filter).count() as u32; - assert!(matcher_columns > 0); - let matcher = Nucleo::new( - Config::DEFAULT, - Arc::new(helix_event::request_redraw), - None, - matcher_columns, - ); - let streamer = Injector { - dst: matcher.injector(), - columns, - editor_data: Arc::new(editor_data), - version: 0, - picker_version: Arc::new(AtomicUsize::new(0)), - _redraw: helix_event::RequestRedrawOnDrop, - }; - (matcher, streamer) - } - pub fn new<C, O, F>( - columns: C, - primary_column: usize, - options: O, - editor_data: D, - callback_fn: F, - ) -> Self - where - C: IntoIterator<Item = Column<T, D>>, - O: IntoIterator<Item = T>, - F: Fn(&mut Context, &T, Action) + 'static, - { - let columns: Arc<[_]> = columns.into_iter().collect(); - let matcher_columns = columns - .iter() - .filter(|col: &&Column<T, D>| col.filter) - .count() as u32; - assert!(matcher_columns > 0); - let matcher = Nucleo::new( - Config::DEFAULT, - Arc::new(helix_event::request_redraw), - None, - matcher_columns, - ); - let injector = matcher.injector(); - for item in options { - inject_nucleo_item(&injector, &columns, item, &editor_data); - } - Self::with( - matcher, - columns, - primary_column, - Arc::new(editor_data), - Arc::new(AtomicUsize::new(0)), - callback_fn, - ) - } - - pub fn with_stream( - matcher: Nucleo<T>, - primary_column: usize, - injector: Injector<T, D>, - callback_fn: impl Fn(&mut Context, &T, Action) + 'static, - ) -> Self { - Self::with( - matcher, - injector.columns, - primary_column, - injector.editor_data, - injector.picker_version, - callback_fn, - ) - } + /// A unique identifier for the picker as a Component + id: &'static str, +} - fn with( - matcher: Nucleo<T>, - columns: Arc<[Column<T, D>]>, - default_column: usize, - editor_data: Arc<D>, - version: Arc<AtomicUsize>, +impl<T: Item + 'static> Picker<T> { + pub fn new( + options: Vec<T>, + editor_data: T::Data, callback_fn: impl Fn(&mut Context, &T, Action) + 'static, ) -> Self { - assert!(!columns.is_empty()); - let prompt = Prompt::new( "".into(), None, @@ -367,45 +161,41 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { |_editor: &mut Context, _pattern: &str, _event: PromptEvent| {}, ); - let widths = columns - .iter() - .map(|column| Constraint::Length(column.name.chars().count() as u16)) - .collect(); - - let query = PickerQuery::new(columns.iter().map(|col| &col.name).cloned(), default_column); - - Self { - columns, - primary_column: default_column, - matcher, + let mut picker = Self { + options, editor_data, - version, + matcher: Box::default(), + matches: Vec::new(), cursor: 0, prompt, - query, + previous_pattern: (String::new(), FuzzyQuery::default()), truncate_start: true, show_preview: true, callback_fn: Box::new(callback_fn), - default_action: Action::Replace, completion_height: 0, - widths, + widths: Vec::new(), preview_cache: HashMap::new(), read_buffer: Vec::with_capacity(1024), file_fn: None, - preview_highlight_handler: PreviewHighlightHandler::<T, D>::default().spawn(), - dynamic_query_handler: None, - } - } + id: PICKER_ID, + }; - pub fn injector(&self) -> Injector<T, D> { - Injector { - dst: self.matcher.injector(), - columns: self.columns.clone(), - editor_data: self.editor_data.clone(), - version: self.version.load(atomic::Ordering::Relaxed), - picker_version: self.version.clone(), - _redraw: helix_event::RequestRedrawOnDrop, - } + picker.calculate_column_widths(); + + // scoring on empty input + // TODO: just reuse score() + 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(), + } + })); + + picker } pub fn truncate_start(mut self, truncate_start: bool) -> Self { @@ -415,49 +205,130 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { pub fn with_preview( mut self, - preview_fn: impl for<'a> Fn(&'a Editor, &'a T) -> Option<FileLocation<'a>> + 'static, + preview_fn: impl Fn(&Editor, &T) -> Option<FileLocation> + 'static, ) -> Self { self.file_fn = Some(Box::new(preview_fn)); - // assumption: if we have a preview we are matching paths... If this is ever - // not true this could be a separate builder function - self.matcher.update_config(Config::DEFAULT.match_paths()); self } - pub fn with_history_register(mut self, history_register: Option<char>) -> Self { - self.prompt.with_history_register(history_register); + pub fn with_id(mut self, id: &'static str) -> Self { + self.id = id; self } - pub fn with_initial_cursor(mut self, cursor: u32) -> Self { - self.cursor = cursor; - self + pub fn set_options(&mut self, new_options: Vec<T>) { + self.options = new_options; + self.cursor = 0; + self.force_score(); + self.calculate_column_widths(); } - pub fn with_dynamic_query( - mut self, - callback: DynQueryCallback<T, D>, - debounce_ms: Option<u64>, - ) -> Self { - let handler = DynamicQueryHandler::new(callback, debounce_ms).spawn(); - let event = DynamicQueryChange { - query: self.primary_query(), - // Treat the initial query as a paste. - is_paste: true, - }; - helix_event::send_blocking(&handler, event); - self.dynamic_query_handler = Some(handler); - self + /// 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(); } - pub fn with_default_action(mut self, action: Action) -> Self { - self.default_action = action; - self + pub fn score(&mut self) { + let pattern = self.prompt.line(); + + if pattern == &self.previous_pattern.0 { + return; + } + + let (query, is_refined) = self + .previous_pattern + .1 + .refine(pattern, &self.previous_pattern.0); + + if pattern.is_empty() { + // Fast path for no pattern. + self.matches.clear(); + 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(), + } + })); + } else if is_refined { + // optimization: if the pattern is a more specific version of the previous one + // then we can score the filtered set. + self.matches.retain_mut(|pmatch| { + let option = &self.options[pmatch.index]; + let text = option.sort_text(&self.editor_data); + + match query.fuzzy_match(&text, &self.matcher) { + Some(s) => { + // Update the score + pmatch.score = s; + true + } + None => false, + } + }); + + self.matches.sort_unstable(); + } else { + self.force_score(); + } + + // reset cursor position + self.cursor = 0; + let pattern = self.prompt.line(); + self.previous_pattern.0.clone_from(pattern); + self.previous_pattern.1 = query; + } + + 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(), + }) + }), + ); + + self.matches.sort_unstable(); } /// Move the cursor by a number of lines, either down (`Forward`) or up (`Backward`) - pub fn move_by(&mut self, amount: u32, direction: Direction) { - let len = self.matcher.snapshot().matched_item_count(); + pub fn move_by(&mut self, amount: usize, direction: Direction) { + let len = self.matches.len(); if len == 0 { // No results, can't move. @@ -476,12 +347,12 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { /// 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 u32, Direction::Backward); + 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 u32, Direction::Forward); + self.move_by(self.completion_height as usize, Direction::Forward); } /// Move the cursor to the first entry @@ -491,33 +362,13 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { /// Move the cursor to the last entry pub fn to_end(&mut self) { - self.cursor = self - .matcher - .snapshot() - .matched_item_count() - .saturating_sub(1); + self.cursor = self.matches.len().saturating_sub(1); } pub fn selection(&self) -> Option<&T> { - self.matcher - .snapshot() - .get_matched_item(self.cursor) - .map(|item| item.data) - } - - fn primary_query(&self) -> Arc<str> { - self.query - .get(&self.columns[self.primary_column].name) - .cloned() - .unwrap_or_else(|| "".into()) - } - - fn header_height(&self) -> u16 { - if self.columns.len() > 1 { - 1 - } else { - 0 - } + self.matches + .get(self.cursor) + .map(|pmatch| &self.options[pmatch.index]) } pub fn toggle_preview(&mut self) { @@ -526,170 +377,132 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { fn prompt_handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult { if let EventResult::Consumed(_) = self.prompt.handle_event(event, cx) { - self.handle_prompt_change(matches!(event, Event::Paste(_))); + // TODO: recalculate only if pattern changed + self.score(); } EventResult::Consumed(None) } - fn handle_prompt_change(&mut self, is_paste: bool) { - // TODO: better track how the pattern has changed - let line = self.prompt.line(); - let old_query = self.query.parse(line); - if self.query == old_query { - return; - } - // If the query has meaningfully changed, reset the cursor to the top of the results. - self.cursor = 0; - // Have nucleo reparse each changed column. - for (i, column) in self - .columns - .iter() - .filter(|column| column.filter) - .enumerate() - { - let pattern = self - .query - .get(&column.name) - .map(|f| &**f) - .unwrap_or_default(); - let old_pattern = old_query - .get(&column.name) - .map(|f| &**f) - .unwrap_or_default(); - // Fastlane: most columns will remain unchanged after each edit. - if pattern == old_pattern { - continue; - } - let is_append = pattern.starts_with(old_pattern); - self.matcher.pattern.reparse( - i, - pattern, - CaseMatching::Smart, - Normalization::Smart, - is_append, - ); - } - // If this is a dynamic picker, notify the query hook that the primary - // query might have been updated. - if let Some(handler) = &self.dynamic_query_handler { - let event = DynamicQueryChange { - query: self.primary_query(), - is_paste, - }; - helix_event::send_blocking(handler, event); - } + fn current_file(&self, editor: &Editor) -> Option<FileLocation> { + self.selection() + .and_then(|current| (self.file_fn.as_ref()?)(editor, current)) + .and_then(|(path_or_id, line)| path_or_id.get_canonicalized().ok().zip(Some(line))) } - /// Get (cached) preview for the currently selected item. If a document corresponding + /// 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, + path_or_id: PathOrId, editor: &'editor Editor, - ) -> Option<(Preview<'picker, 'editor>, Option<(usize, usize)>)> { - let current = self.selection()?; - let (path_or_id, range) = (self.file_fn.as_ref()?)(editor, current)?; - + ) -> Preview<'picker, 'editor> { match path_or_id { PathOrId::Path(path) => { + let path = &path; if let Some(doc) = editor.document_by_path(path) { - return Some((Preview::EditorDocument(doc), range)); + return Preview::EditorDocument(doc); } if self.preview_cache.contains_key(path) { - // NOTE: we use `HashMap::get_key_value` here instead of indexing so we can - // retrieve the `Arc<Path>` key. The `path` in scope here is a `&Path` and - // we can cheaply clone the key for the preview highlight handler. - let (path, preview) = self.preview_cache.get_key_value(path).unwrap(); - if matches!(preview, CachedPreview::Document(doc) if doc.syntax().is_none()) { - helix_event::send_blocking(&self.preview_highlight_handler, path.clone()); - } - return Some((Preview::Cached(preview), range)); + return Preview::Cached(&self.preview_cache[path]); } - let path: Arc<Path> = path.into(); - let preview = std::fs::metadata(&path) - .and_then(|metadata| { - if metadata.is_dir() { - let files = super::directory_content(&path, editor)?; - let file_names: Vec<_> = files - .iter() - .filter_map(|(file_path, is_dir)| { - let name = file_path - .strip_prefix(&path) - .map(|p| Some(p.as_os_str())) - .unwrap_or_else(|_| file_path.file_name())? - .to_string_lossy(); - if *is_dir { - Some((format!("{}/", name), true)) - } else { - Some((name.into_owned(), false)) - } - }) - .collect(); - Ok(CachedPreview::Directory(file_names)) - } else if metadata.is_file() { - if metadata.len() > MAX_FILE_SIZE_FOR_PREVIEW { - return Ok(CachedPreview::LargeFile); + 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 } - let content_type = std::fs::File::open(&path).and_then(|file| { - // 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(content_type) - })?; - if content_type.is_binary() { - return Ok(CachedPreview::Binary); + _ => { + // TODO: enable syntax highlighting; blocked by async rendering + Document::open(path, None, None, editor.config.clone()) + .map(|doc| CachedPreview::Document(Box::new(doc))) + .unwrap_or(CachedPreview::NotFound) } - let mut doc = Document::open( - &path, - None, - false, - editor.config.clone(), - editor.syn_loader.clone(), - ) - .or(Err(std::io::Error::new( - std::io::ErrorKind::NotFound, - "Cannot open document", - )))?; - let loader = editor.syn_loader.load(); - if let Some(language_config) = doc.detect_language_config(&loader) { - doc.language = Some(language_config); - // Asynchronously highlight the new document - helix_event::send_blocking( - &self.preview_highlight_handler, - path.clone(), - ); - } - Ok(CachedPreview::Document(Box::new(doc))) - } else { - Err(std::io::Error::new( - std::io::ErrorKind::NotFound, - "Neither a dir, nor a file", - )) - } - }) + }, + ) .unwrap_or(CachedPreview::NotFound); - self.preview_cache.insert(path.clone(), preview); - Some((Preview::Cached(&self.preview_cache[&path]), range)) + self.preview_cache.insert(path.to_owned(), preview); + Preview::Cached(&self.preview_cache[path]) } PathOrId::Id(id) => { let doc = editor.documents.get(&id).unwrap(); - Some((Preview::EditorDocument(doc), range)) + Preview::EditorDocument(doc) } } } - fn render_picker(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { - let status = self.matcher.tick(10); - let snapshot = self.matcher.snapshot(); - if status.changed { - self.cursor = self - .cursor - .min(snapshot.matched_item_count().saturating_sub(1)) + fn handle_idle_timeout(&mut self, cx: &mut Context) -> EventResult { + let Some((current_file, _)) = self.current_file(cx.editor) else { + return EventResult::Consumed(None) + }; + + // Try to find a document in the cache + let doc = match ¤t_file { + PathOrId::Id(doc_id) => doc_mut!(cx.editor, doc_id), + PathOrId::Path(path) => match self.preview_cache.get_mut(path) { + Some(CachedPreview::Document(ref mut doc)) => doc, + _ => return EventResult::Consumed(None), + }, + }; + + let mut callback: Option<compositor::Callback> = None; + + // Then attempt to highlight it if it has no language set + if doc.language_config().is_none() { + if let Some(language_config) = doc.detect_language_config(&cx.editor.syn_loader) { + doc.language = Some(language_config.clone()); + let text = doc.text().clone(); + let loader = cx.editor.syn_loader.clone(); + let job = tokio::task::spawn_blocking(move || { + let syntax = language_config + .highlight_config(&loader.scopes()) + .and_then(|highlight_config| Syntax::new(&text, highlight_config, loader)); + let callback = move |editor: &mut Editor, compositor: &mut Compositor| { + let Some(syntax) = syntax else { + log::info!("highlighting picker item failed"); + return + }; + let Some(Overlay { content: picker, .. }) = compositor.find::<Overlay<Self>>() else { + log::info!("picker closed before syntax highlighting finished"); + return + }; + // Try to find a document in the cache + let doc = match current_file { + PathOrId::Id(doc_id) => doc_mut!(editor, &doc_id), + PathOrId::Path(path) => match picker.preview_cache.get_mut(&path) { + Some(CachedPreview::Document(ref mut doc)) => doc, + _ => return, + }, + }; + doc.syntax = Some(syntax); + }; + Callback::EditorCompositor(Box::new(callback)) + }); + let tmp: compositor::Callback = Box::new(move |_, ctx| { + ctx.jobs + .callback(job.map(|res| res.map_err(anyhow::Error::from))) + }); + callback = Some(Box::new(tmp)) + } } + // QUESTION: do we want to compute inlay hints in pickers too ? Probably not for now + // but it could be interesting in the future + + EventResult::Consumed(callback) + } + + fn render_picker(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { let text_style = cx.editor.theme.get("ui.text"); let selected = cx.editor.theme.get("ui.text.focus"); let highlight_style = cx.editor.theme.get("special").add_modifier(Modifier::BOLD); @@ -699,32 +512,19 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { let background = cx.editor.theme.get("ui.background"); surface.clear_with(area, background); - const BLOCK: Block<'_> = Block::bordered(); + // 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); + let inner = block.inner(area); - BLOCK.render(area, surface); + block.render(area, surface); // -- Render the input bar: - let count = format!( - "{}{}/{}", - if status.running || self.matcher.active_injectors() > 0 { - "(running) " - } else { - "" - }, - snapshot.matched_item_count(), - snapshot.item_count(), - ); - let area = inner.clip_left(1).with_height(1); - let line_area = area.clip_right(count.len() as u16 + 1); - - // render the prompt first since it will clear its background - self.prompt.render(line_area, surface, cx); + 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, @@ -733,6 +533,8 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { text_style, ); + self.prompt.render(area, surface, cx); + // -- Separator let sep_style = cx.editor.theme.get("ui.background.separator"); let borders = BorderType::line_symbols(BorderType::Plain); @@ -745,128 +547,114 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { // -- Render the contents: // subtract area of prompt from top let inner = inner.clip_top(2); - let rows = inner.height.saturating_sub(self.header_height()) as u32; - let offset = self.cursor - (self.cursor % std::cmp::max(1, rows)); - let cursor = self.cursor.saturating_sub(offset); - let end = offset - .saturating_add(rows) - .min(snapshot.matched_item_count()); - let mut indices = Vec::new(); - let mut matcher = MATCHER.lock(); - matcher.config = Config::DEFAULT; - if self.file_fn.is_some() { - matcher.config.set_match_paths() - } - let options = snapshot.matched_items(offset..end).map(|item| { - let mut widths = self.widths.iter_mut(); - let mut matcher_index = 0; + let rows = inner.height; + let offset = self.cursor - (self.cursor % std::cmp::max(1, rows as usize)); + let cursor = self.cursor.saturating_sub(offset); - Row::new(self.columns.iter().map(|column| { - if column.hidden { - return Cell::default(); - } + let options = self + .matches + .iter() + .skip(offset) + .take(rows as usize) + .map(|pmatch| &self.options[pmatch.index]) + .map(|option| option.format(&self.editor_data)) + .map(|mut row| { + const TEMP_CELL_SEP: &str = " "; + + let line = row.cell_text().fold(String::new(), |mut s, frag| { + s.push_str(&frag); + s.push_str(TEMP_CELL_SEP); + s + }); + + // 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_indices(&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 => { + cell_start_byte_offset += TEMP_CELL_SEP.len(); + continue; + } + }; - let Some(Constraint::Length(max_width)) = widths.next() else { - unreachable!(); - }; - let mut cell = column.format(item.data, &self.editor_data); - let width = if column.filter { - snapshot.pattern().column_pattern(matcher_index).indices( - item.matcher_columns[matcher_index].slice(..), - &mut matcher, - &mut indices, - ); - indices.sort_unstable(); - indices.dedup(); - let mut indices = indices.drain(..); - let mut next_highlight_idx = indices.next().unwrap_or(u32::MAX); - let mut span_list = Vec::new(); - let mut current_span = String::new(); - let mut current_style = Style::default(); - let mut grapheme_idx = 0u32; - let mut width = 0; - - let spans: &[Span] = - cell.content.lines.first().map_or(&[], |it| it.0.as_slice()); - for span in spans { - // this looks like a bug on first glance, we are iterating - // graphemes but treating them as char indices. The reason that - // this is correct is that nucleo will only ever consider the first char - // of a grapheme (and discard the rest of the grapheme) so the indices - // returned by nucleo are essentially grapheme indecies - for grapheme in span.content.graphemes(true) { - let style = if grapheme_idx == next_highlight_idx { - next_highlight_idx = indices.next().unwrap_or(u32::MAX); - span.style.patch(highlight_style) + 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)) } else { - span.style - }; - if style != current_style { - if !current_span.is_empty() { - span_list.push(Span::styled(current_span, current_style)) - } - current_span = String::new(); - current_style = style; + (grapheme, style) } - current_span.push_str(grapheme); - grapheme_idx += 1; + }) + .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)) } - width += span.width(); } - span_list.push(Span::styled(current_span, current_style)); - cell = Cell::from(Spans::from(span_list)); - matcher_index += 1; - width - } else { - cell.content - .lines - .first() - .map(|line| line.width()) - .unwrap_or_default() - }; + 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); - if width as u16 > *max_width { - *max_width = width as u16; + cell_start_byte_offset += cell_len + TEMP_CELL_SEP.len(); } - cell - })) - }); + row + }); - let mut table = Table::new(options) + let table = Table::new(options) .style(text_style) .highlight_style(selected) .highlight_symbol(" > ") .column_spacing(1) .widths(&self.widths); - // -- Header - if self.columns.len() > 1 { - let active_column = self.query.active_column(self.prompt.position()); - let header_style = cx.editor.theme.get("ui.picker.header"); - let header_column_style = cx.editor.theme.get("ui.picker.header.column"); - - table = table.header( - Row::new(self.columns.iter().map(|column| { - if column.hidden { - Cell::default() - } else { - let style = - if active_column.is_some_and(|name| Arc::ptr_eq(name, &column.name)) { - cx.editor.theme.get("ui.picker.header.column.active") - } else { - header_column_style - }; - - Cell::from(Span::styled(Cow::from(&*column.name), style)) - } - })) - .style(header_style), - ); - } - use tui::widgets::TableState; table.render_table( @@ -874,7 +662,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { surface, &mut TableState { offset: 0, - selected: Some(cursor as usize), + selected: Some(cursor), }, self.truncate_start, ); @@ -885,44 +673,23 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { // clear area let background = cx.editor.theme.get("ui.background"); let text = cx.editor.theme.get("ui.text"); - let directory = cx.editor.theme.get("ui.text.directory"); surface.clear_with(area, background); - const BLOCK: Block<'_> = Block::bordered(); + // 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); + let inner = block.inner(area); // 1 column gap on either side let margin = Margin::horizontal(1); - let inner = inner.inner(margin); - BLOCK.render(area, surface); + let inner = inner.inner(&margin); + block.render(area, surface); - if let Some((preview, range)) = self.get_preview(cx.editor) { + if let Some((path, range)) = self.current_file(cx.editor) { + let preview = self.get_preview(path, cx.editor); let doc = match preview.document() { - Some(doc) - if range.is_none_or(|(start, end)| { - start <= end && end <= doc.text().len_lines() - }) => - { - doc - } - _ => { - if let Some(dir_content) = preview.dir_content() { - for (i, (path, is_dir)) in - dir_content.iter().take(inner.height as usize).enumerate() - { - let style = if *is_dir { directory } else { text }; - surface.set_stringn( - inner.x, - inner.y + i as u16, - path, - inner.width as usize, - style, - ); - } - return; - } - + 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; @@ -931,46 +698,34 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { } }; - let mut offset = ViewPosition::default(); - if let Some((start_line, end_line)) = range { - let height = end_line - start_line; - let text = doc.text().slice(..); - let start = text.line_to_char(start_line); - let middle = text.line_to_char(start_line + height / 2); - if height < inner.height as usize { - let text_fmt = doc.text_format(inner.width, None); - let annotations = TextAnnotations::default(); - (offset.anchor, offset.vertical_offset) = char_idx_at_visual_offset( - text, - middle, - // align to middle - -(inner.height as isize / 2), - 0, - &text_fmt, - &annotations, - ); - if start < offset.anchor { - offset.anchor = start; - offset.vertical_offset = 0; - } - } else { - offset.anchor = start; - } - } - - let loader = cx.editor.syn_loader.load(); - - let syntax_highlighter = - EditorView::doc_syntax_highlighter(doc, offset.anchor, area.height, &loader); - let mut overlay_highlights = Vec::new(); + // align to middle + let first_line = range + .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); + + let offset = ViewPosition { + anchor: doc.text().line_to_char(first_line), + horizontal_offset: 0, + vertical_offset: 0, + }; - EditorView::doc_diagnostics_highlights_into( + let mut highlights = EditorView::doc_syntax_highlights( doc, + offset.anchor, + area.height, &cx.editor.theme, - &mut overlay_highlights, ); - - let mut decorations = DecorationManager::default(); + 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)); + } + let mut decorations: Vec<Box<dyn LineDecoration>> = Vec::new(); if let Some((start, end)) = range { let style = cx @@ -982,14 +737,14 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { if (start..=end).contains(&pos.doc_line) { let area = Rect::new( renderer.viewport.x, - pos.visual_line, + renderer.viewport.y + pos.visual_line, renderer.viewport.width, 1, ); - renderer.set_style(area, style) + renderer.surface.set_style(area, style) } }; - decorations.add_decoration(draw_highlight); + decorations.push(Box::new(draw_highlight)) } render_document( @@ -999,16 +754,16 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { offset, // TODO: compute text annotations asynchronously here (like inlay hints) &TextAnnotations::default(), - syntax_highlighter, - overlay_highlights, + highlights, &cx.editor.theme, - decorations, + &mut decorations, + &mut [], ); } } } -impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I, D> { +impl<T: Item + 'static> Component for Picker<T> { fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { // +---------+ +---------+ // |prompt | |preview | @@ -1017,8 +772,7 @@ impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I, // | | | | // +---------+ +---------+ - let render_preview = - self.show_preview && self.file_fn.is_some() && area.width > MIN_AREA_WIDTH_FOR_PREVIEW; + let render_preview = self.show_preview && area.width > MIN_AREA_WIDTH_FOR_PREVIEW; let picker_width = if render_preview { area.width / 2 @@ -1036,6 +790,9 @@ impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I, } fn handle_event(&mut self, event: &Event, ctx: &mut Context) -> EventResult { + if let Event::IdleTimeout = event { + return self.handle_idle_timeout(ctx); + } // TODO: keybinds for scrolling preview let key_event = match event { @@ -1045,28 +802,26 @@ impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I, _ => return EventResult::Ignored(None), }; - let close_fn = |picker: &mut Self| { - // if the picker is very large don't store it as last_picker to avoid - // excessive memory consumption - let callback: compositor::Callback = - if picker.matcher.snapshot().item_count() > 1_000_000 { - Box::new(|compositor: &mut Compositor, _ctx| { - // remove the layer - compositor.pop(); - }) - } else { - // stop streaming in new items in the background, really we should - // be restarting the stream somehow once the picker gets - // reopened instead (like for an FS crawl) that would also remove the - // need for the special case above but that is pretty tricky - picker.version.fetch_add(1, atomic::Ordering::Relaxed); - Box::new(|compositor: &mut Compositor, _ctx| { - // remove the layer - compositor.last_picker = compositor.pop(); - }) - }; - EventResult::Consumed(Some(callback)) - }; + match ctx.keymaps.get_by_component_id(self.id, key_event) { + crate::keymap::KeymapResult::Matched(crate::keymap::MappableCommand::Component { + fun, + .. + }) => { + if let EventResult::Consumed(callback) = fun(self, ctx) { + return EventResult::Consumed(callback); + } + } + _ => (), + } + + let close_fn = + EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _ctx| { + // remove the layer + compositor.last_picker = compositor.pop(); + }))); + + // So that idle timeout retriggers + ctx.editor.reset_idle_timer(); match key_event { shift!(Tab) | key!(Up) | ctrl!('p') => { @@ -1087,58 +842,31 @@ impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I, key!(End) => { self.to_end(); } - key!(Esc) | ctrl!('c') => return close_fn(self), + key!(Esc) | ctrl!('c') => { + return close_fn; + } alt!(Enter) => { if let Some(option) = self.selection() { - (self.callback_fn)(ctx, option, self.default_action); + (self.callback_fn)(ctx, option, Action::Load); } } key!(Enter) => { - // If the prompt has a history completion and is empty, use enter to accept - // that completion - if let Some(completion) = self - .prompt - .first_history_completion(ctx.editor) - .filter(|_| self.prompt.line().is_empty()) - { - // The percent character is used by the query language and needs to be - // escaped with a backslash. - let completion = if completion.contains('%') { - completion.replace('%', "\\%") - } else { - completion.into_owned() - }; - self.prompt.set_line(completion, ctx.editor); - - // Inserting from the history register is a paste. - self.handle_prompt_change(true); - } else { - if let Some(option) = self.selection() { - (self.callback_fn)(ctx, option, self.default_action); - } - if let Some(history_register) = self.prompt.history_register() { - if let Err(err) = ctx - .editor - .registers - .push(history_register, self.primary_query().to_string()) - { - ctx.editor.set_error(err.to_string()); - } - } - return close_fn(self); + if let Some(option) = self.selection() { + (self.callback_fn)(ctx, option, Action::Replace); } + return close_fn; } ctrl!('s') => { if let Some(option) = self.selection() { (self.callback_fn)(ctx, option, Action::HorizontalSplit); } - return close_fn(self); + return close_fn; } ctrl!('v') => { if let Some(option) = self.selection() { (self.callback_fn)(ctx, option, Action::VerticalSplit); } - return close_fn(self); + return close_fn; } ctrl!('t') => { self.toggle_preview(); @@ -1152,38 +880,208 @@ impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I, } fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) { - let block = Block::bordered(); + let block = Block::default().borders(Borders::ALL); // calculate the inner area inside the box let inner = block.inner(area); // prompt area - let render_preview = - self.show_preview && self.file_fn.is_some() && area.width > MIN_AREA_WIDTH_FOR_PREVIEW; - - let picker_width = if render_preview { - area.width / 2 - } else { - area.width - }; - let area = inner.clip_left(1).with_height(1).with_width(picker_width); + let area = inner.clip_left(1).with_height(1); self.prompt.cursor(area, editor) } fn required_size(&mut self, (width, height): (u16, u16)) -> Option<(u16, u16)> { - self.completion_height = height.saturating_sub(4 + self.header_height()); + self.completion_height = height.saturating_sub(4); Some((width, height)) } fn id(&self) -> Option<&'static str> { - Some(ID) + Some(self.id) + } +} + +#[derive(PartialEq, Eq, Debug)] +struct PickerMatch { + score: i64, + index: usize, + len: usize, +} + +impl PickerMatch { + fn key(&self) -> impl Ord { + (cmp::Reverse(self.score), self.len, self.index) + } +} + +impl PartialOrd for PickerMatch { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + Some(self.cmp(other)) } } -impl<T: 'static + Send + Sync, D> Drop for Picker<T, D> { - fn drop(&mut self) { - // ensure we cancel any ongoing background threads streaming into the picker - self.version.fetch_add(1, atomic::Ordering::Relaxed); + +impl Ord for PickerMatch { + fn cmp(&self, other: &Self) -> Ordering { + self.key().cmp(&other.key()) } } type PickerCallback<T> = Box<dyn Fn(&mut Context, &T, Action)>; + +/// 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>>>>; + +pub const DYNAMIC_PICKER_ID: &'static str = "dynamic-picker"; + +/// 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: Picker<T>, + query_callback: DynQueryCallback<T>, + query: String, +} + +impl<T: ui::menu::Item + Send> DynamicPicker<T> { + pub fn new(file_picker: Picker<T>, query_callback: DynQueryCallback<T>) -> Self { + Self { + file_picker, + query_callback, + query: String::new(), + } + } +} + +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.prompt.line(); + + if !matches!(event, Event::IdleTimeout) || self.query == *current_query { + return event_result; + } + + self.query.clone_from(current_query); + + 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 = Callback::EditorCompositor(Box::new(move |editor, compositor| { + // 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>>>(DYNAMIC_PICKER_ID) { + Some(overlay) => &mut overlay.content.file_picker, + None => return, + }; + picker.set_options(new_options); + editor.reset_idle_timer(); + })); + 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(DYNAMIC_PICKER_ID) + } +} + +pub fn close_buffer_in_buffer_picker( + component: &mut dyn Component, + cx: &mut Context, +) -> EventResult { + let Some(picker) = component + .as_any_mut() + .downcast_mut::<crate::commands::BufferPicker>() + else { + return EventResult::Ignored(None); + }; + let Some(id) = picker.selection().map(|meta| meta.id) else { + return EventResult::Ignored(None); + }; + match cx.editor.close_document(id, false) { + Ok(_) => { + picker.options.retain(|item| item.id != id); + if picker.options.is_empty() { + return close_fn(); + } + picker.cursor = picker.cursor.saturating_sub(1); + picker.force_score(); + } + // TODO: impl From<CloseError> for anyhow::Error + Err(_err) => cx.editor.set_error("Failed to close buffer"), + } + + EventResult::Consumed(None) +} + +// Above command is cool because it's for one specific picker. + +// This is also cool because it doesn't even need to interact with +// the picker, so we don't need concrete types: + +pub fn close_picker(_component: &mut dyn Component, _cx: &mut Context) -> EventResult { + close_fn() +} + +// Now this is a problem. It compiles ok. +// We can probably even specify it in the default keymap: +// +// MappableCommand::Component { name: "..", doc: "..", fun: crate::ui::picker::to_start<PathBuf> } +// +// But how do we represent this in keymap config? Do we do namespacing in the +// command names and end up with tens of commands for scrolling each picker? +// +// MappableCommand::Component { +// name: "file_picker::to_start", +// doc: "..", +// crate::ui::picker::to_start<PathBuf>, +// }, +// MappableCommand::Component { +// name: "buffer_picker::to_start", +// doc: "..", +// crate::ui::picker::to_start<BufferMeta>, +// }, +// +// Can we use a macro to close over the verbose parts of this? +// +// Can we do something clever with a hypothetical AnyPicker interface +// similar to AnyComponent? Will we have to do that for every Component +// that uses generics? + +pub fn to_start<T: ui::menu::Item + 'static>( + component: &mut dyn Component, + _cx: &mut Context, +) -> EventResult { + let Some(picker) = component + .as_any_mut() + .downcast_mut::<Picker<T>>() + else { + return EventResult::Ignored(None); + }; + + picker.cursor = 0; + + EventResult::Consumed(None) +} + +fn close_fn() -> EventResult { + EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _ctx| { + // remove the layer + compositor.last_picker = compositor.pop(); + }))) +} |