Fix for extension search crash (revert #22524; revert #22525) (#22543)

Revert "Improve fuzzy match performance and fix corner case that omits
results (#22524)"
This reverts commit 6ef5d8f748.

Revert "Check cancel in multithreaded fuzzy matching (#22525)"
This reverts commit 51ac2d3667.

Fuzzy matching implemented in:
- https://github.com/zed-industries/zed/pull/22524
- https://github.com/zed-industries/zed/pull/22525

Caused a panic in the extension store search:
- Closes: https://github.com/zed-industries/zed/issues/22541

cc: @mgsloan 

Release Notes:

- N/A
This commit is contained in:
Peter Tripp 2025-01-01 17:04:37 +00:00 committed by GitHub
parent 38938ece49
commit 642dab82e5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 96 additions and 65 deletions

View file

@ -1,5 +1,5 @@
use crate::{
matcher::{MatchCandidate, Matcher},
matcher::{Match, MatchCandidate, Matcher},
CharBag,
};
use gpui::BackgroundExecutor;
@ -8,7 +8,7 @@ use std::{
cmp::{self, Ordering},
iter,
ops::Range,
sync::atomic::{self, AtomicBool},
sync::atomic::AtomicBool,
};
#[derive(Clone, Debug)]
@ -46,6 +46,16 @@ pub struct StringMatch {
pub string: String,
}
impl Match for StringMatch {
fn score(&self) -> f64 {
self.score
}
fn set_positions(&mut self, positions: Vec<usize>) {
self.positions = positions;
}
}
impl StringMatch {
pub fn ranges(&self) -> impl '_ + Iterator<Item = Range<usize>> {
let mut positions = self.positions.iter().peekable();
@ -157,8 +167,13 @@ pub async fn match_strings(
scope.spawn(async move {
let segment_start = cmp::min(segment_idx * segment_size, candidates.len());
let segment_end = cmp::min(segment_start + segment_size, candidates.len());
let mut matcher =
Matcher::new(query, lowercase_query, query_char_bag, smart_case);
let mut matcher = Matcher::new(
query,
lowercase_query,
query_char_bag,
smart_case,
max_results,
);
matcher.match_candidates(
&[],
@ -166,10 +181,10 @@ pub async fn match_strings(
candidates[segment_start..segment_end].iter(),
results,
cancel_flag,
|candidate, score, positions| StringMatch {
|candidate, score| StringMatch {
candidate_id: candidate.id,
score,
positions: positions.clone(),
positions: Vec::new(),
string: candidate.string.to_string(),
},
);
@ -178,11 +193,13 @@ pub async fn match_strings(
})
.await;
if cancel_flag.load(atomic::Ordering::Relaxed) {
return Vec::new();
let mut results = Vec::new();
for segment_result in segment_results {
if results.is_empty() {
results = segment_result;
} else {
util::extend_sorted(&mut results, segment_result, max_results, |a, b| b.cmp(a));
}
}
let mut results = segment_results.concat();
util::truncate_to_bottom_n_sorted_by(&mut results, max_results, &|a, b| b.cmp(a));
results
}