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.

Release single worktree files from edit predictions (#65386)

Kirill Bulatov93b4e5edec06
diff --git a/crates/edit_prediction/src/edit_prediction.rs b/crates/edit_prediction/src/edit_prediction.rs
index 61ee444ab1e..d7cb3fc7e7f 100644
--- a/crates/edit_prediction/src/edit_prediction.rs
+++ b/crates/edit_prediction/src/edit_prediction.rs
@@ -1655,12 +1655,17 @@ impl EditPredictionStore {
}
}
}),
- cx.observe_release(buffer, move |this, _buffer, _cx| {
- let Some(project_state) = this.projects.get_mut(&project_entity_id)
+ cx.observe_release(buffer, move |ep_store, buffer, cx| {
+ let Some(project_state) = ep_store.projects.get_mut(&project_entity_id)
else {
return;
};
project_state.registered_buffers.remove(&buffer_id);
+ if project_state.last_event.as_ref().is_some_and(|event| {
+ event.new_snapshot.remote_id() == buffer.remote_id()
+ }) {
+ project_state.finalize_last_event(cx);
+ }
}),
],
})
@@ -1994,6 +1999,7 @@ impl EditPredictionStore {
for project_state in this.projects.values_mut() {
let ProjectState {
last_event,
+ next_last_event_seq,
registered_buffers,
license_detection_watchers,
pending_prediction_captures,
@@ -2048,6 +2054,15 @@ impl EditPredictionStore {
license_detection_watchers,
cx,
);
+ } else if pending_capture
+ .sample_data
+ .as_ref()
+ .and_then(|sample| sample.prompt_history_boundary.as_ref())
+ .is_some_and(|boundary| {
+ boundary.first_event_seq != *next_last_event_seq
+ })
+ {
+ pending_capture.sample_data = None;
}
ready_predictions.push((pending_capture, settled_editable_region));
continue;
diff --git a/crates/edit_prediction/src/edit_prediction_tests.rs b/crates/edit_prediction/src/edit_prediction_tests.rs
index e8845b9df76..a2435318e05 100644
--- a/crates/edit_prediction/src/edit_prediction_tests.rs
+++ b/crates/edit_prediction/src/edit_prediction_tests.rs
@@ -469,6 +469,87 @@ async fn test_request_events(cx: &mut TestAppContext) {
assert_eq!(prediction.edits[0].1.as_ref(), " are you?");
}
+#[gpui::test]
+async fn test_edit_history_releases_single_file_worktree_when_buffer_is_dropped(
+ cx: &mut TestAppContext,
+) {
+ let (ep_store, _requests) = init_test_with_fake_client(cx);
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree(
+ path!("/"),
+ json!({
+ "root": { "other.md": "other\n" },
+ "external.md": "one\n\nthree\n",
+ }),
+ )
+ .await;
+ let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
+ let buffer = project
+ .update(cx, |project, cx| {
+ project.open_local_buffer(path!("/external.md"), cx)
+ })
+ .await
+ .unwrap();
+ let other_buffer = project
+ .update(cx, |project, cx| {
+ project.open_local_buffer(path!("/root/other.md"), cx)
+ })
+ .await
+ .unwrap();
+ let worktree = project.read_with(cx, |project, cx| {
+ let (worktree, _) = project
+ .find_worktree(Path::new(path!("/external.md")), cx)
+ .unwrap();
+ assert!(worktree.read(cx).is_single_file());
+ assert!(!worktree.read(cx).is_visible());
+ worktree.downgrade()
+ });
+ ep_store.update(cx, |ep_store, cx| {
+ ep_store.register_buffer(&buffer, &project, cx);
+ ep_store.register_buffer(&other_buffer, &project, cx);
+ });
+ buffer.update(cx, |buffer, cx| {
+ buffer.edit([(4..4, "two")], None, cx);
+ });
+ project
+ .update(cx, |project, cx| project.save_buffer(buffer.clone(), cx))
+ .await
+ .unwrap();
+
+ cx.update(|_| drop(other_buffer));
+ cx.run_until_parked();
+ ep_store.read_with(cx, |ep_store, _| {
+ let project_state = &ep_store.projects[&project.entity_id()];
+ assert!(project_state.last_event.is_some());
+ });
+
+ let weak_buffer = buffer.downgrade();
+ cx.update(|_| drop(buffer));
+ cx.run_until_parked();
+ weak_buffer.assert_released();
+ worktree.assert_released();
+
+ let events = ep_store.read_with(cx, |ep_store, cx| {
+ ep_store.edit_history_for_project(&project, cx)
+ });
+ assert_eq!(
+ events
+ .iter()
+ .map(|event| {
+ let zeta_prompt::Event::BufferChange { diff, .. } = event.event.as_ref();
+ diff.as_str()
+ })
+ .collect::<Vec<_>>(),
+ [indoc! {"
+ @@ -1,3 +1,3 @@
+ one
+ -
+ +two
+ three
+ "}],
+ );
+}
+
#[gpui::test]
async fn test_edit_history_getter_pause_splits_last_event(cx: &mut TestAppContext) {
let (ep_store, _requests) = init_test_with_fake_client(cx);
@@ -4442,6 +4523,73 @@ async fn test_edit_prediction_settled_sample_data_requires_observing_all_events_
assert_eq!(missed_request.sample_data, None);
}
+#[gpui::test]
+async fn test_edit_prediction_settled_drops_sample_when_buffer_release_finalized_missed_event(
+ cx: &mut TestAppContext,
+) {
+ let (ep_store, mut requests, project, buffer) = init_sample_capture_test(
+ json!({
+ "LICENSE": MIT_LICENSE,
+ "foo.md": "one\n",
+ "other.md": "two\n",
+ }),
+ cx,
+ )
+ .await;
+ let other_buffer = project
+ .update(cx, |project, cx| {
+ project.open_local_buffer(path!("/root/other.md"), cx)
+ })
+ .await
+ .unwrap();
+ let boundary = ep_store.update(cx, |ep_store, cx| {
+ ep_store.register_buffer(&other_buffer, &project, cx);
+ let project_state = &ep_store.projects[&project.entity_id()];
+ PromptHistoryBoundary {
+ first_event_seq: project_state.next_last_event_seq,
+ snapshot: None,
+ }
+ });
+
+ other_buffer.update(cx, |buffer, cx| {
+ buffer.edit([(0..0, "updated ")], None, cx);
+ });
+ cx.update(|_| drop(other_buffer));
+ cx.run_until_parked();
+ ep_store.read_with(cx, |ep_store, _| {
+ assert!(ep_store.projects[&project.entity_id()].last_event.is_none());
+ });
+
+ enqueue_sample_capture(
+ &ep_store,
+ &project,
+ &buffer,
+ "prediction-after-close",
+ Point::new(0, 0)..Point::new(1, 0),
+ CapturedPredictionContext {
+ repository_url: None,
+ revision: None,
+ uncommitted_diff: None,
+ buffer_diagnostics: Vec::new(),
+ editable_context: Vec::new(),
+ },
+ Some(boundary),
+ VecDeque::new(),
+ cx,
+ )
+ .await;
+ cx.executor()
+ .advance_clock(EDIT_PREDICTION_SETTLED_QUIESCENCE);
+ cx.run_until_parked();
+
+ let request = requests
+ .settled
+ .next()
+ .await
+ .expect("settled request should be sent");
+ assert_eq!(request.sample_data, None);
+}
+
#[gpui::test]
async fn test_edit_prediction_settled_drops_future_events_when_their_oss_status_is_unknown(
cx: &mut TestAppContext,