project_panel: Make scroll to bottom to use the last visible entry (#25160)

Closes #25159

Release Notes:

- Fixed project panel implementation of the `menu::SelectLast` action
This commit is contained in:
PJ Tatlow 2025-02-19 09:00:23 -07:00 committed by GitHub
parent ddec72ecbe
commit b6fd06d63c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1942,22 +1942,22 @@ impl ProjectPanel {
} }
fn select_last(&mut self, _: &SelectLast, _: &mut Window, cx: &mut Context<Self>) { fn select_last(&mut self, _: &SelectLast, _: &mut Window, cx: &mut Context<Self>) {
let worktree = self.visible_entries.last().and_then(|(worktree_id, _, _)| { if let Some((worktree_id, visible_worktree_entries, _)) = self.visible_entries.last() {
self.project.read(cx).worktree_for_id(*worktree_id, cx) let worktree = self.project.read(cx).worktree_for_id(*worktree_id, cx);
}); if let (Some(worktree), Some(entry)) = (worktree, visible_worktree_entries.last()) {
if let Some(worktree) = worktree {
let worktree = worktree.read(cx); let worktree = worktree.read(cx);
let worktree_id = worktree.id(); if let Some(entry) = worktree.entry_for_id(entry.id) {
if let Some(last_entry) = worktree.entries(true, 0).last() { let selection = SelectedEntry {
self.selection = Some(SelectedEntry { worktree_id: *worktree_id,
worktree_id, entry_id: entry.id,
entry_id: last_entry.id, };
}); self.selection = Some(selection);
self.autoscroll(cx); self.autoscroll(cx);
cx.notify(); cx.notify();
} }
} }
} }
}
fn autoscroll(&mut self, cx: &mut Context<Self>) { fn autoscroll(&mut self, cx: &mut Context<Self>) {
if let Some((_, _, index)) = self.selection.and_then(|s| self.index_for_selection(s)) { if let Some((_, _, index)) = self.selection.and_then(|s| self.index_for_selection(s)) {
@ -7124,6 +7124,76 @@ mod tests {
] ]
); );
} }
#[gpui::test]
async fn test_select_first_last(cx: &mut gpui::TestAppContext) {
init_test_with_editor(cx);
let fs = FakeFs::new(cx.executor().clone());
fs.insert_tree(
"/project_root",
json!({
"dir_1": {
"nested_dir": {
"file_a.py": "# File contents",
}
},
"file_1.py": "# File contents",
"file_2.py": "# File contents",
"zdir_2": {
"nested_dir2": {
"file_b.py": "# File contents",
}
},
}),
)
.await;
let project = Project::test(fs.clone(), ["/project_root".as_ref()], cx).await;
let workspace =
cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
let cx = &mut VisualTestContext::from_window(*workspace, cx);
let panel = workspace.update(cx, ProjectPanel::new).unwrap();
assert_eq!(
visible_entries_as_strings(&panel, 0..10, cx),
&[
"v project_root",
" > dir_1",
" > zdir_2",
" file_1.py",
" file_2.py",
]
);
panel.update_in(cx, |panel, window, cx| {
panel.select_first(&SelectFirst, window, cx)
});
assert_eq!(
visible_entries_as_strings(&panel, 0..10, cx),
&[
"v project_root <== selected",
" > dir_1",
" > zdir_2",
" file_1.py",
" file_2.py",
]
);
panel.update_in(cx, |panel, window, cx| {
panel.select_last(&SelectLast, window, cx)
});
assert_eq!(
visible_entries_as_strings(&panel, 0..10, cx),
&[
"v project_root",
" > dir_1",
" > zdir_2",
" file_1.py",
" file_2.py <== selected",
]
);
}
#[gpui::test] #[gpui::test]
async fn test_dir_toggle_collapse(cx: &mut gpui::TestAppContext) { async fn test_dir_toggle_collapse(cx: &mut gpui::TestAppContext) {