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: Fix content swap when an LSP rename also renames the file (#59104)

Ibrahim Khan2243c13b9b22
diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rs
index 2a95034cbaa..1555a25dd21 100644
--- a/crates/project/src/lsp_store.rs
+++ b/crates/project/src/lsp_store.rs
@@ -3440,6 +3440,25 @@ impl LocalLspStore {
.to_file_path()
.map_err(|()| anyhow!("can't convert URI to path"))?;
+ // An LSP "rename symbol" can also rename the file, with the text edit
+ // applied only to the in-memory buffer. Persist it before renaming, or
+ // fs.rename moves the stale on-disk content and the files' contents swap.
+ let dirty_buffer = this.update(cx, |this, cx| {
+ let project_path = this
+ .worktree_store()
+ .read(cx)
+ .project_path_for_absolute_path(&source_abs_path, cx)?;
+ let buffer = this.buffer_store().read(cx).get_by_path(&project_path)?;
+ buffer.read(cx).is_dirty().then_some(buffer)
+ });
+ if let Some(buffer) = dirty_buffer {
+ this.update(cx, |this, cx| {
+ this.buffer_store()
+ .update(cx, |buffer_store, cx| buffer_store.save_buffer(buffer, cx))
+ })
+ .await?;
+ }
+
let options = fs::RenameOptions {
overwrite: op
.options
diff --git a/crates/project/tests/integration/project_tests.rs b/crates/project/tests/integration/project_tests.rs
index 7b4f5839e94..47ff6c07409 100644
--- a/crates/project/tests/integration/project_tests.rs
+++ b/crates/project/tests/integration/project_tests.rs
@@ -7673,6 +7673,110 @@ 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]
+async fn test_rename_that_also_renames_file(cx: &mut gpui::TestAppContext) {
+ init_test(cx);
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree(
+ path!("/dir"),
+ json!({
+ "one.rs": "const ONE: usize = 1;",
+ }),
+ )
+ .await;
+
+ let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
+ let language_registry = project.read_with(cx, |project, _| project.languages().clone());
+ language_registry.add(rust_lang());
+ let mut fake_servers = language_registry.register_fake_lsp(
+ "Rust",
+ FakeLspAdapter {
+ capabilities: lsp::ServerCapabilities {
+ rename_provider: Some(lsp::OneOf::Right(lsp::RenameOptions {
+ prepare_provider: Some(true),
+ work_done_progress_options: Default::default(),
+ })),
+ ..Default::default()
+ },
+ ..Default::default()
+ },
+ );
+
+ let (buffer, _handle) = project
+ .update(cx, |project, cx| {
+ project.open_local_buffer_with_lsp(path!("/dir/one.rs"), cx)
+ })
+ .await
+ .unwrap();
+
+ let fake_server = fake_servers.next().await.unwrap();
+ cx.executor().run_until_parked();
+
+ let response = project.update(cx, |project, cx| {
+ project.perform_rename(buffer.clone(), 7, "THREE".to_string(), cx)
+ });
+ fake_server
+ .set_request_handler::<lsp::request::Rename, _, _>(|_params, _| async move {
+ Ok(Some(lsp::WorkspaceEdit {
+ changes: None,
+ document_changes: Some(DocumentChanges::Operations(vec![
+ lsp::DocumentChangeOperation::Edit(lsp::TextDocumentEdit {
+ text_document: lsp::OptionalVersionedTextDocumentIdentifier {
+ uri: Uri::from_file_path(path!("/dir/one.rs")).unwrap(),
+ version: None,
+ },
+ edits: vec![lsp::Edit::Plain(lsp::TextEdit::new(
+ lsp::Range::new(lsp::Position::new(0, 6), lsp::Position::new(0, 9)),
+ "THREE".to_string(),
+ ))],
+ }),
+ lsp::DocumentChangeOperation::Op(lsp::ResourceOp::Rename(lsp::RenameFile {
+ old_uri: Uri::from_file_path(path!("/dir/one.rs")).unwrap(),
+ new_uri: Uri::from_file_path(path!("/dir/three.rs")).unwrap(),
+ options: None,
+ annotation_id: None,
+ })),
+ ])),
+ change_annotations: None,
+ }))
+ })
+ .next()
+ .await
+ .unwrap();
+ response.await.unwrap();
+ cx.executor().run_until_parked();
+
+ // The renamed file must contain the edited content, not the stale pre-edit
+ // content, and the old file must be gone (no content swap).
+ assert_eq!(
+ fs.load(Path::new(path!("/dir/three.rs"))).await.unwrap(),
+ "const THREE: usize = 1;"
+ );
+ assert!(
+ fs.load(Path::new(path!("/dir/one.rs"))).await.is_err(),
+ "old file should not exist after the rename"
+ );
+
+ // The buffer should follow the rename to the new path, hold the edited
+ // content, and be saved (so it can't be written back to the old path).
+ buffer.read_with(cx, |buffer, _cx| {
+ assert_eq!(buffer.text(), "const THREE: usize = 1;");
+ assert_eq!(
+ buffer.file().unwrap().path().as_ref(),
+ rel_path("three.rs"),
+ "buffer should be associated with the renamed file"
+ );
+ assert!(
+ !buffer.is_dirty(),
+ "buffer should be saved after the rename"
+ );
+ });
+}
+
#[gpui::test]
async fn test_search(cx: &mut gpui::TestAppContext) {
init_test(cx);