move semantic search project intialization to a subscribe event for workspace created

KCaverly and Piotr created

Co-authored-by: Piotr <piotr@zed.dev>

Change summary

crates/search/src/project_search.rs         | 10 ----------
crates/semantic_index/src/semantic_index.rs | 18 +++++++++++++++++-
2 files changed, 17 insertions(+), 11 deletions(-)

Detailed changes

crates/search/src/project_search.rs 🔗

@@ -844,16 +844,6 @@ impl ProjectSearchView {
         .detach();
         let filters_enabled = false;
 
-        // Initialize Semantic Index if Needed
-        if SemanticIndex::enabled(cx) {
-            let model = model.read(cx);
-            project = model.project.clone();
-            SemanticIndex::global(cx).map(|semantic| {
-                dbg!("Initializing project");
-                semantic.update(cx, |this, cx| this.initialize_project(project.clone(), cx));
-            });
-        }
-
         // Check if Worktrees have all been previously indexed
         let mut this = ProjectSearchView {
             search_id: model.read(cx).search_id,

crates/semantic_index/src/semantic_index.rs 🔗

@@ -35,6 +35,7 @@ use util::{
     paths::EMBEDDINGS_DIR,
     ResultExt,
 };
+use workspace::WorkspaceCreated;
 
 const SEMANTIC_INDEX_VERSION: usize = 7;
 const EMBEDDINGS_BATCH_SIZE: usize = 80;
@@ -56,6 +57,22 @@ pub fn init(
         return;
     }
 
+    cx.subscribe_global::<WorkspaceCreated, _>({
+        move |event, mut cx| {
+            let Some(semantic_index) = SemanticIndex::global(cx) else { return; };
+            let workspace = &event.0;
+            if let Some(workspace) = workspace.upgrade(cx) {
+                let project = workspace.read(cx).project().clone();
+                if project.read(cx).is_local() {
+                    semantic_index.update(cx, |index, cx| {
+                        index.initialize_project(project, cx);
+                    });
+                }
+            }
+        }
+    })
+    .detach();
+
     cx.spawn(move |mut cx| async move {
         let semantic_index = SemanticIndex::new(
             fs,
@@ -133,7 +150,6 @@ impl ProjectState {
             async move {
                 while let Ok(operation) = job_queue_rx.recv().await {
                     Self::update_queue(&mut worktree_queue, operation);
-                    dbg!(worktree_queue.len());
                 }
             }
         });