diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-03-15 15:53:15 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-03-15 15:53:15 +0000 |
commit | eec64ec01b0553aca855df8146965ed6c6746e7d (patch) | |
tree | 40e81ae73928c85bbd6ab452fd317383cf5322d8 /crates/ide_completion | |
parent | b8a85e960398ebab4624574f9c2ed9c655612d56 (diff) | |
parent | db8bcf132c901cc9b9f993b17b1e648f55c35e78 (diff) |
Merge #7992
7992: improved completion sorting for functions and methods r=JoshMcguigan a=JoshMcguigan
This PR improves completion sorting for functions and methods. Related to #7935.
### Before
The methods are being sorted by `coc` by closeness in file.
![pre-fn-relevance](https://user-images.githubusercontent.com/22216761/111018669-fe3d3f00-836e-11eb-9607-cc05af080a6a.png)
### After
Notice `bbb()` on top (type + name match), followed by `ddd()` (type match).
![image](https://user-images.githubusercontent.com/22216761/111018680-0e551e80-836f-11eb-94b1-c88336ecbc6e.png)
Co-authored-by: Josh Mcguigan <[email protected]>
Diffstat (limited to 'crates/ide_completion')
-rw-r--r-- | crates/ide_completion/src/render.rs | 60 | ||||
-rw-r--r-- | crates/ide_completion/src/render/function.rs | 17 |
2 files changed, 76 insertions, 1 deletions
diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs index 670563e50..2514dda7c 100644 --- a/crates/ide_completion/src/render.rs +++ b/crates/ide_completion/src/render.rs | |||
@@ -928,6 +928,66 @@ fn f(foo: &Foo) { f(foo, w$0) } | |||
928 | } | 928 | } |
929 | 929 | ||
930 | #[test] | 930 | #[test] |
931 | fn score_fn_type_and_name_match() { | ||
932 | check_relevance( | ||
933 | r#" | ||
934 | struct A { bar: u8 } | ||
935 | fn baz() -> u8 { 0 } | ||
936 | fn bar() -> u8 { 0 } | ||
937 | fn f() { A { bar: b$0 }; } | ||
938 | "#, | ||
939 | expect![[r#" | ||
940 | fn baz() [type] | ||
941 | st A [] | ||
942 | fn bar() [type+name] | ||
943 | fn f() [] | ||
944 | "#]], | ||
945 | ); | ||
946 | } | ||
947 | |||
948 | #[test] | ||
949 | fn score_method_type_and_name_match() { | ||
950 | check_relevance( | ||
951 | r#" | ||
952 | fn baz(aaa: u32){} | ||
953 | struct Foo; | ||
954 | impl Foo { | ||
955 | fn aaa(&self) -> u32 { 0 } | ||
956 | fn bbb(&self) -> u32 { 0 } | ||
957 | fn ccc(&self) -> u64 { 0 } | ||
958 | } | ||
959 | fn f() { | ||
960 | baz(Foo.$0 | ||
961 | } | ||
962 | "#, | ||
963 | expect![[r#" | ||
964 | me aaa() [type+name] | ||
965 | me bbb() [type] | ||
966 | me ccc() [] | ||
967 | "#]], | ||
968 | ); | ||
969 | } | ||
970 | |||
971 | #[test] | ||
972 | fn score_method_name_match_only() { | ||
973 | check_relevance( | ||
974 | r#" | ||
975 | fn baz(aaa: u32){} | ||
976 | struct Foo; | ||
977 | impl Foo { | ||
978 | fn aaa(&self) -> u64 { 0 } | ||
979 | } | ||
980 | fn f() { | ||
981 | baz(Foo.$0 | ||
982 | } | ||
983 | "#, | ||
984 | expect![[r#" | ||
985 | me aaa() [name] | ||
986 | "#]], | ||
987 | ); | ||
988 | } | ||
989 | |||
990 | #[test] | ||
931 | fn suggest_ref_mut() { | 991 | fn suggest_ref_mut() { |
932 | cov_mark::check!(suggest_ref); | 992 | cov_mark::check!(suggest_ref); |
933 | check( | 993 | check( |
diff --git a/crates/ide_completion/src/render/function.rs b/crates/ide_completion/src/render/function.rs index f4dabe3d1..47e26a5d8 100644 --- a/crates/ide_completion/src/render/function.rs +++ b/crates/ide_completion/src/render/function.rs | |||
@@ -5,7 +5,7 @@ use ide_db::SymbolKind; | |||
5 | use syntax::ast::Fn; | 5 | use syntax::ast::Fn; |
6 | 6 | ||
7 | use crate::{ | 7 | use crate::{ |
8 | item::{CompletionItem, CompletionItemKind, CompletionKind, ImportEdit}, | 8 | item::{CompletionItem, CompletionItemKind, CompletionKind, CompletionRelevance, ImportEdit}, |
9 | render::{builder_ext::Params, RenderContext}, | 9 | render::{builder_ext::Params, RenderContext}, |
10 | }; | 10 | }; |
11 | 11 | ||
@@ -55,6 +55,21 @@ impl<'a> FunctionRender<'a> { | |||
55 | .add_call_parens(self.ctx.completion, self.name, params) | 55 | .add_call_parens(self.ctx.completion, self.name, params) |
56 | .add_import(import_to_add); | 56 | .add_import(import_to_add); |
57 | 57 | ||
58 | let mut relevance = CompletionRelevance::default(); | ||
59 | if let Some(expected_type) = &self.ctx.completion.expected_type { | ||
60 | let ret_ty = self.func.ret_type(self.ctx.db()); | ||
61 | |||
62 | // We don't ever consider a function which returns unit type to be an | ||
63 | // exact type match, since nearly always this is not meaningful to the | ||
64 | // user. | ||
65 | relevance.exact_type_match = &ret_ty == expected_type && !ret_ty.is_unit(); | ||
66 | } | ||
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 | |||
58 | item.build() | 73 | item.build() |
59 | } | 74 | } |
60 | 75 | ||