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:
parent
196fd65601
commit
d8732adfb2
1 changed files with 115 additions and 65 deletions
|
@ -13812,21 +13812,27 @@ fn snippet_completions(
|
||||||
buffer: &Model<Buffer>,
|
buffer: &Model<Buffer>,
|
||||||
buffer_position: text::Anchor,
|
buffer_position: text::Anchor,
|
||||||
cx: &mut AppContext,
|
cx: &mut AppContext,
|
||||||
) -> Vec<Completion> {
|
) -> Task<Result<Vec<Completion>>> {
|
||||||
let language = buffer.read(cx).language_at(buffer_position);
|
let language = buffer.read(cx).language_at(buffer_position);
|
||||||
let language_name = language.as_ref().map(|language| language.lsp_id());
|
let language_name = language.as_ref().map(|language| language.lsp_id());
|
||||||
let snippet_store = project.snippets().read(cx);
|
let snippet_store = project.snippets().read(cx);
|
||||||
let snippets = snippet_store.snippets_for(language_name, cx);
|
let snippets = snippet_store.snippets_for(language_name, cx);
|
||||||
|
|
||||||
if snippets.is_empty() {
|
if snippets.is_empty() {
|
||||||
return vec![];
|
return Task::ready(Ok(vec![]));
|
||||||
}
|
}
|
||||||
let snapshot = buffer.read(cx).text_snapshot();
|
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 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 classifier = CharClassifier::new(scope).for_completion(true);
|
||||||
let mut last_word = chars
|
let mut last_word = chars
|
||||||
|
.chars()
|
||||||
.take_while(|c| classifier.is_word(*c))
|
.take_while(|c| classifier.is_word(*c))
|
||||||
.collect::<String>();
|
.collect::<String>();
|
||||||
last_word = last_word.chars().rev().collect();
|
last_word = last_word.chars().rev().collect();
|
||||||
|
@ -13836,13 +13842,54 @@ fn snippet_completions(
|
||||||
point_to_lsp(end)
|
point_to_lsp(end)
|
||||||
};
|
};
|
||||||
let lsp_end = to_lsp(&buffer_position);
|
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()
|
.into_iter()
|
||||||
.filter_map(|snippet| {
|
.filter_map(|snippet| {
|
||||||
let matching_prefix = snippet
|
let matching_prefix = snippet
|
||||||
.prefix
|
.prefix
|
||||||
.iter()
|
.iter()
|
||||||
.find(|prefix| prefix.starts_with(&last_word))?;
|
.find(|prefix| matched_strings.contains(*prefix))?;
|
||||||
let start = as_offset - last_word.len();
|
let start = as_offset - last_word.len();
|
||||||
let start = snapshot.anchor_before(start);
|
let start = snapshot.anchor_before(start);
|
||||||
let range = start..buffer_position;
|
let range = start..buffer_position;
|
||||||
|
@ -13885,7 +13932,10 @@ fn snippet_completions(
|
||||||
confirm: None,
|
confirm: None,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.collect()
|
.collect();
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CompletionProvider for Model<Project> {
|
impl CompletionProvider for Model<Project> {
|
||||||
|
@ -13901,8 +13951,8 @@ impl CompletionProvider for Model<Project> {
|
||||||
let project_completions = project.completions(buffer, buffer_position, options, cx);
|
let project_completions = project.completions(buffer, buffer_position, options, cx);
|
||||||
cx.background_executor().spawn(async move {
|
cx.background_executor().spawn(async move {
|
||||||
let mut completions = project_completions.await?;
|
let mut completions = project_completions.await?;
|
||||||
//let snippets = snippets.into_iter().;
|
let snippets_completions = snippets.await?;
|
||||||
completions.extend(snippets);
|
completions.extend(snippets_completions);
|
||||||
Ok(completions)
|
Ok(completions)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue