diff options
Diffstat (limited to 'crates/ide_completion/src/render.rs')
-rw-r--r-- | crates/ide_completion/src/render.rs | 90 |
1 files changed, 73 insertions, 17 deletions
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> { | |||
254 | { | 254 | { |
255 | item = item.set_score(score); | 255 | item = item.set_score(score); |
256 | } | 256 | } |
257 | if let Some(ref_match) = | 257 | |
258 | refed_type_matches(&active_type, &active_name, &ty, &local_name) | 258 | if let Some(ty_without_ref) = active_type.remove_ref() { |
259 | { | 259 | if ty_without_ref == ty { |
260 | item = item.ref_match(ref_match); | 260 | cov_mark::hit!(suggest_ref); |
261 | let mutability = if active_type.is_mutable_reference() { | ||
262 | Mutability::Mut | ||
263 | } else { | ||
264 | Mutability::Shared | ||
265 | }; | ||
266 | item = item.ref_match(mutability) | ||
267 | } | ||
261 | } | 268 | } |
262 | } | 269 | } |
263 | } | 270 | } |
@@ -340,19 +347,6 @@ fn compute_score_from_active( | |||
340 | 347 | ||
341 | Some(res) | 348 | Some(res) |
342 | } | 349 | } |
343 | fn refed_type_matches( | ||
344 | active_type: &Type, | ||
345 | active_name: &str, | ||
346 | ty: &Type, | ||
347 | name: &str, | ||
348 | ) -> Option<(Mutability, CompletionScore)> { | ||
349 | let derefed_active = active_type.remove_ref()?; | ||
350 | let score = compute_score_from_active(&derefed_active, &active_name, &ty, &name)?; | ||
351 | Some(( | ||
352 | if active_type.is_mutable_reference() { Mutability::Mut } else { Mutability::Shared }, | ||
353 | score, | ||
354 | )) | ||
355 | } | ||
356 | 350 | ||
357 | fn compute_score(ctx: &RenderContext, ty: &Type, name: &str) -> Option<CompletionScore> { | 351 | fn compute_score(ctx: &RenderContext, ty: &Type, name: &str) -> Option<CompletionScore> { |
358 | let (active_name, active_type) = ctx.active_name_and_type()?; | 352 | let (active_name, active_type) = ctx.active_name_and_type()?; |
@@ -947,4 +941,66 @@ fn f(foo: &Foo) { f(foo, w$0) } | |||
947 | "#]], | 941 | "#]], |
948 | ); | 942 | ); |
949 | } | 943 | } |
944 | |||
945 | #[test] | ||
946 | fn suggest_ref_mut() { | ||
947 | cov_mark::check!(suggest_ref); | ||
948 | check( | ||
949 | r#" | ||
950 | struct S; | ||
951 | fn foo(s: &mut S) {} | ||
952 | fn main() { | ||
953 | let mut s = S; | ||
954 | foo($0); | ||
955 | } | ||
956 | "#, | ||
957 | expect![[r#" | ||
958 | [ | ||
959 | CompletionItem { | ||
960 | label: "S", | ||
961 | source_range: 70..70, | ||
962 | delete: 70..70, | ||
963 | insert: "S", | ||
964 | kind: SymbolKind( | ||
965 | Struct, | ||
966 | ), | ||
967 | }, | ||
968 | CompletionItem { | ||
969 | label: "foo(…)", | ||
970 | source_range: 70..70, | ||
971 | delete: 70..70, | ||
972 | insert: "foo(${1:&mut s})$0", | ||
973 | kind: SymbolKind( | ||
974 | Function, | ||
975 | ), | ||
976 | lookup: "foo", | ||
977 | detail: "-> ()", | ||
978 | trigger_call_info: true, | ||
979 | }, | ||
980 | CompletionItem { | ||
981 | label: "main()", | ||
982 | source_range: 70..70, | ||
983 | delete: 70..70, | ||
984 | insert: "main()$0", | ||
985 | kind: SymbolKind( | ||
986 | Function, | ||
987 | ), | ||
988 | lookup: "main", | ||
989 | detail: "-> ()", | ||
990 | }, | ||
991 | CompletionItem { | ||
992 | label: "s", | ||
993 | source_range: 70..70, | ||
994 | delete: 70..70, | ||
995 | insert: "s", | ||
996 | kind: SymbolKind( | ||
997 | Local, | ||
998 | ), | ||
999 | detail: "S", | ||
1000 | ref_match: "&mut ", | ||
1001 | }, | ||
1002 | ] | ||
1003 | "#]], | ||
1004 | ) | ||
1005 | } | ||
950 | } | 1006 | } |