From 31fb5034180242d6036d377cab8fc670656a7cf6 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Thu, 3 Aug 2023 20:26:56 +0200 Subject: [PATCH] Rewrite permission queries (it no longer blocks) Co-authored-by: Kyle Co-authored-by: Max --- crates/search/src/project_search.rs | 101 ++++++++++---------- crates/semantic_index/src/semantic_index.rs | 14 --- 2 files changed, 52 insertions(+), 63 deletions(-) diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index 014ec1319727b9a11bbf0b6a13f16881913bc55e..0fc13d992ca614bceb7a5b20b707061a4abe947a 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -2,7 +2,7 @@ use crate::{ NextHistoryQuery, PreviousHistoryQuery, SearchHistory, SearchOptions, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, ToggleWholeWord, }; -use anyhow::Context; +use anyhow::{Context, Result}; use collections::HashMap; use editor::{ items::active_match_index, scroll::autoscroll::Autoscroll, Anchor, Editor, MultiBuffer, @@ -761,46 +761,50 @@ impl ProjectSearchView { match mode { SearchMode::Semantic => { - // let semantic_permissioned = self.semantic_permissioned.await; - // if semantic_permissioned.is_ok_and(|permission| !permission) { - if !self.semantic_permissioned(cx) { - // TODO: Change this to read from the project name - let project = self.model.read(cx).project.clone(); - let project_name = project - .read(cx) - .worktree_root_names(cx) - .collect::>() - .join("/"); - let is_plural = - project_name.chars().filter(|letter| *letter == '/').count() > 0; - let prompt_text = format!("Would you like to index the '{}' project{} for semantic search? This requires sending code to the OpenAI API", project_name, - if is_plural { - "s" - } else {""}); - let mut answer = cx.prompt( - PromptLevel::Info, - prompt_text.as_str(), - &["Continue", "Cancel"], - ); - - cx.spawn(|search_view, mut cx| async move { + let has_permission = self.semantic_permissioned(cx); + cx.spawn(|this, mut cx| async move { + let has_permission = has_permission.await?; + + if !has_permission { + let mut answer = this.update(&mut cx, |this, cx| { + let project = this.model.read(cx).project.clone(); + let project_name = project + .read(cx) + .worktree_root_names(cx) + .collect::>() + .join("/"); + let is_plural = + project_name.chars().filter(|letter| *letter == '/').count() > 0; + let prompt_text = format!("Would you like to index the '{}' project{} for semantic search? This requires sending code to the OpenAI API", project_name, + if is_plural { + "s" + } else {""}); + cx.prompt( + PromptLevel::Info, + prompt_text.as_str(), + &["Continue", "Cancel"], + ) + })?; + if answer.next().await == Some(0) { - search_view.update(&mut cx, |search_view, cx| { - search_view.semantic_permissioned = Some(true); - search_view.index_project(cx); + this.update(&mut cx, |this, cx| { + this.semantic_permissioned = Some(true); })?; - anyhow::Ok(()) } else { - search_view.update(&mut cx, |search_view, cx| { - search_view.activate_search_mode(SearchMode::Regex, cx); + this.update(&mut cx, |this, cx| { + this.semantic_permissioned = Some(false); + this.activate_search_mode(SearchMode::Regex, cx); })?; - anyhow::Ok(()) + return anyhow::Ok(()); } - }) - .detach_and_log_err(cx); - } else { - self.index_project(cx); - } + } + + this.update(&mut cx, |this, cx| { + this.index_project(cx); + })?; + + anyhow::Ok(()) + }).detach_and_log_err(cx); } SearchMode::Regex => { if !self.is_option_enabled(SearchOptions::REGEX) { @@ -922,19 +926,18 @@ impl ProjectSearchView { this.model_changed(cx); this } - fn semantic_permissioned(&mut self, cx: &mut ViewContext) -> bool { - *self.semantic_permissioned.get_or_insert_with(|| { - SemanticIndex::global(cx) - .and_then(|semantic| { - let project = self.model.read(cx).project.clone(); - smol::block_on( - semantic - .update(cx, |this, cx| this.project_previously_indexed(project, cx)), - ) - .ok() - }) - .unwrap_or_default() - }) + + fn semantic_permissioned(&mut self, cx: &mut ViewContext) -> Task> { + if let Some(value) = self.semantic_permissioned { + return Task::ready(Ok(value)); + } + + SemanticIndex::global(cx) + .map(|semantic| { + let project = self.model.read(cx).project.clone(); + semantic.update(cx, |this, cx| this.project_previously_indexed(project, cx)) + }) + .unwrap_or(Task::ready(Ok(false))) } pub fn new_search_in_directory( workspace: &mut Workspace, diff --git a/crates/semantic_index/src/semantic_index.rs b/crates/semantic_index/src/semantic_index.rs index 772dff9af75e7d3f4e7830a0bf18fe5c643435b2..50b871d454a6f6d6f57fc8361f72a6194ebedddf 100644 --- a/crates/semantic_index/src/semantic_index.rs +++ b/crates/semantic_index/src/semantic_index.rs @@ -500,26 +500,12 @@ impl SemanticIndex { project: ModelHandle, cx: &mut ModelContext, ) -> Task> { - let worktree_scans_complete = project - .read(cx) - .worktrees(cx) - .map(|worktree| { - let scan_complete = worktree.read(cx).as_local().unwrap().scan_complete(); - async move { - scan_complete.await; - } - }) - .collect::>(); - let worktrees_indexed_previously = project .read(cx) .worktrees(cx) .map(|worktree| self.worktree_previously_indexed(worktree.read(cx).abs_path())) .collect::>(); - cx.spawn(|_, _cx| async move { - futures::future::join_all(worktree_scans_complete).await; - let worktree_indexed_previously = futures::future::join_all(worktrees_indexed_previously).await;