aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_completion')
-rw-r--r--crates/ide_completion/src/completions/dot.rs24
-rw-r--r--crates/ide_completion/src/completions/flyimport.rs8
-rw-r--r--crates/ide_completion/src/completions/fn_param.rs2
-rw-r--r--crates/ide_completion/src/completions/qualified_path.rs56
-rw-r--r--crates/ide_completion/src/completions/trait_impl.rs7
-rw-r--r--crates/ide_completion/src/completions/unqualified_path.rs50
-rw-r--r--crates/ide_completion/src/context.rs15
-rw-r--r--crates/ide_completion/src/lib.rs6
-rw-r--r--crates/ide_completion/src/patterns.rs16
-rw-r--r--crates/ide_completion/src/render.rs236
-rw-r--r--crates/ide_completion/src/render/enum_variant.rs13
-rw-r--r--crates/ide_completion/src/render/function.rs66
12 files changed, 345 insertions, 154 deletions
diff --git a/crates/ide_completion/src/completions/dot.rs b/crates/ide_completion/src/completions/dot.rs
index 5ee9a9f07..cec2d0c3a 100644
--- a/crates/ide_completion/src/completions/dot.rs
+++ b/crates/ide_completion/src/completions/dot.rs
@@ -81,7 +81,7 @@ fn foo(s: S) { s.$0 }
81"#, 81"#,
82 expect![[r#" 82 expect![[r#"
83 fd foo u32 83 fd foo u32
84 me bar() -> () 84 me bar() fn(&self)
85 "#]], 85 "#]],
86 ); 86 );
87 } 87 }
@@ -97,7 +97,7 @@ impl S {
97"#, 97"#,
98 expect![[r#" 98 expect![[r#"
99 fd the_field (u32,) 99 fd the_field (u32,)
100 me foo() -> () 100 me foo() fn(self)
101 "#]], 101 "#]],
102 ) 102 )
103 } 103 }
@@ -113,7 +113,7 @@ impl A {
113"#, 113"#,
114 expect![[r#" 114 expect![[r#"
115 fd the_field (u32, i32) 115 fd the_field (u32, i32)
116 me foo() -> () 116 me foo() fn(&self)
117 "#]], 117 "#]],
118 ) 118 )
119 } 119 }
@@ -163,7 +163,7 @@ mod m {
163fn foo(a: A) { a.$0 } 163fn foo(a: A) { a.$0 }
164"#, 164"#,
165 expect![[r#" 165 expect![[r#"
166 me the_method() -> () 166 me the_method() fn(&self)
167 "#]], 167 "#]],
168 ); 168 );
169 } 169 }
@@ -196,7 +196,7 @@ impl A<i32> {
196fn foo(a: A<u32>) { a.$0 } 196fn foo(a: A<u32>) { a.$0 }
197"#, 197"#,
198 expect![[r#" 198 expect![[r#"
199 me the_method() -> () 199 me the_method() fn(&self)
200 "#]], 200 "#]],
201 ) 201 )
202 } 202 }
@@ -211,7 +211,7 @@ impl Trait for A {}
211fn foo(a: A) { a.$0 } 211fn foo(a: A) { a.$0 }
212"#, 212"#,
213 expect![[r#" 213 expect![[r#"
214 me the_method() -> () 214 me the_method() fn(&self)
215 "#]], 215 "#]],
216 ); 216 );
217 } 217 }
@@ -226,7 +226,7 @@ impl<T> Trait for T {}
226fn foo(a: &A) { a.$0 } 226fn foo(a: &A) { a.$0 }
227", 227",
228 expect![[r#" 228 expect![[r#"
229 me the_method() -> () 229 me the_method() fn(&self)
230 "#]], 230 "#]],
231 ); 231 );
232 } 232 }
@@ -244,7 +244,7 @@ impl Trait for A {}
244fn foo(a: A) { a.$0 } 244fn foo(a: A) { a.$0 }
245", 245",
246 expect![[r#" 246 expect![[r#"
247 me the_method() -> () 247 me the_method() fn(&self)
248 "#]], 248 "#]],
249 ); 249 );
250 } 250 }
@@ -298,7 +298,7 @@ impl T {
298} 298}
299"#, 299"#,
300 expect![[r#" 300 expect![[r#"
301 me blah() -> () 301 me blah() fn(&self)
302 "#]], 302 "#]],
303 ); 303 );
304 } 304 }
@@ -407,7 +407,7 @@ fn foo() {
407} 407}
408"#, 408"#,
409 expect![[r#" 409 expect![[r#"
410 me the_method() -> () 410 me the_method() fn(&self)
411 "#]], 411 "#]],
412 ); 412 );
413 } 413 }
@@ -422,7 +422,7 @@ macro_rules! make_s { () => { S }; }
422fn main() { make_s!().f$0; } 422fn main() { make_s!().f$0; }
423"#, 423"#,
424 expect![[r#" 424 expect![[r#"
425 me foo() -> () 425 me foo() fn(&self)
426 "#]], 426 "#]],
427 ) 427 )
428 } 428 }
@@ -450,7 +450,7 @@ mod foo {
450} 450}
451 "#, 451 "#,
452 expect![[r#" 452 expect![[r#"
453 me private() -> () 453 me private() fn(&self)
454 "#]], 454 "#]],
455 ); 455 );
456 } 456 }
diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs
index 391a11c91..08df2df3f 100644
--- a/crates/ide_completion/src/completions/flyimport.rs
+++ b/crates/ide_completion/src/completions/flyimport.rs
@@ -402,7 +402,7 @@ fn main() {
402 check( 402 check(
403 fixture, 403 fixture,
404 expect![[r#" 404 expect![[r#"
405 fn weird_function() (dep::test_mod::TestTrait) -> () 405 fn weird_function() (dep::test_mod::TestTrait) fn()
406 "#]], 406 "#]],
407 ); 407 );
408 408
@@ -495,7 +495,7 @@ fn main() {
495 check( 495 check(
496 fixture, 496 fixture,
497 expect![[r#" 497 expect![[r#"
498 me random_method() (dep::test_mod::TestTrait) -> () 498 me random_method() (dep::test_mod::TestTrait) fn(&self)
499 "#]], 499 "#]],
500 ); 500 );
501 501
@@ -665,7 +665,7 @@ fn main() {
665} 665}
666 "#, 666 "#,
667 expect![[r#" 667 expect![[r#"
668 me random_method() (dep::test_mod::TestTrait) -> () DEPRECATED 668 me random_method() (dep::test_mod::TestTrait) fn(&self) DEPRECATED
669 "#]], 669 "#]],
670 ); 670 );
671 671
@@ -696,7 +696,7 @@ fn main() {
696"#, 696"#,
697 expect![[r#" 697 expect![[r#"
698 ct SPECIAL_CONST (dep::test_mod::TestTrait) DEPRECATED 698 ct SPECIAL_CONST (dep::test_mod::TestTrait) DEPRECATED
699 fn weird_function() (dep::test_mod::TestTrait) -> () DEPRECATED 699 fn weird_function() (dep::test_mod::TestTrait) fn() DEPRECATED
700 "#]], 700 "#]],
701 ); 701 );
702 } 702 }
diff --git a/crates/ide_completion/src/completions/fn_param.rs b/crates/ide_completion/src/completions/fn_param.rs
index 0243dce56..0ea558489 100644
--- a/crates/ide_completion/src/completions/fn_param.rs
+++ b/crates/ide_completion/src/completions/fn_param.rs
@@ -33,7 +33,7 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
33 }); 33 });
34 }; 34 };
35 35
36 for node in ctx.token.parent().ancestors() { 36 for node in ctx.token.ancestors() {
37 match_ast! { 37 match_ast! {
38 match node { 38 match node {
39 ast::SourceFile(it) => it.items().filter_map(|item| match item { 39 ast::SourceFile(it) => it.items().filter_map(|item| match item {
diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs
index df74b739e..105ff6013 100644
--- a/crates/ide_completion/src/completions/qualified_path.rs
+++ b/crates/ide_completion/src/completions/qualified_path.rs
@@ -359,8 +359,8 @@ impl S {
359fn foo() { let _ = S::$0 } 359fn foo() { let _ = S::$0 }
360"#, 360"#,
361 expect![[r#" 361 expect![[r#"
362 fn a() -> () 362 fn a() fn()
363 me b(…) -> () 363 me b(…) fn(&self)
364 ct C const C: i32 = 42; 364 ct C const C: i32 = 42;
365 ta T type T = i32; 365 ta T type T = i32;
366 "#]], 366 "#]],
@@ -387,7 +387,7 @@ mod m {
387fn foo() { let _ = S::$0 } 387fn foo() { let _ = S::$0 }
388"#, 388"#,
389 expect![[r#" 389 expect![[r#"
390 fn public_method() -> () 390 fn public_method() fn()
391 ct PUBLIC_CONST pub(crate) const PUBLIC_CONST: u32 = 1; 391 ct PUBLIC_CONST pub(crate) const PUBLIC_CONST: u32 = 1;
392 ta PublicType pub(crate) type PublicType = u32; 392 ta PublicType pub(crate) type PublicType = u32;
393 "#]], 393 "#]],
@@ -404,7 +404,7 @@ impl E { fn m() { } }
404fn foo() { let _ = E::$0 } 404fn foo() { let _ = E::$0 }
405 "#, 405 "#,
406 expect![[r#" 406 expect![[r#"
407 fn m() -> () 407 fn m() fn()
408 "#]], 408 "#]],
409 ); 409 );
410 } 410 }
@@ -419,7 +419,7 @@ impl U { fn m() { } }
419fn foo() { let _ = U::$0 } 419fn foo() { let _ = U::$0 }
420"#, 420"#,
421 expect![[r#" 421 expect![[r#"
422 fn m() -> () 422 fn m() fn()
423 "#]], 423 "#]],
424 ); 424 );
425 } 425 }
@@ -449,7 +449,7 @@ trait Trait { fn m(); }
449fn foo() { let _ = Trait::$0 } 449fn foo() { let _ = Trait::$0 }
450"#, 450"#,
451 expect![[r#" 451 expect![[r#"
452 fn m() -> () 452 fn m() fn()
453 "#]], 453 "#]],
454 ); 454 );
455 } 455 }
@@ -466,7 +466,7 @@ impl Trait for S {}
466fn foo() { let _ = S::$0 } 466fn foo() { let _ = S::$0 }
467"#, 467"#,
468 expect![[r#" 468 expect![[r#"
469 fn m() -> () 469 fn m() fn()
470 "#]], 470 "#]],
471 ); 471 );
472 } 472 }
@@ -483,7 +483,7 @@ impl Trait for S {}
483fn foo() { let _ = <S as Trait>::$0 } 483fn foo() { let _ = <S as Trait>::$0 }
484"#, 484"#,
485 expect![[r#" 485 expect![[r#"
486 fn m() -> () 486 fn m() fn()
487 "#]], 487 "#]],
488 ); 488 );
489 } 489 }
@@ -512,11 +512,11 @@ fn foo<T: Sub>() { T::$0 }
512 ta SubTy type SubTy; 512 ta SubTy type SubTy;
513 ta Ty type Ty; 513 ta Ty type Ty;
514 ct C2 const C2: (); 514 ct C2 const C2: ();
515 fn subfunc() -> () 515 fn subfunc() fn()
516 me submethod(…) -> () 516 me submethod(…) fn(&self)
517 ct CONST const CONST: u8; 517 ct CONST const CONST: u8;
518 fn func() -> () 518 fn func() fn()
519 me method(…) -> () 519 me method(…) fn(&self)
520 "#]], 520 "#]],
521 ); 521 );
522 } 522 }
@@ -552,11 +552,11 @@ impl<T> Sub for Wrap<T> {
552 ta SubTy type SubTy; 552 ta SubTy type SubTy;
553 ta Ty type Ty; 553 ta Ty type Ty;
554 ct CONST const CONST: u8 = 0; 554 ct CONST const CONST: u8 = 0;
555 fn func() -> () 555 fn func() fn()
556 me method(…) -> () 556 me method(…) fn(&self)
557 ct C2 const C2: () = (); 557 ct C2 const C2: () = ();
558 fn subfunc() -> () 558 fn subfunc() fn()
559 me submethod(…) -> () 559 me submethod(…) fn(&self)
560 "#]], 560 "#]],
561 ); 561 );
562 } 562 }
@@ -573,8 +573,8 @@ impl T { fn bar() {} }
573fn main() { T::$0; } 573fn main() { T::$0; }
574"#, 574"#,
575 expect![[r#" 575 expect![[r#"
576 fn foo() -> () 576 fn foo() fn()
577 fn bar() -> () 577 fn bar() fn()
578 "#]], 578 "#]],
579 ); 579 );
580 } 580 }
@@ -589,7 +589,7 @@ macro_rules! foo { () => {} }
589fn main() { let _ = crate::$0 } 589fn main() { let _ = crate::$0 }
590 "#, 590 "#,
591 expect![[r##" 591 expect![[r##"
592 fn main() -> () 592 fn main() fn()
593 ma foo!(…) #[macro_export] macro_rules! foo 593 ma foo!(…) #[macro_export] macro_rules! foo
594 "##]], 594 "##]],
595 ); 595 );
@@ -633,7 +633,7 @@ mod p {
633"#, 633"#,
634 expect![[r#" 634 expect![[r#"
635 ct RIGHT_CONST 635 ct RIGHT_CONST
636 fn right_fn() -> () 636 fn right_fn() fn()
637 st RightType 637 st RightType
638 "#]], 638 "#]],
639 ); 639 );
@@ -680,8 +680,8 @@ fn main() { m!(self::f$0); }
680fn foo() {} 680fn foo() {}
681"#, 681"#,
682 expect![[r#" 682 expect![[r#"
683 fn main() -> () 683 fn main() fn()
684 fn foo() -> () 684 fn foo() fn()
685 "#]], 685 "#]],
686 ); 686 );
687 } 687 }
@@ -699,7 +699,7 @@ mod m {
699"#, 699"#,
700 expect![[r#" 700 expect![[r#"
701 md z 701 md z
702 fn z() -> () 702 fn z() fn()
703 "#]], 703 "#]],
704 ); 704 );
705 } 705 }
@@ -719,7 +719,7 @@ fn foo() {
719} 719}
720"#, 720"#,
721 expect![[r#" 721 expect![[r#"
722 fn new() -> HashMap<K, V, RandomState> 722 fn new() fn() -> HashMap<K, V, RandomState>
723 "#]], 723 "#]],
724 ); 724 );
725 } 725 }
@@ -752,8 +752,8 @@ fn main() {
752} 752}
753"#, 753"#,
754 expect![[r#" 754 expect![[r#"
755 fn main() -> () 755 fn main() fn()
756 fn foo(…) -> () 756 fn foo(…) fn(i32, i32)
757 "#]], 757 "#]],
758 ); 758 );
759 } 759 }
@@ -776,7 +776,7 @@ impl Foo {
776 expect![[r#" 776 expect![[r#"
777 ev Bar () 777 ev Bar ()
778 ev Baz () 778 ev Baz ()
779 me foo(…) -> () 779 me foo(…) fn(self)
780 "#]], 780 "#]],
781 ); 781 );
782 } 782 }
@@ -800,7 +800,7 @@ impl u8 {
800"#, 800"#,
801 expect![[r#" 801 expect![[r#"
802 ct MAX pub const MAX: Self = 255; 802 ct MAX pub const MAX: Self = 255;
803 me func(…) -> () 803 me func(…) fn(self)
804 "#]], 804 "#]],
805 ); 805 );
806 } 806 }
diff --git a/crates/ide_completion/src/completions/trait_impl.rs b/crates/ide_completion/src/completions/trait_impl.rs
index 5a7361f8e..a26fe7c6c 100644
--- a/crates/ide_completion/src/completions/trait_impl.rs
+++ b/crates/ide_completion/src/completions/trait_impl.rs
@@ -82,13 +82,14 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
82 82
83fn completion_match(ctx: &CompletionContext) -> Option<(ImplCompletionKind, SyntaxNode, Impl)> { 83fn completion_match(ctx: &CompletionContext) -> Option<(ImplCompletionKind, SyntaxNode, Impl)> {
84 let mut token = ctx.token.clone(); 84 let mut token = ctx.token.clone();
85 // For keywork without name like `impl .. { fn $0 }`, the current position is inside 85 // For keyword without name like `impl .. { fn $0 }`, the current position is inside
86 // the whitespace token, which is outside `FN` syntax node. 86 // the whitespace token, which is outside `FN` syntax node.
87 // We need to follow the previous token in this case. 87 // We need to follow the previous token in this case.
88 if token.kind() == SyntaxKind::WHITESPACE { 88 if token.kind() == SyntaxKind::WHITESPACE {
89 token = token.prev_token()?; 89 token = token.prev_token()?;
90 } 90 }
91 91
92 let parent_kind = token.parent().map_or(SyntaxKind::EOF, |it| it.kind());
92 let impl_item_offset = match token.kind() { 93 let impl_item_offset = match token.kind() {
93 // `impl .. { const $0 }` 94 // `impl .. { const $0 }`
94 // ERROR 0 95 // ERROR 0
@@ -102,14 +103,14 @@ fn completion_match(ctx: &CompletionContext) -> Option<(ImplCompletionKind, Synt
102 // FN/TYPE_ALIAS/CONST 1 103 // FN/TYPE_ALIAS/CONST 1
103 // NAME 0 104 // NAME 0
104 // IDENT <- * 105 // IDENT <- *
105 SyntaxKind::IDENT if token.parent().kind() == SyntaxKind::NAME => 1, 106 SyntaxKind::IDENT if parent_kind == SyntaxKind::NAME => 1,
106 // `impl .. { foo$0 }` 107 // `impl .. { foo$0 }`
107 // MACRO_CALL 3 108 // MACRO_CALL 3
108 // PATH 2 109 // PATH 2
109 // PATH_SEGMENT 1 110 // PATH_SEGMENT 1
110 // NAME_REF 0 111 // NAME_REF 0
111 // IDENT <- * 112 // IDENT <- *
112 SyntaxKind::IDENT if token.parent().kind() == SyntaxKind::NAME_REF => 3, 113 SyntaxKind::IDENT if parent_kind == SyntaxKind::NAME_REF => 3,
113 _ => return None, 114 _ => return None,
114 }; 115 };
115 116
diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs
index 5ef80f6a7..1b8b063e7 100644
--- a/crates/ide_completion/src/completions/unqualified_path.rs
+++ b/crates/ide_completion/src/completions/unqualified_path.rs
@@ -135,7 +135,7 @@ fn quux(x: i32) {
135 expect![[r#" 135 expect![[r#"
136 lc y i32 136 lc y i32
137 lc x i32 137 lc x i32
138 fn quux(…) -> () 138 fn quux(…) fn(i32)
139 "#]], 139 "#]],
140 ); 140 );
141 } 141 }
@@ -157,7 +157,7 @@ fn quux() {
157 expect![[r#" 157 expect![[r#"
158 lc b i32 158 lc b i32
159 lc a 159 lc a
160 fn quux() -> () 160 fn quux() fn()
161 "#]], 161 "#]],
162 ); 162 );
163 } 163 }
@@ -172,7 +172,7 @@ fn quux() {
172"#, 172"#,
173 expect![[r#" 173 expect![[r#"
174 lc x 174 lc x
175 fn quux() -> () 175 fn quux() fn()
176 "#]], 176 "#]],
177 ); 177 );
178 } 178 }
@@ -203,14 +203,14 @@ fn main() {
203 r#"fn quux<T>() { $0 }"#, 203 r#"fn quux<T>() { $0 }"#,
204 expect![[r#" 204 expect![[r#"
205 tp T 205 tp T
206 fn quux() -> () 206 fn quux() fn()
207 "#]], 207 "#]],
208 ); 208 );
209 check( 209 check(
210 r#"fn quux<const C: usize>() { $0 }"#, 210 r#"fn quux<const C: usize>() { $0 }"#,
211 expect![[r#" 211 expect![[r#"
212 cp C 212 cp C
213 fn quux() -> () 213 fn quux() fn()
214 "#]], 214 "#]],
215 ); 215 );
216 } 216 }
@@ -221,7 +221,7 @@ fn main() {
221 check( 221 check(
222 r#"fn quux<'a>() { $0 }"#, 222 r#"fn quux<'a>() { $0 }"#,
223 expect![[r#" 223 expect![[r#"
224 fn quux() -> () 224 fn quux() fn()
225 "#]], 225 "#]],
226 ); 226 );
227 } 227 }
@@ -259,7 +259,7 @@ fn quux() { $0 }
259"#, 259"#,
260 expect![[r#" 260 expect![[r#"
261 st S 261 st S
262 fn quux() -> () 262 fn quux() fn()
263 en E 263 en E
264 "#]], 264 "#]],
265 ); 265 );
@@ -312,7 +312,7 @@ mod m {
312} 312}
313"#, 313"#,
314 expect![[r#" 314 expect![[r#"
315 fn quux() -> () 315 fn quux() fn()
316 st Bar 316 st Bar
317 "#]], 317 "#]],
318 ); 318 );
@@ -327,7 +327,7 @@ fn x() -> $0
327"#, 327"#,
328 expect![[r#" 328 expect![[r#"
329 st Foo 329 st Foo
330 fn x() -> () 330 fn x() fn()
331 "#]], 331 "#]],
332 ); 332 );
333 } 333 }
@@ -348,7 +348,7 @@ fn foo() {
348 expect![[r#" 348 expect![[r#"
349 lc bar i32 349 lc bar i32
350 lc bar i32 350 lc bar i32
351 fn foo() -> () 351 fn foo() fn()
352 "#]], 352 "#]],
353 ); 353 );
354 } 354 }
@@ -378,7 +378,7 @@ use prelude::*;
378mod prelude { struct Option; } 378mod prelude { struct Option; }
379"#, 379"#,
380 expect![[r#" 380 expect![[r#"
381 fn foo() -> () 381 fn foo() fn()
382 md std 382 md std
383 st Option 383 st Option
384 "#]], 384 "#]],
@@ -408,7 +408,7 @@ mod macros {
408} 408}
409"#, 409"#,
410 expect![[r##" 410 expect![[r##"
411 fn f() -> () 411 fn f() fn()
412 ma concat!(…) #[macro_export] macro_rules! concat 412 ma concat!(…) #[macro_export] macro_rules! concat
413 md std 413 md std
414 "##]], 414 "##]],
@@ -435,7 +435,7 @@ use prelude::*;
435mod prelude { struct String; } 435mod prelude { struct String; }
436"#, 436"#,
437 expect![[r#" 437 expect![[r#"
438 fn foo() -> () 438 fn foo() fn()
439 md std 439 md std
440 md core 440 md core
441 st String 441 st String
@@ -466,7 +466,7 @@ fn main() { let v = $0 }
466 expect![[r##" 466 expect![[r##"
467 md m1 467 md m1
468 ma baz!(…) #[macro_export] macro_rules! baz 468 ma baz!(…) #[macro_export] macro_rules! baz
469 fn main() -> () 469 fn main() fn()
470 md m2 470 md m2
471 ma bar!(…) macro_rules! bar 471 ma bar!(…) macro_rules! bar
472 ma foo!(…) macro_rules! foo 472 ma foo!(…) macro_rules! foo
@@ -482,7 +482,7 @@ macro_rules! foo { () => {} }
482fn foo() { $0 } 482fn foo() { $0 }
483"#, 483"#,
484 expect![[r#" 484 expect![[r#"
485 fn foo() -> () 485 fn foo() fn()
486 ma foo!(…) macro_rules! foo 486 ma foo!(…) macro_rules! foo
487 "#]], 487 "#]],
488 ); 488 );
@@ -496,7 +496,7 @@ macro_rules! foo { () => {} }
496fn main() { let x: $0 } 496fn main() { let x: $0 }
497"#, 497"#,
498 expect![[r#" 498 expect![[r#"
499 fn main() -> () 499 fn main() fn()
500 ma foo!(…) macro_rules! foo 500 ma foo!(…) macro_rules! foo
501 "#]], 501 "#]],
502 ); 502 );
@@ -510,7 +510,7 @@ macro_rules! foo { () => {} }
510fn main() { $0 } 510fn main() { $0 }
511"#, 511"#,
512 expect![[r#" 512 expect![[r#"
513 fn main() -> () 513 fn main() fn()
514 ma foo!(…) macro_rules! foo 514 ma foo!(…) macro_rules! foo
515 "#]], 515 "#]],
516 ); 516 );
@@ -526,8 +526,8 @@ fn main() {
526} 526}
527"#, 527"#,
528 expect![[r#" 528 expect![[r#"
529 fn frobnicate() -> () 529 fn frobnicate() fn()
530 fn main() -> () 530 fn main() fn()
531 "#]], 531 "#]],
532 ); 532 );
533 } 533 }
@@ -545,7 +545,7 @@ fn quux(x: i32) {
545 expect![[r#" 545 expect![[r#"
546 lc y i32 546 lc y i32
547 lc x i32 547 lc x i32
548 fn quux(…) -> () 548 fn quux(…) fn(i32)
549 ma m!(…) macro_rules! m 549 ma m!(…) macro_rules! m
550 "#]], 550 "#]],
551 ); 551 );
@@ -564,7 +564,7 @@ fn quux(x: i32) {
564 expect![[r#" 564 expect![[r#"
565 lc y i32 565 lc y i32
566 lc x i32 566 lc x i32
567 fn quux(…) -> () 567 fn quux(…) fn(i32)
568 ma m!(…) macro_rules! m 568 ma m!(…) macro_rules! m
569 "#]], 569 "#]],
570 ); 570 );
@@ -583,7 +583,7 @@ fn quux(x: i32) {
583 expect![[r#" 583 expect![[r#"
584 lc y i32 584 lc y i32
585 lc x i32 585 lc x i32
586 fn quux(…) -> () 586 fn quux(…) fn(i32)
587 ma m!(…) macro_rules! m 587 ma m!(…) macro_rules! m
588 "#]], 588 "#]],
589 ); 589 );
@@ -598,7 +598,7 @@ use spam::Quux;
598fn main() { $0 } 598fn main() { $0 }
599"#, 599"#,
600 expect![[r#" 600 expect![[r#"
601 fn main() -> () 601 fn main() fn()
602 ?? Quux 602 ?? Quux
603 "#]], 603 "#]],
604 ); 604 );
@@ -616,7 +616,7 @@ fn main() { let foo: Foo = Q$0 }
616 ev Foo::Baz () 616 ev Foo::Baz ()
617 ev Foo::Quux () 617 ev Foo::Quux ()
618 en Foo 618 en Foo
619 fn main() -> () 619 fn main() fn()
620 "#]], 620 "#]],
621 ) 621 )
622 } 622 }
@@ -631,7 +631,7 @@ fn f() -> m::E { V$0 }
631 expect![[r#" 631 expect![[r#"
632 ev m::E::V () 632 ev m::E::V ()
633 md m 633 md m
634 fn f() -> E 634 fn f() fn() -> E
635 "#]], 635 "#]],
636 ) 636 )
637 } 637 }
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs
index 5a081de7e..6d57da06a 100644
--- a/crates/ide_completion/src/context.rs
+++ b/crates/ide_completion/src/context.rs
@@ -120,7 +120,7 @@ impl<'a> CompletionContext<'a> {
120 let original_token = 120 let original_token =
121 original_file.syntax().token_at_offset(position.offset).left_biased()?; 121 original_file.syntax().token_at_offset(position.offset).left_biased()?;
122 let token = sema.descend_into_macros(original_token.clone()); 122 let token = sema.descend_into_macros(original_token.clone());
123 let scope = sema.scope_at_offset(&token.parent(), position.offset); 123 let scope = sema.scope_at_offset(&token, position.offset);
124 let mut locals = vec![]; 124 let mut locals = vec![];
125 scope.process_all_names(&mut |name, scope| { 125 scope.process_all_names(&mut |name, scope| {
126 if let ScopeDef::Local(local) = scope { 126 if let ScopeDef::Local(local) = scope {
@@ -281,7 +281,7 @@ impl<'a> CompletionContext<'a> {
281 fn fill_impl_def(&mut self) { 281 fn fill_impl_def(&mut self) {
282 self.impl_def = self 282 self.impl_def = self
283 .sema 283 .sema
284 .ancestors_with_macros(self.token.parent()) 284 .token_ancestors_with_macros(self.token.clone())
285 .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE) 285 .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
286 .find_map(ast::Impl::cast); 286 .find_map(ast::Impl::cast);
287 } 287 }
@@ -293,7 +293,10 @@ impl<'a> CompletionContext<'a> {
293 offset: TextSize, 293 offset: TextSize,
294 ) { 294 ) {
295 let expected = { 295 let expected = {
296 let mut node = self.token.parent(); 296 let mut node = match self.token.parent() {
297 Some(it) => it,
298 None => return,
299 };
297 loop { 300 loop {
298 let ret = match_ast! { 301 let ret = match_ast! {
299 match node { 302 match node {
@@ -474,17 +477,17 @@ impl<'a> CompletionContext<'a> {
474 } 477 }
475 478
476 self.use_item_syntax = 479 self.use_item_syntax =
477 self.sema.ancestors_with_macros(self.token.parent()).find_map(ast::Use::cast); 480 self.sema.token_ancestors_with_macros(self.token.clone()).find_map(ast::Use::cast);
478 481
479 self.function_syntax = self 482 self.function_syntax = self
480 .sema 483 .sema
481 .ancestors_with_macros(self.token.parent()) 484 .token_ancestors_with_macros(self.token.clone())
482 .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE) 485 .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
483 .find_map(ast::Fn::cast); 486 .find_map(ast::Fn::cast);
484 487
485 self.record_field_syntax = self 488 self.record_field_syntax = self
486 .sema 489 .sema
487 .ancestors_with_macros(self.token.parent()) 490 .token_ancestors_with_macros(self.token.clone())
488 .take_while(|it| { 491 .take_while(|it| {
489 it.kind() != SOURCE_FILE && it.kind() != MODULE && it.kind() != CALL_EXPR 492 it.kind() != SOURCE_FILE && it.kind() != MODULE && it.kind() != CALL_EXPR
490 }) 493 })
diff --git a/crates/ide_completion/src/lib.rs b/crates/ide_completion/src/lib.rs
index 263554ecf..5b7ad38d5 100644
--- a/crates/ide_completion/src/lib.rs
+++ b/crates/ide_completion/src/lib.rs
@@ -230,7 +230,7 @@ fn foo() {
230 bar.fo$0; 230 bar.fo$0;
231} 231}
232"#, 232"#,
233 DetailAndDocumentation { detail: "-> ()", documentation: "Do the foo" }, 233 DetailAndDocumentation { detail: "fn(&self)", documentation: "Do the foo" },
234 ); 234 );
235 } 235 }
236 236
@@ -255,7 +255,7 @@ fn foo() {
255 bar.fo$0; 255 bar.fo$0;
256} 256}
257"#, 257"#,
258 DetailAndDocumentation { detail: "-> ()", documentation: " Do the foo" }, 258 DetailAndDocumentation { detail: "fn(&self)", documentation: " Do the foo" },
259 ); 259 );
260 } 260 }
261 261
@@ -273,7 +273,7 @@ fn bar() {
273 for c in fo$0 273 for c in fo$0
274} 274}
275"#, 275"#,
276 DetailAndDocumentation { detail: "-> &str", documentation: "Do the foo" }, 276 DetailAndDocumentation { detail: "fn() -> &str", documentation: "Do the foo" },
277 ); 277 );
278 } 278 }
279} 279}
diff --git a/crates/ide_completion/src/patterns.rs b/crates/ide_completion/src/patterns.rs
index f3ce91dd1..cf5ef07b7 100644
--- a/crates/ide_completion/src/patterns.rs
+++ b/crates/ide_completion/src/patterns.rs
@@ -184,11 +184,7 @@ fn test_has_impl_as_prev_sibling() {
184} 184}
185 185
186pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool { 186pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool {
187 let leaf = match element { 187 for node in element.ancestors() {
188 NodeOrToken::Node(node) => node,
189 NodeOrToken::Token(token) => token.parent(),
190 };
191 for node in leaf.ancestors() {
192 if node.kind() == FN || node.kind() == CLOSURE_EXPR { 188 if node.kind() == FN || node.kind() == CLOSURE_EXPR {
193 break; 189 break;
194 } 190 }
@@ -201,7 +197,7 @@ pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool {
201 } 197 }
202 }; 198 };
203 if let Some(body) = loop_body { 199 if let Some(body) = loop_body {
204 if body.syntax().text_range().contains_range(leaf.text_range()) { 200 if body.syntax().text_range().contains_range(element.text_range()) {
205 return true; 201 return true;
206 } 202 }
207 } 203 }
@@ -235,12 +231,8 @@ fn previous_sibling_or_ancestor_sibling(element: SyntaxElement) -> Option<Syntax
235 Some(sibling) 231 Some(sibling)
236 } else { 232 } else {
237 // if not trying to find first ancestor which has such a sibling 233 // if not trying to find first ancestor which has such a sibling
238 let node = match element { 234 let range = element.text_range();
239 NodeOrToken::Node(node) => node, 235 let top_node = element.ancestors().take_while(|it| it.text_range() == range).last()?;
240 NodeOrToken::Token(token) => token.parent(),
241 };
242 let range = node.text_range();
243 let top_node = node.ancestors().take_while(|it| it.text_range() == range).last()?;
244 let prev_sibling_node = top_node.ancestors().find(|it| { 236 let prev_sibling_node = top_node.ancestors().find(|it| {
245 non_trivia_sibling(NodeOrToken::Node(it.to_owned()), Direction::Prev).is_some() 237 non_trivia_sibling(NodeOrToken::Node(it.to_owned()), Direction::Prev).is_some()
246 })?; 238 })?;
diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs
index 2514dda7c..36655667c 100644
--- a/crates/ide_completion/src/render.rs
+++ b/crates/ide_completion/src/render.rs
@@ -10,10 +10,8 @@ pub(crate) mod type_alias;
10 10
11mod builder_ext; 11mod builder_ext;
12 12
13use base_db::Upcast;
14use hir::{ 13use hir::{
15 db::HirDatabase, AsAssocItem, Documentation, HasAttrs, HirDisplay, ModuleDef, Mutability, 14 AsAssocItem, Documentation, HasAttrs, HirDisplay, ModuleDef, Mutability, ScopeDef, Type,
16 ScopeDef, Type,
17}; 15};
18use ide_db::{ 16use ide_db::{
19 helpers::{item_name, SnippetCap}, 17 helpers::{item_name, SnippetCap},
@@ -22,8 +20,8 @@ use ide_db::{
22use syntax::TextRange; 20use syntax::TextRange;
23 21
24use crate::{ 22use crate::{
25 item::{CompletionRelevance, ImportEdit}, 23 item::ImportEdit, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind,
26 CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, 24 CompletionRelevance,
27}; 25};
28 26
29use crate::render::{enum_variant::render_variant, function::render_fn, macro_::render_macro}; 27use crate::render::{enum_variant::render_variant, function::render_fn, macro_::render_macro};
@@ -144,7 +142,15 @@ impl<'a> Render<'a> {
144 .set_documentation(field.docs(self.ctx.db())) 142 .set_documentation(field.docs(self.ctx.db()))
145 .set_deprecated(is_deprecated); 143 .set_deprecated(is_deprecated);
146 144
147 item.set_relevance(compute_relevance(&self.ctx, &ty, &name.to_string())); 145 item.set_relevance(CompletionRelevance {
146 exact_type_match: compute_exact_type_match(self.ctx.completion, ty),
147 exact_name_match: compute_exact_name_match(self.ctx.completion, name.to_string()),
148 ..CompletionRelevance::default()
149 });
150
151 if let Some(ref_match) = compute_ref_match(self.ctx.completion, ty) {
152 item.ref_match(ref_match);
153 }
148 154
149 item.build() 155 item.build()
150 } 156 }
@@ -234,31 +240,18 @@ impl<'a> Render<'a> {
234 if !ty.is_unknown() { 240 if !ty.is_unknown() {
235 item.detail(ty.display(self.ctx.db()).to_string()); 241 item.detail(ty.display(self.ctx.db()).to_string());
236 } 242 }
237 };
238 243
239 if let ScopeDef::Local(local) = resolution { 244 item.set_relevance(CompletionRelevance {
240 let ty = local.ty(self.ctx.db()); 245 exact_type_match: compute_exact_type_match(self.ctx.completion, &ty),
246 exact_name_match: compute_exact_name_match(self.ctx.completion, local_name.clone()),
247 is_local: true,
248 ..CompletionRelevance::default()
249 });
241 250
242 let mut relevance = compute_relevance(&self.ctx, &ty, &local_name); 251 if let Some(ref_match) = compute_ref_match(self.ctx.completion, &ty) {
243 relevance.is_local = true; 252 item.ref_match(ref_match);
244 item.set_relevance(relevance);
245
246 if let Some(expected_type) = self.ctx.completion.expected_type.as_ref() {
247 if &ty != expected_type {
248 if let Some(ty_without_ref) = expected_type.remove_ref() {
249 if relevance_type_match(self.ctx.db().upcast(), &ty, &ty_without_ref) {
250 cov_mark::hit!(suggest_ref);
251 let mutability = if expected_type.is_mutable_reference() {
252 Mutability::Mut
253 } else {
254 Mutability::Shared
255 };
256 item.ref_match(mutability);
257 }
258 }
259 }
260 } 253 }
261 } 254 };
262 255
263 // Add `<>` for generic types 256 // Add `<>` for generic types
264 if self.ctx.completion.is_path_type 257 if self.ctx.completion.is_path_type
@@ -313,17 +306,44 @@ impl<'a> Render<'a> {
313 } 306 }
314} 307}
315 308
316fn compute_relevance(ctx: &RenderContext, ty: &Type, name: &str) -> CompletionRelevance { 309fn compute_exact_type_match(ctx: &CompletionContext, completion_ty: &hir::Type) -> bool {
317 let mut res = CompletionRelevance::default(); 310 if let Some(expected_type) = ctx.expected_type.as_ref() {
311 // We don't ever consider unit type to be an exact type match, since
312 // nearly always this is not meaningful to the user.
313 completion_ty == expected_type && !expected_type.is_unit()
314 } else {
315 false
316 }
317}
318 318
319 res.exact_type_match = Some(ty) == ctx.completion.expected_type.as_ref(); 319fn compute_exact_name_match(ctx: &CompletionContext, completion_name: impl Into<String>) -> bool {
320 res.exact_name_match = Some(name) == ctx.completion.expected_name.as_deref(); 320 let completion_name = completion_name.into();
321 321
322 res 322 Some(&completion_name) == ctx.expected_name.as_ref()
323} 323}
324 324
325fn relevance_type_match(db: &dyn HirDatabase, ty: &Type, expected_type: &Type) -> bool { 325fn compute_ref_match(ctx: &CompletionContext, completion_ty: &hir::Type) -> Option<Mutability> {
326 ty == expected_type || ty.autoderef(db).any(|deref_ty| &deref_ty == expected_type) 326 let mut ref_match = None;
327 if let Some(expected_type) = &ctx.expected_type {
328 if completion_ty != expected_type {
329 if let Some(expected_type_without_ref) = expected_type.remove_ref() {
330 if completion_ty == &expected_type_without_ref
331 || completion_ty
332 .autoderef(ctx.db)
333 .any(|deref_ty| deref_ty == expected_type_without_ref)
334 {
335 cov_mark::hit!(suggest_ref);
336 let mutability = if expected_type.is_mutable_reference() {
337 Mutability::Mut
338 } else {
339 Mutability::Shared
340 };
341 ref_match = Some(mutability);
342 }
343 }
344 }
345 };
346 ref_match
327} 347}
328 348
329#[cfg(test)] 349#[cfg(test)]
@@ -432,6 +452,44 @@ fn main() { Foo::Fo$0 }
432 } 452 }
433 453
434 #[test] 454 #[test]
455 fn fn_detail_includes_args_and_return_type() {
456 check(
457 r#"
458fn foo<T>(a: u32, b: u32, t: T) -> (u32, T) { (a, t) }
459
460fn main() { fo$0 }
461"#,
462 expect![[r#"
463 [
464 CompletionItem {
465 label: "foo(…)",
466 source_range: 68..70,
467 delete: 68..70,
468 insert: "foo(${1:a}, ${2:b}, ${3:t})$0",
469 kind: SymbolKind(
470 Function,
471 ),
472 lookup: "foo",
473 detail: "fn(u32, u32, T) -> (u32, T)",
474 trigger_call_info: true,
475 },
476 CompletionItem {
477 label: "main()",
478 source_range: 68..70,
479 delete: 68..70,
480 insert: "main()$0",
481 kind: SymbolKind(
482 Function,
483 ),
484 lookup: "main",
485 detail: "fn()",
486 },
487 ]
488 "#]],
489 );
490 }
491
492 #[test]
435 fn enum_detail_just_parentheses_for_unit() { 493 fn enum_detail_just_parentheses_for_unit() {
436 check( 494 check(
437 r#" 495 r#"
@@ -477,6 +535,11 @@ fn main() { let _: m::Spam = S$0 }
477 ), 535 ),
478 lookup: "Spam::Bar", 536 lookup: "Spam::Bar",
479 detail: "(i32)", 537 detail: "(i32)",
538 relevance: CompletionRelevance {
539 exact_name_match: false,
540 exact_type_match: true,
541 is_local: false,
542 },
480 trigger_call_info: true, 543 trigger_call_info: true,
481 }, 544 },
482 CompletionItem { 545 CompletionItem {
@@ -498,6 +561,11 @@ fn main() { let _: m::Spam = S$0 }
498 ), 561 ),
499 lookup: "Spam::Foo", 562 lookup: "Spam::Foo",
500 detail: "()", 563 detail: "()",
564 relevance: CompletionRelevance {
565 exact_name_match: false,
566 exact_type_match: true,
567 is_local: false,
568 },
501 }, 569 },
502 CompletionItem { 570 CompletionItem {
503 label: "main()", 571 label: "main()",
@@ -508,7 +576,7 @@ fn main() { let _: m::Spam = S$0 }
508 Function, 576 Function,
509 ), 577 ),
510 lookup: "main", 578 lookup: "main",
511 detail: "-> ()", 579 detail: "fn()",
512 }, 580 },
513 ] 581 ]
514 "#]], 582 "#]],
@@ -537,7 +605,7 @@ fn main() { som$0 }
537 Function, 605 Function,
538 ), 606 ),
539 lookup: "main", 607 lookup: "main",
540 detail: "-> ()", 608 detail: "fn()",
541 }, 609 },
542 CompletionItem { 610 CompletionItem {
543 label: "something_deprecated()", 611 label: "something_deprecated()",
@@ -548,7 +616,7 @@ fn main() { som$0 }
548 Function, 616 Function,
549 ), 617 ),
550 lookup: "something_deprecated", 618 lookup: "something_deprecated",
551 detail: "-> ()", 619 detail: "fn()",
552 deprecated: true, 620 deprecated: true,
553 }, 621 },
554 CompletionItem { 622 CompletionItem {
@@ -560,7 +628,7 @@ fn main() { som$0 }
560 Function, 628 Function,
561 ), 629 ),
562 lookup: "something_else_deprecated", 630 lookup: "something_else_deprecated",
563 detail: "-> ()", 631 detail: "fn()",
564 deprecated: true, 632 deprecated: true,
565 }, 633 },
566 ] 634 ]
@@ -611,7 +679,7 @@ impl S {
611 insert: "bar()$0", 679 insert: "bar()$0",
612 kind: Method, 680 kind: Method,
613 lookup: "bar", 681 lookup: "bar",
614 detail: "-> ()", 682 detail: "fn(self)",
615 documentation: Documentation( 683 documentation: Documentation(
616 "Method docs", 684 "Method docs",
617 ), 685 ),
@@ -711,7 +779,7 @@ fn foo(s: S) { s.$0 }
711 insert: "the_method()$0", 779 insert: "the_method()$0",
712 kind: Method, 780 kind: Method,
713 lookup: "the_method", 781 lookup: "the_method",
714 detail: "-> ()", 782 detail: "fn(&self)",
715 }, 783 },
716 ] 784 ]
717 "#]], 785 "#]],
@@ -1019,7 +1087,7 @@ fn main() {
1019 Function, 1087 Function,
1020 ), 1088 ),
1021 lookup: "foo", 1089 lookup: "foo",
1022 detail: "-> ()", 1090 detail: "fn(&mut S)",
1023 trigger_call_info: true, 1091 trigger_call_info: true,
1024 }, 1092 },
1025 CompletionItem { 1093 CompletionItem {
@@ -1031,7 +1099,7 @@ fn main() {
1031 Function, 1099 Function,
1032 ), 1100 ),
1033 lookup: "main", 1101 lookup: "main",
1034 detail: "-> ()", 1102 detail: "fn()",
1035 }, 1103 },
1036 CompletionItem { 1104 CompletionItem {
1037 label: "s", 1105 label: "s",
@@ -1169,4 +1237,86 @@ fn foo(bar: u32) {
1169 "#]], 1237 "#]],
1170 ); 1238 );
1171 } 1239 }
1240
1241 #[test]
1242 fn enum_owned() {
1243 check_relevance(
1244 r#"
1245enum Foo { A, B }
1246fn foo() {
1247 bar($0);
1248}
1249fn bar(t: Foo) {}
1250"#,
1251 expect![[r#"
1252 ev Foo::A [type]
1253 ev Foo::B [type]
1254 en Foo []
1255 fn bar(…) []
1256 fn foo() []
1257 "#]],
1258 );
1259 }
1260
1261 #[test]
1262 fn enum_ref() {
1263 check_relevance(
1264 r#"
1265enum Foo { A, B }
1266fn foo() {
1267 bar($0);
1268}
1269fn bar(t: &Foo) {}
1270"#,
1271 expect![[r#"
1272 ev Foo::A []
1273 ev &Foo::A [type]
1274 ev Foo::B []
1275 ev &Foo::B [type]
1276 en Foo []
1277 fn bar(…) []
1278 fn foo() []
1279 "#]],
1280 );
1281 }
1282
1283 #[test]
1284 fn suggest_deref_fn_ret() {
1285 check_relevance(
1286 r#"
1287#[lang = "deref"]
1288trait Deref {
1289 type Target;
1290 fn deref(&self) -> &Self::Target;
1291}
1292
1293struct S;
1294struct T(S);
1295
1296impl Deref for T {
1297 type Target = S;
1298
1299 fn deref(&self) -> &Self::Target {
1300 &self.0
1301 }
1302}
1303
1304fn foo(s: &S) {}
1305fn bar() -> T {}
1306
1307fn main() {
1308 foo($0);
1309}
1310 "#,
1311 expect![[r#"
1312 tt Deref []
1313 fn bar() []
1314 fn &bar() [type]
1315 fn foo(…) []
1316 st T []
1317 st S []
1318 fn main() []
1319 "#]],
1320 )
1321 }
1172} 1322}
diff --git a/crates/ide_completion/src/render/enum_variant.rs b/crates/ide_completion/src/render/enum_variant.rs
index e8cfcc0c7..374247b05 100644
--- a/crates/ide_completion/src/render/enum_variant.rs
+++ b/crates/ide_completion/src/render/enum_variant.rs
@@ -6,7 +6,8 @@ use itertools::Itertools;
6 6
7use crate::{ 7use crate::{
8 item::{CompletionItem, CompletionKind, ImportEdit}, 8 item::{CompletionItem, CompletionKind, ImportEdit},
9 render::{builder_ext::Params, RenderContext}, 9 render::{builder_ext::Params, compute_exact_type_match, compute_ref_match, RenderContext},
10 CompletionRelevance,
10}; 11};
11 12
12pub(crate) fn render_variant<'a>( 13pub(crate) fn render_variant<'a>(
@@ -74,6 +75,16 @@ impl<'a> EnumRender<'a> {
74 item.lookup_by(self.short_qualified_name); 75 item.lookup_by(self.short_qualified_name);
75 } 76 }
76 77
78 let ty = self.variant.parent_enum(self.ctx.completion.db).ty(self.ctx.completion.db);
79 item.set_relevance(CompletionRelevance {
80 exact_type_match: compute_exact_type_match(self.ctx.completion, &ty),
81 ..CompletionRelevance::default()
82 });
83
84 if let Some(ref_match) = compute_ref_match(self.ctx.completion, &ty) {
85 item.ref_match(ref_match);
86 }
87
77 item.build() 88 item.build()
78 } 89 }
79 90
diff --git a/crates/ide_completion/src/render/function.rs b/crates/ide_completion/src/render/function.rs
index 47e26a5d8..010303182 100644
--- a/crates/ide_completion/src/render/function.rs
+++ b/crates/ide_completion/src/render/function.rs
@@ -2,11 +2,15 @@
2 2
3use hir::{HasSource, HirDisplay, Type}; 3use hir::{HasSource, HirDisplay, Type};
4use ide_db::SymbolKind; 4use ide_db::SymbolKind;
5use itertools::Itertools;
5use syntax::ast::Fn; 6use syntax::ast::Fn;
6 7
7use crate::{ 8use crate::{
8 item::{CompletionItem, CompletionItemKind, CompletionKind, CompletionRelevance, ImportEdit}, 9 item::{CompletionItem, CompletionItemKind, CompletionKind, CompletionRelevance, ImportEdit},
9 render::{builder_ext::Params, RenderContext}, 10 render::{
11 builder_ext::Params, compute_exact_name_match, compute_exact_type_match, compute_ref_match,
12 RenderContext,
13 },
10}; 14};
11 15
12pub(crate) fn render_fn<'a>( 16pub(crate) fn render_fn<'a>(
@@ -52,30 +56,60 @@ impl<'a> FunctionRender<'a> {
52 self.ctx.is_deprecated(self.func) || self.ctx.is_deprecated_assoc_item(self.func), 56 self.ctx.is_deprecated(self.func) || self.ctx.is_deprecated_assoc_item(self.func),
53 ) 57 )
54 .detail(self.detail()) 58 .detail(self.detail())
55 .add_call_parens(self.ctx.completion, self.name, params) 59 .add_call_parens(self.ctx.completion, self.name.clone(), params)
56 .add_import(import_to_add); 60 .add_import(import_to_add);
57 61
58 let mut relevance = CompletionRelevance::default(); 62 let ret_type = self.func.ret_type(self.ctx.db());
59 if let Some(expected_type) = &self.ctx.completion.expected_type { 63 item.set_relevance(CompletionRelevance {
60 let ret_ty = self.func.ret_type(self.ctx.db()); 64 exact_type_match: compute_exact_type_match(self.ctx.completion, &ret_type),
65 exact_name_match: compute_exact_name_match(self.ctx.completion, self.name.clone()),
66 ..CompletionRelevance::default()
67 });
61 68
62 // We don't ever consider a function which returns unit type to be an 69 if let Some(ref_match) = compute_ref_match(self.ctx.completion, &ret_type) {
63 // exact type match, since nearly always this is not meaningful to the 70 item.ref_match(ref_match);
64 // user.
65 relevance.exact_type_match = &ret_ty == expected_type && !ret_ty.is_unit();
66 } 71 }
67 if let Some(expected_name) = &self.ctx.completion.expected_name {
68 relevance.exact_name_match =
69 expected_name == &self.func.name(self.ctx.db()).to_string();
70 }
71 item.set_relevance(relevance);
72 72
73 item.build() 73 item.build()
74 } 74 }
75 75
76 fn detail(&self) -> String { 76 fn detail(&self) -> String {
77 let ty = self.func.ret_type(self.ctx.db()); 77 let ret_ty = self.func.ret_type(self.ctx.db());
78 format!("-> {}", ty.display(self.ctx.db())) 78 let ret = if ret_ty.is_unit() {
79 // Omit the return type if it is the unit type
80 String::new()
81 } else {
82 format!(" {}", self.ty_display())
83 };
84
85 format!("fn({}){}", self.params_display(), ret)
86 }
87
88 fn params_display(&self) -> String {
89 if let Some(self_param) = self.func.self_param(self.ctx.db()) {
90 let params = self
91 .func
92 .assoc_fn_params(self.ctx.db())
93 .into_iter()
94 .skip(1) // skip the self param because we are manually handling that
95 .map(|p| p.ty().display(self.ctx.db()).to_string());
96
97 std::iter::once(self_param.display(self.ctx.db()).to_owned()).chain(params).join(", ")
98 } else {
99 let params = self
100 .func
101 .assoc_fn_params(self.ctx.db())
102 .into_iter()
103 .map(|p| p.ty().display(self.ctx.db()).to_string())
104 .join(", ");
105 params
106 }
107 }
108
109 fn ty_display(&self) -> String {
110 let ret_ty = self.func.ret_type(self.ctx.db());
111
112 format!("-> {}", ret_ty.display(self.ctx.db()))
79 } 113 }
80 114
81 fn add_arg(&self, arg: &str, ty: &Type) -> String { 115 fn add_arg(&self, arg: &str, ty: &Type) -> String {