Skip to content

Commit

Permalink
Auto merge of rust-lang#12074 - jonas-schievink:deprioritize-flyimpor…
Browse files Browse the repository at this point in the history
…t, r=jonas-schievink

Reduce priority of flyimport completions

Fixes rust-lang/rust-analyzer#12068
  • Loading branch information
bors committed Apr 25, 2022
2 parents c1de78f + 3a83684 commit b24b82f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
7 changes: 7 additions & 0 deletions crates/ide_completion/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ pub struct CompletionRelevance {
pub is_item_from_trait: bool,
/// This is set when an import is suggested whose name is already imported.
pub is_name_already_imported: bool,
/// This is set for completions that will insert a `use` item.
pub requires_import: bool,
/// Set for method completions of the `core::ops` and `core::cmp` family.
pub is_op_method: bool,
/// Set for item completions that are private but in the workspace.
Expand Down Expand Up @@ -208,6 +210,7 @@ impl CompletionRelevance {
is_local,
is_item_from_trait,
is_name_already_imported,
requires_import,
is_op_method,
is_private_editable,
postfix_match,
Expand All @@ -226,6 +229,10 @@ impl CompletionRelevance {
if !is_name_already_imported {
score += 1;
}
// lower rank for items that don't need an import
if !requires_import {
score += 1;
}
if exact_name_match {
score += 10;
}
Expand Down
41 changes: 39 additions & 2 deletions crates/ide_completion/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ impl<'a> RenderContext<'a> {
}

fn completion_relevance(&self) -> CompletionRelevance {
CompletionRelevance { is_private_editable: self.is_private_editable, ..Default::default() }
CompletionRelevance {
is_private_editable: self.is_private_editable,
requires_import: self.import_to_add.is_some(),
..Default::default()
}
}

fn is_deprecated(&self, def: impl HasAttrs) -> bool {
Expand Down Expand Up @@ -247,6 +251,7 @@ fn render_resolution_simple_(

let local_name = local_name.to_smol_str();
let mut item = CompletionItem::new(kind, ctx.source_range(), local_name.clone());
item.set_relevance(ctx.completion_relevance());
if let ScopeDef::Local(local) = resolution {
let ty = local.ty(db);
if !ty.is_unknown() {
Expand Down Expand Up @@ -446,6 +451,7 @@ mod tests {
"snippet",
),
(relevance.is_op_method, "op_method"),
(relevance.requires_import, "requires_import"),
]
.into_iter()
.filter_map(|(cond, desc)| if cond { Some(desc) } else { None })
Expand Down Expand Up @@ -626,6 +632,7 @@ fn main() { let _: m::Spam = S$0 }
is_local: false,
is_item_from_trait: false,
is_name_already_imported: false,
requires_import: false,
is_op_method: false,
is_private_editable: false,
postfix_match: None,
Expand All @@ -650,6 +657,7 @@ fn main() { let _: m::Spam = S$0 }
is_local: false,
is_item_from_trait: false,
is_name_already_imported: false,
requires_import: false,
is_op_method: false,
is_private_editable: false,
postfix_match: None,
Expand Down Expand Up @@ -740,6 +748,7 @@ fn foo() { A { the$0 } }
is_local: false,
is_item_from_trait: false,
is_name_already_imported: false,
requires_import: false,
is_op_method: false,
is_private_editable: false,
postfix_match: None,
Expand Down Expand Up @@ -1579,7 +1588,7 @@ fn main() {
&[CompletionItemKind::Snippet, CompletionItemKind::Method],
expect![[r#"
sn not [snippet]
me not() (use ops::Not) [type_could_unify]
me not() (use ops::Not) [type_could_unify+requires_import]
sn if []
sn while []
sn ref []
Expand Down Expand Up @@ -1621,4 +1630,32 @@ fn main() {
"#]],
);
}

#[test]
fn flyimport_reduced_relevance() {
check_relevance(
r#"
mod std {
pub mod io {
pub trait BufRead {}
pub struct BufReader;
pub struct BufWriter;
}
}
struct Buffer;
fn f() {
Buf$0
}
"#,
expect![[r#"
md std []
st Buffer []
fn f() []
tt BufRead (use std::io::BufRead) [requires_import]
st BufReader (use std::io::BufReader) [requires_import]
st BufWriter (use std::io::BufWriter) [requires_import]
"#]],
);
}
}

0 comments on commit b24b82f

Please sign in to comment.