Maintain recently opened files history
This commit is contained in:
parent
bd29812391
commit
711d2c6fe7
2 changed files with 151 additions and 3 deletions
|
@ -239,6 +239,13 @@ impl PickerDelegate for FileFinderDelegate {
|
||||||
if raw_query.is_empty() {
|
if raw_query.is_empty() {
|
||||||
self.latest_search_id = post_inc(&mut self.search_count);
|
self.latest_search_id = post_inc(&mut self.search_count);
|
||||||
self.matches.clear();
|
self.matches.clear();
|
||||||
|
self.matches = self
|
||||||
|
.project
|
||||||
|
.read(cx)
|
||||||
|
.search_panel_state()
|
||||||
|
.recent_selections()
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
cx.notify();
|
cx.notify();
|
||||||
Task::ready(())
|
Task::ready(())
|
||||||
} else {
|
} else {
|
||||||
|
@ -261,11 +268,14 @@ impl PickerDelegate for FileFinderDelegate {
|
||||||
fn confirm(&mut self, cx: &mut ViewContext<FileFinder>) {
|
fn confirm(&mut self, cx: &mut ViewContext<FileFinder>) {
|
||||||
if let Some(m) = self.matches.get(self.selected_index()) {
|
if let Some(m) = self.matches.get(self.selected_index()) {
|
||||||
if let Some(workspace) = self.workspace.upgrade(cx) {
|
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 {
|
let project_path = ProjectPath {
|
||||||
worktree_id: WorktreeId::from_usize(m.worktree_id),
|
worktree_id: WorktreeId::from_usize(m.worktree_id),
|
||||||
path: m.path.clone(),
|
path: m.path.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let open_task = workspace.update(cx, |workspace, cx| {
|
let open_task = workspace.update(cx, |workspace, cx| {
|
||||||
workspace.open_path(project_path.clone(), None, true, cx)
|
workspace.open_path(project_path.clone(), None, true, cx)
|
||||||
});
|
});
|
||||||
|
@ -301,7 +311,6 @@ impl PickerDelegate for FileFinderDelegate {
|
||||||
.log_err();
|
.log_err();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
workspace
|
workspace
|
||||||
.update(&mut cx, |workspace, cx| workspace.dismiss_modal(cx))
|
.update(&mut cx, |workspace, cx| workspace.dismiss_modal(cx))
|
||||||
.log_err();
|
.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::<FileFinder>().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::<FileFinder>().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::<Vec<_>>();
|
||||||
|
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::<FileFinder>().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::<Vec<_>>();
|
||||||
|
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<AppState> {
|
fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
|
||||||
cx.foreground().forbid_parking();
|
cx.foreground().forbid_parking();
|
||||||
cx.update(|cx| {
|
cx.update(|cx| {
|
||||||
|
|
|
@ -12,13 +12,14 @@ mod project_tests;
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use client::{proto, Client, TypedEnvelope, UserStore};
|
use client::{proto, Client, TypedEnvelope, UserStore};
|
||||||
use clock::ReplicaId;
|
use clock::ReplicaId;
|
||||||
use collections::{hash_map, BTreeMap, HashMap, HashSet};
|
use collections::{hash_map, BTreeMap, HashMap, HashSet, VecDeque};
|
||||||
use copilot::Copilot;
|
use copilot::Copilot;
|
||||||
use futures::{
|
use futures::{
|
||||||
channel::mpsc::{self, UnboundedReceiver},
|
channel::mpsc::{self, UnboundedReceiver},
|
||||||
future::{try_join_all, Shared},
|
future::{try_join_all, Shared},
|
||||||
AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt,
|
AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt,
|
||||||
};
|
};
|
||||||
|
use fuzzy::PathMatch;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
AnyModelHandle, AppContext, AsyncAppContext, BorrowAppContext, Entity, ModelContext,
|
AnyModelHandle, AppContext, AsyncAppContext, BorrowAppContext, Entity, ModelContext,
|
||||||
ModelHandle, Task, WeakModelHandle,
|
ModelHandle, Task, WeakModelHandle,
|
||||||
|
@ -135,6 +136,7 @@ pub struct Project {
|
||||||
_maintain_workspace_config: Task<()>,
|
_maintain_workspace_config: Task<()>,
|
||||||
terminals: Terminals,
|
terminals: Terminals,
|
||||||
copilot_enabled: bool,
|
copilot_enabled: bool,
|
||||||
|
search_panel_state: SearchPanelState,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct LspBufferSnapshot {
|
struct LspBufferSnapshot {
|
||||||
|
@ -388,6 +390,42 @@ impl FormatTrigger {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MAX_RECENT_SELECTIONS: usize = 20;
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct SearchPanelState {
|
||||||
|
recent_selections: VecDeque<PathMatch>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SearchPanelState {
|
||||||
|
pub fn recent_selections(&self) -> impl Iterator<Item = &PathMatch> {
|
||||||
|
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 {
|
impl Project {
|
||||||
pub fn init_settings(cx: &mut AppContext) {
|
pub fn init_settings(cx: &mut AppContext) {
|
||||||
settings::register::<ProjectSettings>(cx);
|
settings::register::<ProjectSettings>(cx);
|
||||||
|
@ -487,6 +525,7 @@ impl Project {
|
||||||
local_handles: Vec::new(),
|
local_handles: Vec::new(),
|
||||||
},
|
},
|
||||||
copilot_enabled: Copilot::global(cx).is_some(),
|
copilot_enabled: Copilot::global(cx).is_some(),
|
||||||
|
search_panel_state: SearchPanelState::default(),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -577,6 +616,7 @@ impl Project {
|
||||||
local_handles: Vec::new(),
|
local_handles: Vec::new(),
|
||||||
},
|
},
|
||||||
copilot_enabled: Copilot::global(cx).is_some(),
|
copilot_enabled: Copilot::global(cx).is_some(),
|
||||||
|
search_panel_state: SearchPanelState::default(),
|
||||||
};
|
};
|
||||||
for worktree in worktrees {
|
for worktree in worktrees {
|
||||||
let _ = this.add_worktree(&worktree, cx);
|
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(
|
fn primary_language_servers_for_buffer(
|
||||||
&self,
|
&self,
|
||||||
buffer: &Buffer,
|
buffer: &Buffer,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue