Skip to content

Commit

Permalink
Consolidate DynamicPicker into Picker
Browse files Browse the repository at this point in the history
DynamicPicker is a thin wrapper over Picker that holds some additional
state, similar to the old FilePicker type. Like with FilePicker, we want
to fold the two types together, having Picker optionally hold that
extra state.

The DynamicPicker is a little more complicated than FilePicker was
though - it holds a query callback and current query string in state and
provides some debounce for queries using the IdleTimeout event.
We can move all of that state and debounce logic into an AsyncHook
implementation, introduced here as `DynamicQueryHandler`. The hook
receives updates to the primary query and debounces those events so
that once a query has been idle for a short time (275ms) we re-run
the query.

A standard Picker created through `new` for example can be promoted into
a Dynamic picker by chaining the new `with_dynamic_query` function, very
similar to FilePicker's replacement `with_preview`.

The workspace symbol picker has been migrated to the new way of writing
dynamic pickers as an example. The child commit will promote global
search into a dynamic Picker as well.
  • Loading branch information
the-mikedavis committed Feb 25, 2024
1 parent ea2ff77 commit aceb293
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 118 deletions.
69 changes: 50 additions & 19 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use helix_view::{
use crate::{
compositor::{self, Compositor},
job::Callback,
ui::{self, overlay::overlaid, DynamicPicker, FileLocation, Picker, Popup, PromptEvent},
ui::{self, overlay::overlaid, FileLocation, Picker, Popup, PromptEvent},
};

use std::{
Expand Down Expand Up @@ -414,6 +414,8 @@ pub fn symbol_picker(cx: &mut Context) {
}

pub fn workspace_symbol_picker(cx: &mut Context) {
use crate::ui::picker::Injector;

let doc = doc!(cx.editor);
if doc
.language_servers_with_feature(LanguageServerFeature::WorkspaceSymbols)
Expand All @@ -425,19 +427,21 @@ pub fn workspace_symbol_picker(cx: &mut Context) {
return;
}

let get_symbols = move |pattern: String, editor: &mut Editor| {
let get_symbols = |pattern: &str, editor: &mut Editor, _data, injector: &Injector<_, _>| {
let doc = doc!(editor);
let mut seen_language_servers = HashSet::new();
let mut futures: FuturesUnordered<_> = doc
.language_servers_with_feature(LanguageServerFeature::WorkspaceSymbols)
.filter(|ls| seen_language_servers.insert(ls.id()))
.map(|language_server| {
let request = language_server.workspace_symbols(pattern.clone()).unwrap();
let request = language_server
.workspace_symbols(pattern.to_string())
.unwrap();
let offset_encoding = language_server.offset_encoding();
async move {
let json = request.await?;

let response =
let response: Vec<_> =
serde_json::from_value::<Option<Vec<lsp::SymbolInformation>>>(json)?
.unwrap_or_default()
.into_iter()
Expand All @@ -456,29 +460,56 @@ pub fn workspace_symbol_picker(cx: &mut Context) {
editor.set_error("No configured language server supports workspace symbols");
}

let injector = injector.clone();
async move {
let mut symbols = Vec::new();
// TODO if one symbol request errors, all other requests are discarded (even if they're valid)
while let Some(mut lsp_items) = futures.try_next().await? {
symbols.append(&mut lsp_items);
while let Some(lsp_items) = futures.try_next().await? {
for item in lsp_items {
injector.push(item)?;
}
}
anyhow::Ok(symbols)
Ok(())
}
.boxed()
};
let columns = vec![
ui::PickerColumn::new("kind", |item: &SymbolInformationItem, _| {
display_symbol_kind(item.symbol.kind).into()
}),
ui::PickerColumn::new("name", |item: &SymbolInformationItem, _| {
item.symbol.name.as_str().into()
})
.without_filtering(),
ui::PickerColumn::new("path", |item: &SymbolInformationItem, _| {
match item.symbol.location.uri.to_file_path() {
Ok(path) => path::get_relative_path(path.as_path())
.to_string_lossy()
.to_string()
.into(),
Err(_) => item.symbol.location.uri.to_string().into(),
}
}),
];

let initial_symbols = get_symbols("".to_owned(), cx.editor);

cx.jobs.callback(async move {
let symbols = initial_symbols.await?;
let call = move |_editor: &mut Editor, compositor: &mut Compositor| {
let picker = sym_picker(symbols, true);
let dyn_picker = DynamicPicker::new(picker, Box::new(get_symbols));
compositor.push(Box::new(overlaid(dyn_picker)))
};
let picker = Picker::new(
columns,
1, // name column
vec![],
(),
move |cx, item, action| {
jump_to_location(
cx.editor,
&item.symbol.location,
item.offset_encoding,
action,
);
},
)
.with_preview(|_editor, item| Some(location_to_file_location(&item.symbol.location)))
.with_dynamic_query(get_symbols)
.truncate_start(false);

Ok(Callback::EditorCompositor(Box::new(call)))
});
cx.push_layer(Box::new(overlaid(picker)));
}

pub fn diagnostics_picker(cx: &mut Context) {
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use completion::{Completion, CompletionItem};
pub use editor::EditorView;
pub use markdown::Markdown;
pub use menu::Menu;
pub use picker::{Column as PickerColumn, DynamicPicker, FileLocation, Picker};
pub use picker::{Column as PickerColumn, FileLocation, Picker};
pub use popup::Popup;
pub use prompt::{Prompt, PromptEvent};
pub use spinner::{ProgressSpinners, Spinner};
Expand Down
104 changes: 21 additions & 83 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ mod query;
use crate::{
alt,
compositor::{self, Component, Compositor, Context, Event, EventResult},
ctrl,
job::Callback,
key, shift,
ctrl, key, shift,
ui::{
self,
document::{render_document, LineDecoration, LinePos, TextRenderer},
Expand Down Expand Up @@ -52,8 +50,6 @@ use helix_view::{

pub const ID: &str = "picker";

use super::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;
Expand Down Expand Up @@ -223,6 +219,11 @@ impl<T, D> Column<T, D> {
}
}

/// 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 struct Picker<T: 'static + Send + Sync, D: 'static> {
column_names: Vec<&'static str>,
columns: Arc<Vec<Column<T, D>>>,
Expand Down Expand Up @@ -253,6 +254,7 @@ pub struct Picker<T: 'static + Send + Sync, D: 'static> {
file_fn: Option<FileCallback<T>>,
/// An event handler for syntax highlighting the currently previewed file.
preview_highlight_handler: tokio::sync::mpsc::Sender<Arc<Path>>,
dynamic_query_handler: Option<tokio::sync::mpsc::Sender<Arc<str>>>,
}

impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
Expand Down Expand Up @@ -362,6 +364,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
read_buffer: Vec::with_capacity(1024),
file_fn: None,
preview_highlight_handler: handlers::PreviewHighlightHandler::<T, D>::default().spawn(),
dynamic_query_handler: None,
}
}

Expand Down Expand Up @@ -396,12 +399,11 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
self
}

pub fn set_options(&mut self, new_options: Vec<T>) {
self.matcher.restart(false);
let injector = self.matcher.injector();
for item in new_options {
inject_nucleo_item(&injector, &self.columns, item, &self.editor_data);
}
pub fn with_dynamic_query(mut self, callback: DynQueryCallback<T, D>) -> Self {
let handler = handlers::DynamicQueryHandler::new(callback).spawn();
helix_event::send_blocking(&handler, self.primary_query());
self.dynamic_query_handler = Some(handler);
self
}

/// Move the cursor by a number of lines, either down (`Forward`) or up (`Backward`)
Expand Down Expand Up @@ -503,6 +505,9 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
);
}
self.query = new_query;
if let Some(handler) = &self.dynamic_query_handler {
helix_event::send_blocking(handler, self.primary_query());
}
}
}
EventResult::Consumed(None)
Expand Down Expand Up @@ -614,7 +619,11 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {

let count = format!(
"{}{}/{}",
if status.running { "(running) " } else { "" },
if status.running || self.matcher.active_injectors() > 0 {
"(running) "
} else {
""
},
snapshot.matched_item_count(),
snapshot.item_count(),
);
Expand Down Expand Up @@ -1014,74 +1023,3 @@ impl<T: 'static + Send + Sync, D> Drop for Picker<T, D> {
}

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>>>>;

/// 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: 'static + Send + Sync, D: 'static + Send + Sync> {
file_picker: Picker<T, D>,
query_callback: DynQueryCallback<T>,
query: String,
}

impl<T: Send + Sync, D: Send + Sync> DynamicPicker<T, D> {
pub fn new(file_picker: Picker<T, D>, query_callback: DynQueryCallback<T>) -> Self {
Self {
file_picker,
query_callback,
query: String::new(),
}
}
}

impl<T: Send + Sync + 'static, D: Send + Sync + 'static> Component for DynamicPicker<T, D> {
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 Some(current_query) = self.file_picker.primary_query() else {
return event_result;
};

if !matches!(event, Event::IdleTimeout) || self.query == *current_query {
return event_result;
}

self.query = current_query.to_string();

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<Self>>(ID) {
Some(overlay) => &mut overlay.content.file_picker,
None => return,
};
picker.set_options(new_options);
}));
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(ID)
}
}
83 changes: 68 additions & 15 deletions helix-term/src/ui/picker/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use std::{path::Path, sync::Arc, time::Duration};
use std::{
path::Path,
sync::{atomic, Arc},
time::Duration,
};

use helix_event::AsyncHook;
use tokio::time::Instant;

use crate::{job, ui::overlay::Overlay};

use super::{CachedPreview, DynamicPicker, Picker};
use super::{CachedPreview, DynQueryCallback, Picker};

pub(super) struct PreviewHighlightHandler<T: 'static + Send + Sync, D: 'static + Send + Sync> {
trigger: Option<Arc<Path>>,
Expand Down Expand Up @@ -48,12 +52,8 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> AsyncHook
let Some(path) = self.trigger.take() else { return };

job::dispatch_blocking(move |editor, compositor| {
let picker = match compositor.find::<Overlay<Picker<T, D>>>() {
Some(Overlay { content, .. }) => content,
None => match compositor.find::<Overlay<DynamicPicker<T, D>>>() {
Some(Overlay { content, .. }) => &mut content.file_picker,
None => return,
},
let Some(Overlay { content: picker, .. }) = compositor.find::<Overlay<Picker<T, D>>>() else {
return;
};

let Some(CachedPreview::Document(ref mut doc)) = picker.preview_cache.get_mut(&path) else {
Expand Down Expand Up @@ -85,13 +85,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> AsyncHook
};

job::dispatch_blocking(move |editor, compositor| {
let picker = match compositor.find::<Overlay<Picker<T, D>>>() {
Some(Overlay { content, .. }) => Some(content),
None => compositor
.find::<Overlay<DynamicPicker<T, D>>>()
.map(|overlay| &mut overlay.content.file_picker),
};
let Some(picker) = picker else {
let Some(Overlay { content: picker, .. }) = compositor.find::<Overlay<Picker<T, D>>>() else {
log::info!("picker closed before syntax highlighting finished");
return;
};
Expand All @@ -110,3 +104,62 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> AsyncHook
});
}
}

pub(super) struct DynamicQueryHandler<T: 'static + Send + Sync, D: 'static + Send + Sync> {
callback: Arc<DynQueryCallback<T, D>>,
last_query: Arc<str>,
query: Option<Arc<str>>,
}

impl<T: 'static + Send + Sync, D: 'static + Send + Sync> DynamicQueryHandler<T, D> {
pub(super) fn new(callback: DynQueryCallback<T, D>) -> Self {
Self {
callback: Arc::new(callback),
last_query: "".into(),
query: None,
}
}
}

impl<T: 'static + Send + Sync, D: 'static + Send + Sync> AsyncHook for DynamicQueryHandler<T, D> {
type Event = Arc<str>;

fn handle_event(&mut self, query: Self::Event, _timeout: Option<Instant>) -> Option<Instant> {
if query == self.last_query {
// If the search query reverts to the last one we requested, no need to
// make a new request.
self.query = None;
None
} else {
self.query = Some(query);
Some(Instant::now() + Duration::from_millis(275))
}
}

fn finish_debounce(&mut self) {
let Some(query) = self.query.take() else { return };
self.last_query = query.clone();
let callback = self.callback.clone();

job::dispatch_blocking(move |editor, compositor| {
let Some(Overlay { content: picker, .. }) = compositor.find::<Overlay<Picker<T, D>>>() else {
return;
};
// Increment the version number to cancel any ongoing requests.
picker.version.fetch_add(1, atomic::Ordering::Relaxed);
picker.matcher.restart(false);
let injector = picker.injector();
let get_options = (callback)(&query, editor, picker.editor_data.clone(), &injector);
tokio::spawn(async move {
if let Err(err) = get_options.await {
log::info!("Dynamic request failed: {err}");
}
// The picker's shows its running indicator when there are any active
// injectors. When we're done injecting new options, drop the injector
// and request a redraw to remove the running indicator.
drop(injector);
helix_event::request_redraw();
});
})
}
}

0 comments on commit aceb293

Please sign in to comment.