editor: Utilize filter_text from language server for filter_range (#33155)

Closes #33106

Instead of directly using `filter_text` in `StringMatchCandidate`, which
yields better results for fuzzy matching but messes up with highlighting
letters in bold, as `positions` list generated by fuzzy crate are now of
`filter_text` and not `label` shown to the user.

This PR fixes it by keeping use of `filter_range` in
`StringMatchCandidate`, which is range w.r.t to `label` shown to user.
And actually generating this `filter_range` at source by using
`filter_range` if exists.

- [x] Tests

Release Notes:

- Fixed issue where incorrect letters are marked as bold in completions.
This commit is contained in:
Smit Barmase 2025-06-21 19:47:16 +05:30 committed by GitHub
parent 834cdc1271
commit 76e3136369
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 206 additions and 61 deletions

View file

@ -310,10 +310,15 @@ impl LspAdapter for RustLspAdapter {
let source = Rope::from(format!("{prefix}{text} }}"));
let runs =
language.highlight_text(&source, prefix.len()..prefix.len() + text.len());
let filter_range = completion
.filter_text
.as_deref()
.and_then(|filter| text.find(filter).map(|ix| ix..ix + filter.len()))
.unwrap_or(0..name.len());
return Some(CodeLabel {
text,
runs,
filter_range: 0..name.len(),
filter_range,
});
}
(
@ -330,10 +335,15 @@ impl LspAdapter for RustLspAdapter {
let source = Rope::from(format!("{prefix}{text} = ();"));
let runs =
language.highlight_text(&source, prefix.len()..prefix.len() + text.len());
let filter_range = completion
.filter_text
.as_deref()
.and_then(|filter| text.find(filter).map(|ix| ix..ix + filter.len()))
.unwrap_or(0..name.len());
return Some(CodeLabel {
text,
runs,
filter_range: 0..name.len(),
filter_range,
});
}
(
@ -367,9 +377,13 @@ impl LspAdapter for RustLspAdapter {
text.push(' ');
text.push_str(&detail);
}
let filter_range = completion
.filter_text
.as_deref()
.and_then(|filter| text.find(filter).map(|ix| ix..ix + filter.len()))
.unwrap_or(0..completion.label.find('(').unwrap_or(text.len()));
return Some(CodeLabel {
filter_range: 0..completion.label.find('(').unwrap_or(text.len()),
filter_range,
text,
runs,
});
@ -378,12 +392,18 @@ impl LspAdapter for RustLspAdapter {
.as_ref()
.map_or(false, |detail| detail.starts_with("macro_rules! "))
{
let source = Rope::from(completion.label.as_str());
let runs = language.highlight_text(&source, 0..completion.label.len());
let text = completion.label.clone();
let len = text.len();
let source = Rope::from(text.as_str());
let runs = language.highlight_text(&source, 0..len);
let filter_range = completion
.filter_text
.as_deref()
.and_then(|filter| text.find(filter).map(|ix| ix..ix + filter.len()))
.unwrap_or(0..len);
return Some(CodeLabel {
filter_range: 0..completion.label.len(),
text: completion.label.clone(),
filter_range,
text,
runs,
});
}
@ -406,7 +426,7 @@ impl LspAdapter for RustLspAdapter {
label.push(' ');
label.push_str(detail);
}
let mut label = CodeLabel::plain(label, None);
let mut label = CodeLabel::plain(label, completion.filter_text.as_deref());
if let Some(highlight_name) = highlight_name {
let highlight_id = language.grammar()?.highlight_id_for_name(highlight_name)?;
label.runs.push((
@ -1181,6 +1201,49 @@ mod tests {
],
})
);
assert_eq!(
adapter
.label_for_completion(
&lsp::CompletionItem {
kind: Some(lsp::CompletionItemKind::METHOD),
label: "await.as_deref_mut()".to_string(),
filter_text: Some("as_deref_mut".to_string()),
label_details: Some(CompletionItemLabelDetails {
detail: None,
description: Some("fn(&mut self) -> IterMut<'_, T>".to_string()),
}),
..Default::default()
},
&language
)
.await,
Some(CodeLabel {
text: "await.as_deref_mut()".to_string(),
filter_range: 6..18,
runs: vec![],
})
);
assert_eq!(
adapter
.label_for_completion(
&lsp::CompletionItem {
kind: Some(lsp::CompletionItemKind::FIELD),
label: "inner_value".to_string(),
filter_text: Some("value".to_string()),
detail: Some("String".to_string()),
..Default::default()
},
&language,
)
.await,
Some(CodeLabel {
text: "inner_value: String".to_string(),
filter_range: 6..11,
runs: vec![(0..11, HighlightId(3)), (13..19, HighlightId(0))],
})
);
}
#[gpui::test]