Skip to content

Commit 9349769

Browse files
committed
exclude non-identifier aliases from completion filtering text
1 parent b64e5b3 commit 9349769

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ide-completion/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ itertools = "0.10.5"
1717

1818
once_cell = "1.17.0"
1919
smallvec.workspace = true
20+
unicode-ident = "1.0.0"
2021

2122

2223
# local deps

crates/ide-completion/src/item.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,21 @@ impl Builder {
427427
let insert_text = self.insert_text.unwrap_or_else(|| label.to_string());
428428

429429
if !self.doc_aliases.is_empty() {
430-
let doc_aliases = self.doc_aliases.into_iter().join(", ");
430+
let doc_aliases = self.doc_aliases.iter().join(", ");
431431
label = SmolStr::from(format!("{label} (alias {doc_aliases})"));
432-
lookup = SmolStr::from(format!("{lookup} {doc_aliases}"));
432+
let lookup_doc_aliases = (self.doc_aliases.iter())
433+
// Don't include aliases in `lookup` that aren't valid identifiers as including
434+
// them results in weird completion filtering behavior e.g. `Partial>` matching
435+
// `PartialOrd` because it has an alias of ">".
436+
.filter(|alias| {
437+
let mut chars = alias.chars();
438+
chars.next().map(unicode_ident::is_xid_start).unwrap_or(false)
439+
&& chars.all(unicode_ident::is_xid_continue)
440+
})
441+
.join(", ");
442+
if !lookup_doc_aliases.is_empty() {
443+
lookup = SmolStr::from(format!("{lookup} {lookup_doc_aliases}"));
444+
}
433445
}
434446
if let [import_edit] = &*self.imports_to_add {
435447
// snippets can have multiple imports, but normal completions only have up to one
@@ -553,9 +565,12 @@ impl Builder {
553565

554566
#[cfg(test)]
555567
mod tests {
568+
use ide_db::SymbolKind;
556569
use itertools::Itertools;
557570
use test_utils::assert_eq_text;
558571

572+
use crate::{CompletionItem, CompletionItemKind};
573+
559574
use super::{
560575
CompletionRelevance, CompletionRelevancePostfixMatch, CompletionRelevanceTypeMatch,
561576
};
@@ -630,4 +645,19 @@ mod tests {
630645

631646
check_relevance_score_ordered(expected_relevance_order);
632647
}
648+
649+
#[test]
650+
fn exclude_non_identifier_aliases_from_lookup() {
651+
let mut item = CompletionItem::new(
652+
CompletionItemKind::SymbolKind(SymbolKind::Trait),
653+
Default::default(),
654+
"PartialOrd",
655+
);
656+
let aliases = [">", "<", "<=", ">="];
657+
item.doc_aliases(aliases.iter().map(|&alias| alias.into()).collect());
658+
let item = item.build(&Default::default());
659+
for alias in aliases {
660+
assert!(!item.lookup().contains(alias));
661+
}
662+
}
633663
}

0 commit comments

Comments
 (0)