@@ -3,6 +3,7 @@ use crate::{
3 ctrl, key, shift,
3 ctrl, key, shift,
4 ui::{self, fuzzy_match::FuzzyQuery, EditorView},
4 ui::{self, fuzzy_match::FuzzyQuery, EditorView},
6+use futures_util::future::BoxFuture;
7 buffer::Buffer as Surface,
8 buffer::Buffer as Surface,
8 widgets::{Block, BorderType, Borders},
9 widgets::{Block, BorderType, Borders},
@@ -22,7 +23,7 @@ use helix_view::{
22 Document, DocumentId, Editor,
23 Document, DocumentId, Editor,
25−use super::menu::Item;
26+use super::{menu::Item, overlay::Overlay};
27pub const MIN_AREA_WIDTH_FOR_PREVIEW: u16 = 72;
28pub const MIN_AREA_WIDTH_FOR_PREVIEW: u16 = 72;
28/// Biggest file size to preview in bytes
29/// Biggest file size to preview in bytes
@@ -751,3 +752,74 @@ impl<T: Item + 'static> Component for Picker<T> {
751 self.prompt.cursor(area, editor)
752 self.prompt.cursor(area, editor)
756+/// Returns a new list of options to replace the contents of the picker
757+/// when called with the current picker query,
758+pub type DynQueryCallback<T> =
759+ Box<dyn Fn(String, &mut Editor) -> BoxFuture<'static, anyhow::Result<Vec<T>>>>;
761+/// A picker that updates its contents via a callback whenever the
762+/// query string changes. Useful for live grep, workspace symbols, etc.
763+pub struct DynamicPicker<T: ui::menu::Item + Send> {
764+ file_picker: FilePicker<T>,
765+ query_callback: DynQueryCallback<T>,
768+impl<T: ui::menu::Item + Send> DynamicPicker<T> {
769+ pub const ID: &'static str = "dynamic-picker";
771+ pub fn new(file_picker: FilePicker<T>, query_callback: DynQueryCallback<T>) -> Self {
779+impl<T: Item + Send + 'static> Component for DynamicPicker<T> {
780+ fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
781+ self.file_picker.render(area, surface, cx);
784+ fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
785+ let prev_query = self.file_picker.picker.prompt.line().to_owned();
786+ let event_result = self.file_picker.handle_event(event, cx);
787+ let current_query = self.file_picker.picker.prompt.line();
789+ if *current_query == prev_query || matches!(event_result, EventResult::Ignored(_)) {
790+ return event_result;
793+ let new_options = (self.query_callback)(current_query.to_owned(), cx.editor);
795+ cx.jobs.callback(async move {
796+ let new_options = new_options.await?;
798+ crate::job::Callback::EditorCompositor(Box::new(move |_editor, compositor| {
799+ // Wrapping of pickers in overlay is done outside the picker code,
800+ // so this is fragile and will break if wrapped in some other widget.
801+ let picker = match compositor.find_id::<Overlay<DynamicPicker<T>>>(Self::ID) {
802+ Some(overlay) => &mut overlay.content.file_picker.picker,
805+ picker.options = new_options;
807+ picker.force_score();
809+ anyhow::Ok(callback)
811+ EventResult::Consumed(None)
814+ fn cursor(&self, area: Rect, ctx: &Editor) -> (Option<Position>, CursorKind) {
815+ self.file_picker.cursor(area, ctx)
818+ fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
819+ self.file_picker.required_size(viewport)
822+ fn id(&self) -> Option<&'static str> {