Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
buffer_store: Fix path_to_buffer_id never update when move action occurs in remote mode (#64005)
xiaoff290cbcb9cb6a
diff --git a/crates/project/src/buffer_store.rs b/crates/project/src/buffer_store.rsindex b9fb887d499..3f96a1da968 100644--- a/crates/project/src/buffer_store.rs+++ b/crates/project/src/buffer_store.rs@@ -1404,7 +1404,21 @@ impl BufferStore {let new_path = file.path.clone();buffer.file_updated(Arc::new(file), cx);- if old_file.as_ref().is_none_or(|old| *old.path() != new_path) {+ if old_file.as_ref().is_none_or(|old| {+ if *old.path() == new_path {+ return false;+ }++ let old_path = ProjectPath {+ worktree_id: old.worktree_id(cx),+ path: old.path().clone(),+ };++ if this.path_to_buffer_id.get(&old_path) == Some(&buffer_id) {+ this.path_to_buffer_id.remove(&old_path);+ }+ true+ }) {Some(old_file)} else {Nonediff --git a/crates/remote_server/src/remote_editing_tests.rs b/crates/remote_server/src/remote_editing_tests.rsindex e7d0273cffb..ff679f47a5c 100644--- a/crates/remote_server/src/remote_editing_tests.rs+++ b/crates/remote_server/src/remote_editing_tests.rs@@ -184,6 +184,36 @@ async fn test_basic_remote_editing(cx: &mut TestAppContext, server_cx: &mut Testbuffer.update(cx, |buffer, _| {assert_eq!(&**buffer.file().unwrap().path(), rel_path("src/lib2.rs"));});+ let renamed_buffer = project+ .update(cx, |project, cx| {+ project.open_buffer((worktree_id, rel_path("src/lib2.rs")), cx)+ })+ .await+ .unwrap();+ assert_eq!(renamed_buffer, buffer);++ fs.insert_file(+ path!("/code/project1/src/lib.rs"),+ b"fn two() -> usize { 2 }".to_vec(),+ )+ .await;+ cx.run_until_parked();++ let recreated_buffer = project+ .update(cx, |project, cx| {+ project.open_buffer((worktree_id, rel_path("src/lib.rs")), cx)+ })+ .await+ .unwrap();+ assert_ne!(recreated_buffer, buffer);+ recreated_buffer.read_with(cx, |buffer, _| {+ assert_eq!(&**buffer.file().unwrap().path(), rel_path("src/lib.rs"));+ assert_eq!(buffer.text(), "fn two() -> usize { 2 }");+ });+ buffer.read_with(cx, |buffer, _| {+ assert_eq!(&**buffer.file().unwrap().path(), rel_path("src/lib2.rs"));+ assert_eq!(buffer.text(), "fn one() -> usize { 100 }");+ });fs.set_index_for_repo(Path::new(path!("/code/project1/.git")),@@ -198,6 +228,68 @@ async fn test_basic_remote_editing(cx: &mut TestAppContext, server_cx: &mut Test});}+#[gpui::test]+async fn test_remote_buffer_path_swap(cx: &mut TestAppContext, server_cx: &mut TestAppContext) {+ let fs = FakeFs::new(server_cx.executor());+ fs.insert_tree(+ path!("/code/project"),+ json!({ "a.txt": "first", "b.txt": "second" }),+ )+ .await;+ let (project, headless) = init_test(&fs, cx, server_cx).await;+ let session = headless.read_with(server_cx, |headless, _| headless.session.clone());+ let (worktree, _) = project+ .update(cx, |project, cx| {+ project.find_or_create_worktree(path!("/code/project"), true, cx)+ })+ .await+ .unwrap();+ let worktree_id = worktree.read_with(cx, |worktree, _| worktree.id());+ let first_buffer = project+ .update(cx, |project, cx| {+ project.open_buffer((worktree_id, rel_path("a.txt")), cx)+ })+ .await+ .unwrap();+ let second_buffer = project+ .update(cx, |project, cx| {+ project.open_buffer((worktree_id, rel_path("b.txt")), cx)+ })+ .await+ .unwrap();+ cx.run_until_parked();++ for updates in [+ [(&first_buffer, "b.txt"), (&second_buffer, "a.txt")],+ [(&second_buffer, "b.txt"), (&first_buffer, "a.txt")],+ ] {+ for (buffer, path) in updates {+ let message = buffer.read_with(cx, |buffer, cx| {+ let mut file = buffer.file().unwrap().to_proto(cx);+ file.path = path.to_owned();+ proto::UpdateBufferFile {+ project_id: proto::REMOTE_SERVER_PROJECT_ID,+ buffer_id: buffer.remote_id().to_proto(),+ file: Some(file),+ }+ });+ session.send(message).unwrap();+ cx.run_until_parked();+ }+ for (buffer, path) in updates {+ buffer.read_with(cx, |buffer, _| {+ assert_eq!(&**buffer.file().unwrap().path(), rel_path(path));+ });+ project.read_with(cx, |project, cx| {+ assert_eq!(+ project.get_open_buffer(&(worktree_id, rel_path(path)).into(), cx),+ Some(buffer.clone())+ );+ });+ }+ }+}+#[gpui::test]async fn test_remote_telemetry_event_forwarding(cx: &mut TestAppContext,