zeta2: Support `edit prediction: clear history` (#38808)

Bennet Bo Fenner and Agus Zubiaga created

Release Notes:

- N/A

Co-authored-by: Agus Zubiaga <agus@zed.dev>

Change summary

crates/zed/src/zed/edit_prediction_registry.rs |  2 ++
crates/zeta2/src/zeta2.rs                      | 10 ++++++++++
2 files changed, 12 insertions(+)

Detailed changes

crates/zed/src/zed/edit_prediction_registry.rs 🔗

@@ -97,6 +97,8 @@ pub fn init(client: Arc<Client>, user_store: Entity<UserStore>, cx: &mut App) {
 fn clear_zeta_edit_history(_: &zeta::ClearHistory, cx: &mut App) {
     if let Some(zeta) = zeta::Zeta::global(cx) {
         zeta.update(cx, |zeta, _| zeta.clear_history());
+    } else if let Some(zeta) = zeta2::Zeta::try_global(cx) {
+        zeta.update(cx, |zeta, _| zeta.clear_history());
     }
 }
 

crates/zeta2/src/zeta2.rs 🔗

@@ -108,6 +108,10 @@ pub enum Event {
 }
 
 impl Zeta {
+    pub fn try_global(cx: &App) -> Option<Entity<Self>> {
+        cx.try_global::<ZetaGlobal>().map(|global| global.0.clone())
+    }
+
     pub fn global(
         client: &Arc<Client>,
         user_store: &Entity<UserStore>,
@@ -162,6 +166,12 @@ impl Zeta {
         self.options = options;
     }
 
+    pub fn clear_history(&mut self) {
+        for zeta_project in self.projects.values_mut() {
+            zeta_project.events.clear();
+        }
+    }
+
     pub fn usage(&self, cx: &App) -> Option<EditPredictionUsage> {
         self.user_store.read(cx).edit_prediction_usage()
     }