Fix rendering of project search while semantic index is indexing or running

Co-authored-by: Kyle <kyle@zed.dev>
This commit is contained in:
Max Brunsfeld 2023-07-18 12:01:42 -07:00
parent 8d0614ce74
commit 342dbc6945

View file

@ -110,7 +110,6 @@ struct SemanticSearchState {
file_count: usize, file_count: usize,
outstanding_file_count: usize, outstanding_file_count: usize,
_progress_task: Task<()>, _progress_task: Task<()>,
search_task: Option<Task<Result<()>>>,
} }
pub struct ProjectSearchBar { pub struct ProjectSearchBar {
@ -188,18 +187,17 @@ impl ProjectSearch {
cx.notify(); cx.notify();
} }
fn semantic_search(&mut self, query: String, cx: &mut ModelContext<Self>) -> Option<()> { fn semantic_search(&mut self, query: String, cx: &mut ModelContext<Self>) {
let project = self.project.clone(); let search = SemanticIndex::global(cx).map(|index| {
let semantic_index = SemanticIndex::global(cx)?; index.update(cx, |semantic_index, cx| {
let search_task = semantic_index.update(cx, |semantic_index, cx| { semantic_index.search_project(self.project.clone(), query.clone(), 10, cx)
semantic_index.search_project(project, query.clone(), 10, cx) })
}); });
self.search_id += 1; self.search_id += 1;
// self.active_query = Some(query); // self.active_query = Some(query);
self.match_ranges.clear(); self.match_ranges.clear();
self.pending_search = Some(cx.spawn(|this, mut cx| async move { self.pending_search = Some(cx.spawn(|this, mut cx| async move {
let results = search_task.await.log_err()?; let results = search?.await.log_err()?;
let (_task, mut match_ranges) = this.update(&mut cx, |this, cx| { let (_task, mut match_ranges) = this.update(&mut cx, |this, cx| {
this.excerpts.update(cx, |excerpts, cx| { this.excerpts.update(cx, |excerpts, cx| {
@ -231,8 +229,7 @@ impl ProjectSearch {
None None
})); }));
cx.notify();
Some(())
} }
} }
@ -257,12 +254,10 @@ impl View for ProjectSearchView {
enum Status {} enum Status {}
let theme = theme::current(cx).clone(); let theme = theme::current(cx).clone();
let text = if self.query_editor.read(cx).text(cx).is_empty() { let text = if model.pending_search.is_some() {
Cow::Borrowed("") Cow::Borrowed("Searching...")
} else if let Some(semantic) = &self.semantic { } else if let Some(semantic) = &self.semantic {
if semantic.search_task.is_some() { if semantic.outstanding_file_count > 0 {
Cow::Borrowed("Searching...")
} else if semantic.outstanding_file_count > 0 {
Cow::Owned(format!( Cow::Owned(format!(
"Indexing. {} of {}...", "Indexing. {} of {}...",
semantic.file_count - semantic.outstanding_file_count, semantic.file_count - semantic.outstanding_file_count,
@ -271,8 +266,8 @@ impl View for ProjectSearchView {
} else { } else {
Cow::Borrowed("Indexing complete") Cow::Borrowed("Indexing complete")
} }
} else if model.pending_search.is_some() { } else if self.query_editor.read(cx).text(cx).is_empty() {
Cow::Borrowed("Searching...") Cow::Borrowed("")
} else { } else {
Cow::Borrowed("No results") Cow::Borrowed("No results")
}; };
@ -978,10 +973,10 @@ impl ProjectSearchBar {
let (files_to_index, mut files_remaining_rx) = index_task.await?; let (files_to_index, mut files_remaining_rx) = index_task.await?;
search_view.update(&mut cx, |search_view, cx| { search_view.update(&mut cx, |search_view, cx| {
cx.notify();
search_view.semantic = Some(SemanticSearchState { search_view.semantic = Some(SemanticSearchState {
file_count: files_to_index, file_count: files_to_index,
outstanding_file_count: files_to_index, outstanding_file_count: files_to_index,
search_task: None,
_progress_task: cx.spawn(|search_view, mut cx| async move { _progress_task: cx.spawn(|search_view, mut cx| async move {
while let Some(count) = files_remaining_rx.recv().await { while let Some(count) = files_remaining_rx.recv().await {
search_view search_view
@ -1006,6 +1001,7 @@ impl ProjectSearchBar {
}) })
.detach_and_log_err(cx); .detach_and_log_err(cx);
} }
cx.notify();
}); });
cx.notify(); cx.notify();
true true