Add fuzzy matching for snippets completions (#21524)

Closes #21439

This PR uses fuzzy matching for snippet completions instead of
fixed-prefix matching. This mimics the behavior of VSCode.

<img
src="https://github.com/user-attachments/assets/68537114-c5cf-4e4d-bc5c-4bb69ce947e5"
alt="fuzzy" width="450px" />

Release Notes:

- Improved suggestions for snippets.
This commit is contained in:
tims 2024-12-04 18:10:53 +05:30 committed by GitHub
parent 196fd65601
commit d8732adfb2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -13812,21 +13812,27 @@ fn snippet_completions(
buffer: &Model<Buffer>,
buffer_position: text::Anchor,
cx: &mut AppContext,
) -> Vec<Completion> {
) -> Task<Result<Vec<Completion>>> {
let language = buffer.read(cx).language_at(buffer_position);
let language_name = language.as_ref().map(|language| language.lsp_id());
let snippet_store = project.snippets().read(cx);
let snippets = snippet_store.snippets_for(language_name, cx);
if snippets.is_empty() {
return vec![];
return Task::ready(Ok(vec![]));
}
let snapshot = buffer.read(cx).text_snapshot();
let chars = snapshot.reversed_chars_for_range(text::Anchor::MIN..buffer_position);
let chars: String = snapshot
.reversed_chars_for_range(text::Anchor::MIN..buffer_position)
.collect();
let scope = language.map(|language| language.default_scope());
let executor = cx.background_executor().clone();
cx.background_executor().spawn(async move {
let classifier = CharClassifier::new(scope).for_completion(true);
let mut last_word = chars
.chars()
.take_while(|c| classifier.is_word(*c))
.collect::<String>();
last_word = last_word.chars().rev().collect();
@ -13836,13 +13842,54 @@ fn snippet_completions(
point_to_lsp(end)
};
let lsp_end = to_lsp(&buffer_position);
snippets
let candidates = snippets
.iter()
.enumerate()
.flat_map(|(ix, snippet)| {
snippet
.prefix
.iter()
.map(move |prefix| StringMatchCandidate::new(ix, prefix.clone()))
})
.collect::<Vec<StringMatchCandidate>>();
let mut matches = fuzzy::match_strings(
&candidates,
&last_word,
last_word.chars().any(|c| c.is_uppercase()),
100,
&Default::default(),
executor,
)
.await;
// Remove all candidates where the query's start does not match the start of any word in the candidate
if let Some(query_start) = last_word.chars().next() {
matches.retain(|string_match| {
split_words(&string_match.string).any(|word| {
// Check that the first codepoint of the word as lowercase matches the first
// codepoint of the query as lowercase
word.chars()
.flat_map(|codepoint| codepoint.to_lowercase())
.zip(query_start.to_lowercase())
.all(|(word_cp, query_cp)| word_cp == query_cp)
})
});
}
let matched_strings = matches
.into_iter()
.map(|m| m.string)
.collect::<HashSet<_>>();
let result: Vec<Completion> = snippets
.into_iter()
.filter_map(|snippet| {
let matching_prefix = snippet
.prefix
.iter()
.find(|prefix| prefix.starts_with(&last_word))?;
.find(|prefix| matched_strings.contains(*prefix))?;
let start = as_offset - last_word.len();
let start = snapshot.anchor_before(start);
let range = start..buffer_position;
@ -13885,7 +13932,10 @@ fn snippet_completions(
confirm: None,
})
})
.collect()
.collect();
Ok(result)
})
}
impl CompletionProvider for Model<Project> {
@ -13901,8 +13951,8 @@ impl CompletionProvider for Model<Project> {
let project_completions = project.completions(buffer, buffer_position, options, cx);
cx.background_executor().spawn(async move {
let mut completions = project_completions.await?;
//let snippets = snippets.into_iter().;
completions.extend(snippets);
let snippets_completions = snippets.await?;
completions.extend(snippets_completions);
Ok(completions)
})
})