diff --git a/crates/file_finder/src/file_finder.rs b/crates/file_finder/src/file_finder.rs index b318f1d167..311882bd8e 100644 --- a/crates/file_finder/src/file_finder.rs +++ b/crates/file_finder/src/file_finder.rs @@ -239,6 +239,13 @@ impl PickerDelegate for FileFinderDelegate { if raw_query.is_empty() { self.latest_search_id = post_inc(&mut self.search_count); self.matches.clear(); + self.matches = self + .project + .read(cx) + .search_panel_state() + .recent_selections() + .cloned() + .collect(); cx.notify(); Task::ready(()) } else { @@ -261,11 +268,14 @@ impl PickerDelegate for FileFinderDelegate { fn confirm(&mut self, cx: &mut ViewContext) { if let Some(m) = self.matches.get(self.selected_index()) { if let Some(workspace) = self.workspace.upgrade(cx) { + self.project.update(cx, |project, _cx| { + project.update_search_panel_state().add_selection(m.clone()) + }); + let project_path = ProjectPath { worktree_id: WorktreeId::from_usize(m.worktree_id), path: m.path.clone(), }; - let open_task = workspace.update(cx, |workspace, cx| { workspace.open_path(project_path.clone(), None, true, cx) }); @@ -301,7 +311,6 @@ impl PickerDelegate for FileFinderDelegate { .log_err(); } } - workspace .update(&mut cx, |workspace, cx| workspace.dismiss_modal(cx)) .log_err(); @@ -904,6 +913,97 @@ mod tests { }); } + #[gpui::test] + async fn test_query_history(cx: &mut gpui::TestAppContext) { + let app_state = init_test(cx); + + app_state + .fs + .as_fake() + .insert_tree( + "/src", + json!({ + "test": { + "first.rs": "// First Rust file", + "second.rs": "// Second Rust file", + "third.rs": "// Third Rust file", + } + }), + ) + .await; + + let project = Project::test(app_state.fs.clone(), ["/src".as_ref()], cx).await; + let (window_id, workspace) = cx.add_window(|cx| Workspace::test_new(project, cx)); + cx.dispatch_action(window_id, Toggle); + let finder = cx.read(|cx| workspace.read(cx).modal::().unwrap()); + + finder + .update(cx, |finder, cx| { + finder.delegate_mut().update_matches("fir".to_string(), cx) + }) + .await; + cx.dispatch_action(window_id, SelectNext); + cx.dispatch_action(window_id, Confirm); + + cx.dispatch_action(window_id, Toggle); + let finder = cx.read(|cx| workspace.read(cx).modal::().unwrap()); + finder + .update(cx, |finder, cx| { + finder.delegate_mut().update_matches("sec".to_string(), cx) + }) + .await; + cx.dispatch_action(window_id, SelectNext); + cx.dispatch_action(window_id, Confirm); + + finder.read_with(cx, |finder, cx| { + let recent_query_paths = finder + .delegate() + .project + .read(cx) + .search_panel_state() + .recent_selections() + .map(|query| query.path.to_path_buf()) + .collect::>(); + assert_eq!( + vec![ + Path::new("test/second.rs").to_path_buf(), + Path::new("test/first.rs").to_path_buf(), + ], + recent_query_paths, + "Two finder queries should produce only two recent queries. Second query should be more recent (first)" + ) + }); + + cx.dispatch_action(window_id, Toggle); + let finder = cx.read(|cx| workspace.read(cx).modal::().unwrap()); + finder + .update(cx, |finder, cx| { + finder.delegate_mut().update_matches("fir".to_string(), cx) + }) + .await; + cx.dispatch_action(window_id, SelectNext); + cx.dispatch_action(window_id, Confirm); + + finder.read_with(cx, |finder, cx| { + let recent_query_paths = finder + .delegate() + .project + .read(cx) + .search_panel_state() + .recent_selections() + .map(|query| query.path.to_path_buf()) + .collect::>(); + assert_eq!( + vec![ + Path::new("test/first.rs").to_path_buf(), + Path::new("test/second.rs").to_path_buf(), + ], + recent_query_paths, + "Three finder queries on two different files should produce only two recent queries. First query should be more recent (first), since got queried again" + ) + }); + } + fn init_test(cx: &mut TestAppContext) -> Arc { cx.foreground().forbid_parking(); cx.update(|cx| { diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index 13809622f9..f19ef44644 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -12,13 +12,14 @@ mod project_tests; use anyhow::{anyhow, Context, Result}; use client::{proto, Client, TypedEnvelope, UserStore}; use clock::ReplicaId; -use collections::{hash_map, BTreeMap, HashMap, HashSet}; +use collections::{hash_map, BTreeMap, HashMap, HashSet, VecDeque}; use copilot::Copilot; use futures::{ channel::mpsc::{self, UnboundedReceiver}, future::{try_join_all, Shared}, AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt, }; +use fuzzy::PathMatch; use gpui::{ AnyModelHandle, AppContext, AsyncAppContext, BorrowAppContext, Entity, ModelContext, ModelHandle, Task, WeakModelHandle, @@ -135,6 +136,7 @@ pub struct Project { _maintain_workspace_config: Task<()>, terminals: Terminals, copilot_enabled: bool, + search_panel_state: SearchPanelState, } struct LspBufferSnapshot { @@ -388,6 +390,42 @@ impl FormatTrigger { } } +const MAX_RECENT_SELECTIONS: usize = 20; + +#[derive(Debug, Default)] +pub struct SearchPanelState { + recent_selections: VecDeque, +} + +impl SearchPanelState { + pub fn recent_selections(&self) -> impl Iterator { + self.recent_selections.iter().rev() + } + + pub fn add_selection(&mut self, mut new_selection: PathMatch) { + let old_len = self.recent_selections.len(); + + // remove `new_selection` element, if it's in the list + self.recent_selections.retain(|old_selection| { + old_selection.worktree_id != new_selection.worktree_id + || old_selection.path != new_selection.path + }); + // if `new_selection` was not present and we're adding a new element, + // ensure we do not exceed max allowed elements + if self.recent_selections.len() == old_len { + if self.recent_selections.len() >= MAX_RECENT_SELECTIONS { + self.recent_selections.pop_front(); + } + } + + // do not highlight query matches in the selection + new_selection.positions.clear(); + // always re-add the element even if it exists to the back + // this way, it gets to the top as the most recently selected element + self.recent_selections.push_back(new_selection); + } +} + impl Project { pub fn init_settings(cx: &mut AppContext) { settings::register::(cx); @@ -487,6 +525,7 @@ impl Project { local_handles: Vec::new(), }, copilot_enabled: Copilot::global(cx).is_some(), + search_panel_state: SearchPanelState::default(), } }) } @@ -577,6 +616,7 @@ impl Project { local_handles: Vec::new(), }, copilot_enabled: Copilot::global(cx).is_some(), + search_panel_state: SearchPanelState::default(), }; for worktree in worktrees { let _ = this.add_worktree(&worktree, cx); @@ -6435,6 +6475,14 @@ impl Project { }) } + pub fn search_panel_state(&self) -> &SearchPanelState { + &self.search_panel_state + } + + pub fn update_search_panel_state(&mut self) -> &mut SearchPanelState { + &mut self.search_panel_state + } + fn primary_language_servers_for_buffer( &self, buffer: &Buffer,