From 12fe301a0cbe4ffecdabae1c9b827e740e3ce027 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 9 Mar 2021 18:06:08 +0300 Subject: Cleanup auto-ref in completion --- crates/ide_completion/src/item.rs | 13 +++--- crates/ide_completion/src/render.rs | 90 ++++++++++++++++++++++++++++++------- 2 files changed, 81 insertions(+), 22 deletions(-) (limited to 'crates/ide_completion') diff --git a/crates/ide_completion/src/item.rs b/crates/ide_completion/src/item.rs index b16f0775a..5e8ed75f1 100644 --- a/crates/ide_completion/src/item.rs +++ b/crates/ide_completion/src/item.rs @@ -68,7 +68,7 @@ pub struct CompletionItem { /// Indicates that a reference or mutable reference to this variable is a /// possible match. - ref_match: Option<(Mutability, CompletionScore)>, + ref_match: Option, /// The import data to add to completion's edits. import_to_add: Option, @@ -104,6 +104,9 @@ impl fmt::Debug for CompletionItem { if let Some(score) = &self.score { s.field("score", score); } + if let Some(mutability) = &self.ref_match { + s.field("ref_match", &format!("&{}", mutability.as_keyword_for_ref())); + } if self.trigger_call_info { s.field("trigger_call_info", &true); } @@ -261,7 +264,7 @@ impl CompletionItem { self.trigger_call_info } - pub fn ref_match(&self) -> Option<(Mutability, CompletionScore)> { + pub fn ref_match(&self) -> Option { self.ref_match } @@ -311,7 +314,7 @@ pub(crate) struct Builder { deprecated: bool, trigger_call_info: Option, score: Option, - ref_match: Option<(Mutability, CompletionScore)>, + ref_match: Option, } impl Builder { @@ -430,8 +433,8 @@ impl Builder { self.import_to_add = import_to_add; self } - pub(crate) fn ref_match(mut self, ref_match: (Mutability, CompletionScore)) -> Builder { - self.ref_match = Some(ref_match); + pub(crate) fn ref_match(mut self, mutability: Mutability) -> Builder { + self.ref_match = Some(mutability); self } } diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs index 0a6ac8804..0a1b0f95d 100644 --- a/crates/ide_completion/src/render.rs +++ b/crates/ide_completion/src/render.rs @@ -254,10 +254,17 @@ impl<'a> Render<'a> { { item = item.set_score(score); } - if let Some(ref_match) = - refed_type_matches(&active_type, &active_name, &ty, &local_name) - { - item = item.ref_match(ref_match); + + if let Some(ty_without_ref) = active_type.remove_ref() { + if ty_without_ref == ty { + cov_mark::hit!(suggest_ref); + let mutability = if active_type.is_mutable_reference() { + Mutability::Mut + } else { + Mutability::Shared + }; + item = item.ref_match(mutability) + } } } } @@ -340,19 +347,6 @@ fn compute_score_from_active( Some(res) } -fn refed_type_matches( - active_type: &Type, - active_name: &str, - ty: &Type, - name: &str, -) -> Option<(Mutability, CompletionScore)> { - let derefed_active = active_type.remove_ref()?; - let score = compute_score_from_active(&derefed_active, &active_name, &ty, &name)?; - Some(( - if active_type.is_mutable_reference() { Mutability::Mut } else { Mutability::Shared }, - score, - )) -} fn compute_score(ctx: &RenderContext, ty: &Type, name: &str) -> Option { let (active_name, active_type) = ctx.active_name_and_type()?; @@ -947,4 +941,66 @@ fn f(foo: &Foo) { f(foo, w$0) } "#]], ); } + + #[test] + fn suggest_ref_mut() { + cov_mark::check!(suggest_ref); + check( + r#" +struct S; +fn foo(s: &mut S) {} +fn main() { + let mut s = S; + foo($0); +} + "#, + expect![[r#" + [ + CompletionItem { + label: "S", + source_range: 70..70, + delete: 70..70, + insert: "S", + kind: SymbolKind( + Struct, + ), + }, + CompletionItem { + label: "foo(…)", + source_range: 70..70, + delete: 70..70, + insert: "foo(${1:&mut s})$0", + kind: SymbolKind( + Function, + ), + lookup: "foo", + detail: "-> ()", + trigger_call_info: true, + }, + CompletionItem { + label: "main()", + source_range: 70..70, + delete: 70..70, + insert: "main()$0", + kind: SymbolKind( + Function, + ), + lookup: "main", + detail: "-> ()", + }, + CompletionItem { + label: "s", + source_range: 70..70, + delete: 70..70, + insert: "s", + kind: SymbolKind( + Local, + ), + detail: "S", + ref_match: "&mut ", + }, + ] + "#]], + ) + } } -- cgit v1.2.3