Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request workspace symbols on idle-timeout #4

Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,15 @@ impl<T: Item> FilePicker<T> {
}

fn handle_idle_timeout(&mut self, cx: &mut Context) -> EventResult {
self.highlight_current_file(cx.editor);

EventResult::Consumed(None)
}

pub fn highlight_current_file(&mut self, editor: &Editor) {
// Try to find a document in the cache
let doc = self
.current_file(cx.editor)
.current_file(editor)
.and_then(|(path, _range)| self.preview_cache.get_mut(&path))
.and_then(|cache| match cache {
CachedPreview::Document(doc) => Some(doc),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use an early return here and drop the Option wrapping

Expand All @@ -175,12 +181,10 @@ impl<T: Item> FilePicker<T> {
// 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();
let loader = editor.syn_loader.clone();
doc.detect_language(loader);
}
}

EventResult::Consumed(None)
}
}

Expand Down Expand Up @@ -696,6 +700,7 @@ pub type DynQueryCallback<T> =
pub struct DynamicPicker<T: ui::menu::Item + Send> {
file_picker: FilePicker<T>,
query_callback: DynQueryCallback<T>,
query: String,
}

impl<T: ui::menu::Item + Send> DynamicPicker<T> {
Expand All @@ -705,6 +710,7 @@ impl<T: ui::menu::Item + Send> DynamicPicker<T> {
Self {
file_picker,
query_callback,
query: String::new(),
}
}
}
Expand All @@ -715,28 +721,33 @@ impl<T: Item + Send + 'static> Component for DynamicPicker<T> {
}

fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
let prev_query = self.file_picker.picker.prompt.line().to_owned();
let event_result = self.file_picker.handle_event(event, cx);
let current_query = self.file_picker.picker.prompt.line();

if *current_query == prev_query || matches!(event_result, EventResult::Ignored(_)) {
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: crate::job::Callback = Box::new(move |_editor, compositor| {
let callback: crate::job::Callback = 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>>>(Self::ID) {
Some(overlay) => &mut overlay.content.file_picker.picker,
let file_picker = match compositor.find_id::<Overlay<DynamicPicker<T>>>(Self::ID) {
Some(overlay) => &mut overlay.content.file_picker,
None => return,
};
picker.options = new_options;
picker.cursor = 0;
picker.force_score();
file_picker.picker.options = new_options;
file_picker.picker.cursor = 0;
file_picker.picker.force_score();
// This callback is triggered on the idle timeout, so we simulate the
// file-picker's handle_idle_timeout behavior by highlighting the
// currently previewed file.
file_picker.highlight_current_file(editor);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed here? Isn't the highlighting already handled by calling file_picker.handle_event() at the beginning of the function, which calls handle_idle_timeout()?

https://github.com/helix-editor/helix/blob/26f21da531179ccc77e70b73e32a1b458c4e225b/helix-term/src/ui/picker.rs#L289-L292

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the new symbols response triggered by the idle timeout selects a new file then it won't be highlighted. I just noticed that it doesn't work anyways so I'll remove this 😅

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(for example searching for something specific like "flip" in the helix repo)

});
anyhow::Ok(callback)
});
Expand Down