Skip to content

Commit

Permalink
Auto merge of #15348 - max-heller:issue-14692, r=lowr
Browse files Browse the repository at this point in the history
Exclude non-identifier aliases from completion filtering text

When building `CompletionItem`s, this excludes aliases that aren't valid identifiers from the "lookup" text used to filter completions in the LSP client. Including them results in weird completion filtering behavior e.g. `Partial>` matching a completion for the `PartialOrd` trait because it has a doc alias of ">".

Closes #14692
  • Loading branch information
bors committed Aug 2, 2023
2 parents 30f526c + a743903 commit 2f2cf21
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
21 changes: 19 additions & 2 deletions crates/ide-completion/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,26 @@ 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()
// 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().is_some_and(char::is_alphabetic)
&& chars.all(|c| c.is_alphanumeric() || c == '_')
})
// Deliberately concatenated without separators as adding separators e.g.
// `alias1, alias2` results in LSP clients continuing to display the completion even
// after typing a comma or space.
.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
23 changes: 23 additions & 0 deletions crates/ide-completion/src/tests/special.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,3 +1280,26 @@ fn here_we_go() {
"#]],
);
}

#[test]
fn completion_filtering_excludes_non_identifier_doc_aliases() {
check_edit(
"PartialOrdcmporder",
r#"
#[doc(alias = ">")]
#[doc(alias = "cmp")]
#[doc(alias = "order")]
trait PartialOrd {}
struct Foo<T: Partial$0
"#,
r#"
#[doc(alias = ">")]
#[doc(alias = "cmp")]
#[doc(alias = "order")]
trait PartialOrd {}
struct Foo<T: PartialOrd
"#,
);
}

0 comments on commit 2f2cf21

Please sign in to comment.