file_finder: Fix create wrong file in multiple worktree (#33139)

When open multiple worktree, using `file_finder` to create a new file
shoud respect current focused worktree.
test case:
```
project:
   worktree A
        file1
   worktree B
        file2 <-  focused
```
when focused `file2`, `ctrl-p` toggle `file_finder` to create `file3`
should exists in worktreeB.


I try add test case for `CreateNew` in file_finder, but found not
worked, if you help me, I can try add this test case.

Release Notes:

- Fixed file finder selecting wrong worktree when creating a file
This commit is contained in:
CharlesChen0823 2025-06-26 00:17:41 +08:00 committed by GitHub
parent 6848073c38
commit 630a326a07
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 176 additions and 7 deletions

View file

@ -881,6 +881,148 @@ async fn test_single_file_worktrees(cx: &mut TestAppContext) {
picker.update(cx, |f, _| assert_eq!(f.delegate.matches.len(), 0));
}
#[gpui::test]
async fn test_create_file_for_multiple_worktrees(cx: &mut TestAppContext) {
let app_state = init_test(cx);
app_state
.fs
.as_fake()
.insert_tree(
path!("/roota"),
json!({ "the-parent-dira": { "filea": "" } }),
)
.await;
app_state
.fs
.as_fake()
.insert_tree(
path!("/rootb"),
json!({ "the-parent-dirb": { "fileb": "" } }),
)
.await;
let project = Project::test(
app_state.fs.clone(),
[path!("/roota").as_ref(), path!("/rootb").as_ref()],
cx,
)
.await;
let (workspace, cx) = cx.add_window_view(|window, cx| Workspace::test_new(project, window, cx));
let (_worktree_id1, worktree_id2) = cx.read(|cx| {
let worktrees = workspace.read(cx).worktrees(cx).collect::<Vec<_>>();
(
WorktreeId::from_usize(worktrees[0].entity_id().as_u64() as usize),
WorktreeId::from_usize(worktrees[1].entity_id().as_u64() as usize),
)
});
let b_path = ProjectPath {
worktree_id: worktree_id2,
path: Arc::from(Path::new(path!("the-parent-dirb/fileb"))),
};
workspace
.update_in(cx, |workspace, window, cx| {
workspace.open_path(b_path, None, true, window, cx)
})
.await
.unwrap();
let finder = open_file_picker(&workspace, cx);
finder
.update_in(cx, |f, window, cx| {
f.delegate.spawn_search(
test_path_position(path!("the-parent-dirb/filec")),
window,
cx,
)
})
.await;
cx.run_until_parked();
finder.update_in(cx, |picker, window, cx| {
assert_eq!(picker.delegate.matches.len(), 1);
picker.delegate.confirm(false, window, cx)
});
cx.run_until_parked();
cx.read(|cx| {
let active_editor = workspace.read(cx).active_item_as::<Editor>(cx).unwrap();
let project_path = active_editor.read(cx).project_path(cx);
assert_eq!(
project_path,
Some(ProjectPath {
worktree_id: worktree_id2,
path: Arc::from(Path::new(path!("the-parent-dirb/filec")))
})
);
});
}
#[gpui::test]
async fn test_create_file_no_focused_with_multiple_worktrees(cx: &mut TestAppContext) {
let app_state = init_test(cx);
app_state
.fs
.as_fake()
.insert_tree(
path!("/roota"),
json!({ "the-parent-dira": { "filea": "" } }),
)
.await;
app_state
.fs
.as_fake()
.insert_tree(
path!("/rootb"),
json!({ "the-parent-dirb": { "fileb": "" } }),
)
.await;
let project = Project::test(
app_state.fs.clone(),
[path!("/roota").as_ref(), path!("/rootb").as_ref()],
cx,
)
.await;
let (workspace, cx) = cx.add_window_view(|window, cx| Workspace::test_new(project, window, cx));
let (_worktree_id1, worktree_id2) = cx.read(|cx| {
let worktrees = workspace.read(cx).worktrees(cx).collect::<Vec<_>>();
(
WorktreeId::from_usize(worktrees[0].entity_id().as_u64() as usize),
WorktreeId::from_usize(worktrees[1].entity_id().as_u64() as usize),
)
});
let finder = open_file_picker(&workspace, cx);
finder
.update_in(cx, |f, window, cx| {
f.delegate
.spawn_search(test_path_position(path!("rootb/filec")), window, cx)
})
.await;
cx.run_until_parked();
finder.update_in(cx, |picker, window, cx| {
assert_eq!(picker.delegate.matches.len(), 1);
picker.delegate.confirm(false, window, cx)
});
cx.run_until_parked();
cx.read(|cx| {
let active_editor = workspace.read(cx).active_item_as::<Editor>(cx).unwrap();
let project_path = active_editor.read(cx).project_path(cx);
assert_eq!(
project_path,
Some(ProjectPath {
worktree_id: worktree_id2,
path: Arc::from(Path::new("filec"))
})
);
});
}
#[gpui::test]
async fn test_path_distance_ordering(cx: &mut TestAppContext) {
let app_state = init_test(cx);