@@ -3,6 +3,7 @@ use crate::{
33 ctrl, key, shift,
44 ui::{self, fuzzy_match::FuzzyQuery, EditorView},
55};
6+use futures_util::future::BoxFuture;
67use tui::{
78 buffer::Buffer as Surface,
89 widgets::{Block, BorderType, Borders},
@@ -22,7 +23,7 @@ use helix_view::{
2223 Document, DocumentId, Editor,
2324};
2425
25−use super::menu::Item;
26+use super::{menu::Item, overlay::Overlay};
2627
2728pub const MIN_AREA_WIDTH_FOR_PREVIEW: u16 = 72;
2829/// Biggest file size to preview in bytes
@@ -751,3 +752,74 @@ impl<T: Item + 'static> Component for Picker<T> {
751752 self.prompt.cursor(area, editor)
752753 }
753754}
755+
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>>>>;
760+
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>,
766+}
767+
768+impl<T: ui::menu::Item + Send> DynamicPicker<T> {
769+ pub const ID: &'static str = "dynamic-picker";
770+
771+ pub fn new(file_picker: FilePicker<T>, query_callback: DynQueryCallback<T>) -> Self {
772+ Self {
773+ file_picker,
774+ query_callback,
775+ }
776+ }
777+}
778+
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);
782+ }
783+
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();
788+
789+ if *current_query == prev_query || matches!(event_result, EventResult::Ignored(_)) {
790+ return event_result;
791+ }
792+
793+ let new_options = (self.query_callback)(current_query.to_owned(), cx.editor);
794+
795+ cx.jobs.callback(async move {
796+ let new_options = new_options.await?;
797+ let callback =
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,
803+ None => return,
804+ };
805+ picker.options = new_options;
806+ picker.cursor = 0;
807+ picker.force_score();
808+ }));
809+ anyhow::Ok(callback)
810+ });
811+ EventResult::Consumed(None)
812+ }
813+
814+ fn cursor(&self, area: Rect, ctx: &Editor) -> (Option<Position>, CursorKind) {
815+ self.file_picker.cursor(area, ctx)
816+ }
817+
818+ fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
819+ self.file_picker.required_size(viewport)
820+ }
821+
822+ fn id(&self) -> Option<&'static str> {
823+ Some(Self::ID)
824+ }
825+}