Allow creating entries when nothing is selected in the project panel (#29336)
Closes https://github.com/zed-industries/zed/issues/29249 Release Notes: - Allowed creating entries when nothing is selected in the project panel
This commit is contained in:
parent
c0f8e0f605
commit
fcfeea4825
2 changed files with 134 additions and 51 deletions
|
@ -1389,11 +1389,29 @@ impl ProjectPanel {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_entry(&mut self, is_dir: bool, window: &mut Window, cx: &mut Context<Self>) {
|
fn add_entry(&mut self, is_dir: bool, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
if let Some(SelectedEntry {
|
let Some((worktree_id, entry_id)) = self
|
||||||
|
.selection
|
||||||
|
.map(|entry| (entry.worktree_id, entry.entry_id))
|
||||||
|
.or_else(|| {
|
||||||
|
let entry_id = self.last_worktree_root_id?;
|
||||||
|
let worktree_id = self
|
||||||
|
.project
|
||||||
|
.read(cx)
|
||||||
|
.worktree_for_entry(entry_id, cx)?
|
||||||
|
.read(cx)
|
||||||
|
.id();
|
||||||
|
|
||||||
|
self.selection = Some(SelectedEntry {
|
||||||
worktree_id,
|
worktree_id,
|
||||||
entry_id,
|
entry_id,
|
||||||
}) = self.selection
|
});
|
||||||
{
|
|
||||||
|
Some((worktree_id, entry_id))
|
||||||
|
})
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
let directory_id;
|
let directory_id;
|
||||||
let new_entry_id = self.resolve_entry(entry_id);
|
let new_entry_id = self.resolve_entry(entry_id);
|
||||||
if let Some((worktree, expanded_dir_ids)) = self
|
if let Some((worktree, expanded_dir_ids)) = self
|
||||||
|
@ -1427,6 +1445,7 @@ impl ProjectPanel {
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
self.marked_entries.clear();
|
self.marked_entries.clear();
|
||||||
self.edit_state = Some(EditState {
|
self.edit_state = Some(EditState {
|
||||||
worktree_id,
|
worktree_id,
|
||||||
|
@ -1446,7 +1465,6 @@ impl ProjectPanel {
|
||||||
self.autoscroll(cx);
|
self.autoscroll(cx);
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn unflatten_entry_id(&self, leaf_entry_id: ProjectEntryId) -> ProjectEntryId {
|
fn unflatten_entry_id(&self, leaf_entry_id: ProjectEntryId) -> ProjectEntryId {
|
||||||
if let Some(ancestors) = self.ancestors.get(&leaf_entry_id) {
|
if let Some(ancestors) = self.ancestors.get(&leaf_entry_id) {
|
||||||
|
|
|
@ -4948,6 +4948,71 @@ async fn test_collapse_all_for_entry(cx: &mut gpui::TestAppContext) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[gpui::test]
|
||||||
|
async fn test_create_entries_without_selection(cx: &mut gpui::TestAppContext) {
|
||||||
|
init_test(cx);
|
||||||
|
|
||||||
|
let fs = FakeFs::new(cx.executor().clone());
|
||||||
|
fs.insert_tree(
|
||||||
|
path!("/root"),
|
||||||
|
json!({
|
||||||
|
"dir1": {
|
||||||
|
"file1.txt": "",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let project = Project::test(fs.clone(), [path!("/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, |workspace, window, cx| {
|
||||||
|
let panel = ProjectPanel::new(workspace, window, cx);
|
||||||
|
workspace.add_panel(panel.clone(), window, cx);
|
||||||
|
panel
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
#[rustfmt::skip]
|
||||||
|
assert_eq!(
|
||||||
|
visible_entries_as_strings(&panel, 0..20, cx),
|
||||||
|
&[
|
||||||
|
separator!("v root"),
|
||||||
|
separator!(" > dir1"),
|
||||||
|
],
|
||||||
|
"Initial state with nothing selected"
|
||||||
|
);
|
||||||
|
|
||||||
|
panel.update_in(cx, |panel, window, cx| {
|
||||||
|
panel.new_file(&NewFile, window, cx);
|
||||||
|
});
|
||||||
|
panel.update_in(cx, |panel, window, cx| {
|
||||||
|
assert!(panel.filename_editor.read(cx).is_focused(window));
|
||||||
|
});
|
||||||
|
panel
|
||||||
|
.update_in(cx, |panel, window, cx| {
|
||||||
|
panel.filename_editor.update(cx, |editor, cx| {
|
||||||
|
editor.set_text("hello_from_no_selections", window, cx)
|
||||||
|
});
|
||||||
|
panel.confirm_edit(window, cx).unwrap()
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
#[rustfmt::skip]
|
||||||
|
assert_eq!(
|
||||||
|
visible_entries_as_strings(&panel, 0..20, cx),
|
||||||
|
&[
|
||||||
|
separator!("v root"),
|
||||||
|
separator!(" > dir1"),
|
||||||
|
separator!(" hello_from_no_selections <== selected <== marked"),
|
||||||
|
],
|
||||||
|
"A new file is created under the root directory"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
fn select_path(panel: &Entity<ProjectPanel>, path: impl AsRef<Path>, cx: &mut VisualTestContext) {
|
fn select_path(panel: &Entity<ProjectPanel>, path: impl AsRef<Path>, cx: &mut VisualTestContext) {
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
panel.update(cx, |panel, cx| {
|
panel.update(cx, |panel, cx| {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue