Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude non-identifier aliases from completion filtering text #15348

Merged
merged 6 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/ide-completion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ itertools = "0.10.5"

once_cell = "1.17.0"
smallvec.workspace = true
unicode-ident = "1.0.0"
max-heller marked this conversation as resolved.
Show resolved Hide resolved


# local deps
Expand Down
34 changes: 32 additions & 2 deletions crates/ide-completion/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,21 @@ impl Builder {
let insert_text = self.insert_text.unwrap_or_else(|| label.to_string());

if !self.doc_aliases.is_empty() {
let doc_aliases = self.doc_aliases.into_iter().join(", ");
let doc_aliases = self.doc_aliases.iter().join(", ");
label = SmolStr::from(format!("{label} (alias {doc_aliases})"));
lookup = SmolStr::from(format!("{lookup} {doc_aliases}"));
let lookup_doc_aliases = (self.doc_aliases.iter())
max-heller marked this conversation as resolved.
Show resolved Hide resolved
// Don't include aliases in `lookup` that aren't valid identifiers as including
// them results in weird completion filtering behavior e.g. `Partial>` matching
// `PartialOrd` because it has an alias of ">".
.filter(|alias| {
let mut chars = alias.chars();
chars.next().map(unicode_ident::is_xid_start).unwrap_or(false)
max-heller marked this conversation as resolved.
Show resolved Hide resolved
&& chars.all(unicode_ident::is_xid_continue)
})
.join(", ");
if !lookup_doc_aliases.is_empty() {
lookup = SmolStr::from(format!("{lookup} {lookup_doc_aliases}"));
}
}
if let [import_edit] = &*self.imports_to_add {
// snippets can have multiple imports, but normal completions only have up to one
Expand Down Expand Up @@ -553,9 +565,12 @@ impl Builder {

#[cfg(test)]
mod tests {
use ide_db::SymbolKind;
use itertools::Itertools;
use test_utils::assert_eq_text;

use crate::{CompletionItem, CompletionItemKind};

use super::{
CompletionRelevance, CompletionRelevancePostfixMatch, CompletionRelevanceTypeMatch,
};
Expand Down Expand Up @@ -630,4 +645,19 @@ mod tests {

check_relevance_score_ordered(expected_relevance_order);
}

#[test]
fn exclude_non_identifier_aliases_from_lookup() {
max-heller marked this conversation as resolved.
Show resolved Hide resolved
let mut item = CompletionItem::new(
CompletionItemKind::SymbolKind(SymbolKind::Trait),
Default::default(),
"PartialOrd",
);
let aliases = [">", "<", "<=", ">="];
item.doc_aliases(aliases.iter().map(|&alias| alias.into()).collect());
let item = item.build(&Default::default());
for alias in aliases {
assert!(!item.lookup().contains(alias));
}
}
}