Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

project: Keep the buffer associated after an LSP rename also renames the file (#61142)

Toru Nayukifdf5de99c645
diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rs
index e5eb2c0da5b..58a2c12a532 100644
--- a/crates/project/src/lsp_store.rs
+++ b/crates/project/src/lsp_store.rs
@@ -3547,6 +3547,35 @@ impl LocalLspStore {
fs.rename(&source_abs_path, &target_abs_path, options)
.await?;
+
+ // Preserve the entry id across the rename so an open buffer follows it
+ // to the new path, instead of being stranded at the old path when the
+ // filesystem watcher reports the deletion before the creation. Only a
+ // rename within one worktree can do this; anything else falls back to
+ // the watcher.
+ let refresh = this.update(cx, |this, cx| {
+ let (source_worktree, source_rel_path) = this
+ .worktree_store()
+ .read(cx)
+ .find_worktree(&source_abs_path, cx)?;
+ let (target_worktree, target_rel_path) = this
+ .worktree_store()
+ .read(cx)
+ .find_worktree(&target_abs_path, cx)?;
+ if source_worktree != target_worktree {
+ return None;
+ }
+ target_worktree.update(cx, |worktree, cx| {
+ Some(worktree.as_local()?.refresh_entry(
+ target_rel_path,
+ Some(source_rel_path),
+ cx,
+ ))
+ })
+ });
+ if let Some(refresh) = refresh {
+ refresh.await?;
+ }
}
lsp::DocumentChangeOperation::Op(lsp::ResourceOp::Delete(op)) => {
diff --git a/crates/project/tests/integration/project_tests.rs b/crates/project/tests/integration/project_tests.rs
index 701ff7b0381..6cefd2a231e 100644
--- a/crates/project/tests/integration/project_tests.rs
+++ b/crates/project/tests/integration/project_tests.rs
@@ -7985,8 +7985,10 @@ async fn test_rename(cx: &mut gpui::TestAppContext) {
// Regression test for https://github.com/zed-industries/zed/issues/59077:
// a "rename symbol" whose workspace edit also renames the file used to swap the
-// two files' contents. The edited content must end up in the renamed file.
-#[gpui::test]
+// two files' contents. The edited content must end up in the renamed file, and
+// the open buffer must follow the rename regardless of the order the filesystem
+// watcher reports the change (hence the seed iterations).
+#[gpui::test(iterations = 30)]
async fn test_rename_that_also_renames_file(cx: &mut gpui::TestAppContext) {
init_test(cx);