aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/completion
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/completion')
-rw-r--r--crates/ra_ide/src/completion/complete_dot.rs32
-rw-r--r--crates/ra_ide/src/completion/complete_path.rs70
-rw-r--r--crates/ra_ide/src/completion/complete_scope.rs36
-rw-r--r--crates/ra_ide/src/completion/completion_item.rs3
-rw-r--r--crates/ra_ide/src/completion/presentation.rs22
5 files changed, 158 insertions, 5 deletions
diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs
index 81e5037aa..f07611d88 100644
--- a/crates/ra_ide/src/completion/complete_dot.rs
+++ b/crates/ra_ide/src/completion/complete_dot.rs
@@ -402,6 +402,38 @@ mod tests {
402 } 402 }
403 403
404 #[test] 404 #[test]
405 fn completes_trait_method_from_other_module() {
406 assert_debug_snapshot!(
407 do_ref_completion(
408 r"
409 struct A {}
410 mod m {
411 pub trait Trait { fn the_method(&self); }
412 }
413 use m::Trait;
414 impl Trait for A {}
415 fn foo(a: A) {
416 a.<|>
417 }
418 ",
419 ),
420 @r###"
421 [
422 CompletionItem {
423 label: "the_method()",
424 source_range: [219; 219),
425 delete: [219; 219),
426 insert: "the_method()$0",
427 kind: Method,
428 lookup: "the_method",
429 detail: "fn the_method(&self)",
430 },
431 ]
432 "###
433 );
434 }
435
436 #[test]
405 fn test_no_non_self_method() { 437 fn test_no_non_self_method() {
406 assert_debug_snapshot!( 438 assert_debug_snapshot!(
407 do_ref_completion( 439 do_ref_completion(
diff --git a/crates/ra_ide/src/completion/complete_path.rs b/crates/ra_ide/src/completion/complete_path.rs
index d588ee364..3db17f15f 100644
--- a/crates/ra_ide/src/completion/complete_path.rs
+++ b/crates/ra_ide/src/completion/complete_path.rs
@@ -967,4 +967,74 @@ mod tests {
967 ] 967 ]
968 "###); 968 "###);
969 } 969 }
970
971 #[test]
972 fn function_mod_share_name() {
973 assert_debug_snapshot!(
974 do_reference_completion(
975 r"
976 fn foo() {
977 self::m::<|>
978 }
979
980 mod m {
981 pub mod z {}
982 pub fn z() {}
983 }
984 ",
985 ),
986 @r###"
987 [
988 CompletionItem {
989 label: "z",
990 source_range: [57; 57),
991 delete: [57; 57),
992 insert: "z",
993 kind: Module,
994 },
995 CompletionItem {
996 label: "z()",
997 source_range: [57; 57),
998 delete: [57; 57),
999 insert: "z()$0",
1000 kind: Function,
1001 lookup: "z",
1002 detail: "pub fn z()",
1003 },
1004 ]
1005 "###
1006 );
1007 }
1008
1009 #[test]
1010 fn completes_hashmap_new() {
1011 assert_debug_snapshot!(
1012 do_reference_completion(
1013 r"
1014 struct RandomState;
1015 struct HashMap<K, V, S = RandomState> {}
1016
1017 impl<K, V> HashMap<K, V, RandomState> {
1018 pub fn new() -> HashMap<K, V, RandomState> { }
1019 }
1020 fn foo() {
1021 HashMap::<|>
1022 }
1023 "
1024 ),
1025 @r###"
1026 [
1027 CompletionItem {
1028 label: "new()",
1029 source_range: [292; 292),
1030 delete: [292; 292),
1031 insert: "new()$0",
1032 kind: Function,
1033 lookup: "new",
1034 detail: "pub fn new() -> HashMap<K, V, RandomState>",
1035 },
1036 ]
1037 "###
1038 );
1039 }
970} 1040}
diff --git a/crates/ra_ide/src/completion/complete_scope.rs b/crates/ra_ide/src/completion/complete_scope.rs
index bd4adf23a..5ffff5a1c 100644
--- a/crates/ra_ide/src/completion/complete_scope.rs
+++ b/crates/ra_ide/src/completion/complete_scope.rs
@@ -42,6 +42,7 @@ mod tests {
42 kind: Function, 42 kind: Function,
43 lookup: "quux", 43 lookup: "quux",
44 detail: "fn quux(x: i32)", 44 detail: "fn quux(x: i32)",
45 trigger_call_info: true,
45 }, 46 },
46 CompletionItem { 47 CompletionItem {
47 label: "x", 48 label: "x",
@@ -844,6 +845,7 @@ mod tests {
844 kind: Function, 845 kind: Function,
845 lookup: "quux", 846 lookup: "quux",
846 detail: "fn quux(x: i32)", 847 detail: "fn quux(x: i32)",
848 trigger_call_info: true,
847 }, 849 },
848 CompletionItem { 850 CompletionItem {
849 label: "x", 851 label: "x",
@@ -865,4 +867,38 @@ mod tests {
865 "### 867 "###
866 ); 868 );
867 } 869 }
870
871 #[test]
872 fn completes_unresolved_uses() {
873 assert_debug_snapshot!(
874 do_reference_completion(
875 r"
876 use spam::Quux;
877
878 fn main() {
879 <|>
880 }
881 "
882 ),
883 @r###"
884 [
885 CompletionItem {
886 label: "Quux",
887 source_range: [82; 82),
888 delete: [82; 82),
889 insert: "Quux",
890 },
891 CompletionItem {
892 label: "main()",
893 source_range: [82; 82),
894 delete: [82; 82),
895 insert: "main()$0",
896 kind: Function,
897 lookup: "main",
898 detail: "fn main()",
899 },
900 ]
901 "###
902 );
903 }
868} 904}
diff --git a/crates/ra_ide/src/completion/completion_item.rs b/crates/ra_ide/src/completion/completion_item.rs
index ef0eb43b2..bc0f1aff5 100644
--- a/crates/ra_ide/src/completion/completion_item.rs
+++ b/crates/ra_ide/src/completion/completion_item.rs
@@ -80,6 +80,9 @@ impl fmt::Debug for CompletionItem {
80 if self.deprecated { 80 if self.deprecated {
81 s.field("deprecated", &true); 81 s.field("deprecated", &true);
82 } 82 }
83 if self.trigger_call_info {
84 s.field("trigger_call_info", &true);
85 }
83 s.finish() 86 s.finish()
84 } 87 }
85} 88}
diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs
index 5213def20..253848602 100644
--- a/crates/ra_ide/src/completion/presentation.rs
+++ b/crates/ra_ide/src/completion/presentation.rs
@@ -1,6 +1,6 @@
1//! This modules takes care of rendering various definitions as completion items. 1//! This modules takes care of rendering various definitions as completion items.
2 2
3use hir::{db::HirDatabase, Docs, HasAttrs, HasSource, HirDisplay, ScopeDef, StructKind, Type}; 3use hir::{Docs, HasAttrs, HasSource, HirDisplay, ScopeDef, StructKind, Type};
4use join_to_string::join; 4use join_to_string::join;
5use ra_syntax::ast::NameOwner; 5use ra_syntax::ast::NameOwner;
6use test_utils::tested_by; 6use test_utils::tested_by;
@@ -9,7 +9,10 @@ use crate::completion::{
9 CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, 9 CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
10}; 10};
11 11
12use crate::display::{const_label, macro_label, type_label, FunctionSignature}; 12use crate::{
13 display::{const_label, macro_label, type_label, FunctionSignature},
14 RootDatabase,
15};
13 16
14impl Completions { 17impl Completions {
15 pub(crate) fn add_field( 18 pub(crate) fn add_field(
@@ -273,8 +276,10 @@ impl Completions {
273 pub(crate) fn add_enum_variant(&mut self, ctx: &CompletionContext, variant: hir::EnumVariant) { 276 pub(crate) fn add_enum_variant(&mut self, ctx: &CompletionContext, variant: hir::EnumVariant) {
274 let is_deprecated = is_deprecated(variant, ctx.db); 277 let is_deprecated = is_deprecated(variant, ctx.db);
275 let name = variant.name(ctx.db); 278 let name = variant.name(ctx.db);
276 let detail_types = 279 let detail_types = variant
277 variant.fields(ctx.db).into_iter().map(|field| (field.name(ctx.db), field.ty(ctx.db))); 280 .fields(ctx.db)
281 .into_iter()
282 .map(|field| (field.name(ctx.db), field.signature_ty(ctx.db)));
278 let detail = match variant.kind(ctx.db) { 283 let detail = match variant.kind(ctx.db) {
279 StructKind::Tuple | StructKind::Unit => { 284 StructKind::Tuple | StructKind::Unit => {
280 join(detail_types.map(|(_, t)| t.display(ctx.db).to_string())) 285 join(detail_types.map(|(_, t)| t.display(ctx.db).to_string()))
@@ -298,7 +303,7 @@ impl Completions {
298 } 303 }
299} 304}
300 305
301fn is_deprecated(node: impl HasAttrs, db: &impl HirDatabase) -> bool { 306fn is_deprecated(node: impl HasAttrs, db: &RootDatabase) -> bool {
302 node.attrs(db).by_key("deprecated").exists() 307 node.attrs(db).by_key("deprecated").exists()
303} 308}
304 309
@@ -510,6 +515,7 @@ mod tests {
510 kind: Function, 515 kind: Function,
511 lookup: "with_args", 516 lookup: "with_args",
512 detail: "fn with_args(x: i32, y: String)", 517 detail: "fn with_args(x: i32, y: String)",
518 trigger_call_info: true,
513 }, 519 },
514 ] 520 ]
515 "### 521 "###
@@ -566,6 +572,7 @@ mod tests {
566 kind: Method, 572 kind: Method,
567 lookup: "foo", 573 lookup: "foo",
568 detail: "fn foo(&self, x: i32)", 574 detail: "fn foo(&self, x: i32)",
575 trigger_call_info: true,
569 }, 576 },
570 ] 577 ]
571 "### 578 "###
@@ -600,6 +607,7 @@ mod tests {
600 kind: Method, 607 kind: Method,
601 lookup: "foo", 608 lookup: "foo",
602 detail: "fn foo(&self, x: i32)", 609 detail: "fn foo(&self, x: i32)",
610 trigger_call_info: true,
603 }, 611 },
604 ] 612 ]
605 "### 613 "###
@@ -718,6 +726,7 @@ mod tests {
718 kind: Function, 726 kind: Function,
719 lookup: "foo", 727 lookup: "foo",
720 detail: "fn foo(xs: Ve)", 728 detail: "fn foo(xs: Ve)",
729 trigger_call_info: true,
721 }, 730 },
722 ] 731 ]
723 "### 732 "###
@@ -747,6 +756,7 @@ mod tests {
747 kind: Function, 756 kind: Function,
748 lookup: "foo", 757 lookup: "foo",
749 detail: "fn foo(xs: Ve)", 758 detail: "fn foo(xs: Ve)",
759 trigger_call_info: true,
750 }, 760 },
751 ] 761 ]
752 "### 762 "###
@@ -775,6 +785,7 @@ mod tests {
775 kind: Function, 785 kind: Function,
776 lookup: "foo", 786 lookup: "foo",
777 detail: "fn foo(xs: Ve)", 787 detail: "fn foo(xs: Ve)",
788 trigger_call_info: true,
778 }, 789 },
779 ] 790 ]
780 "### 791 "###
@@ -803,6 +814,7 @@ mod tests {
803 kind: Function, 814 kind: Function,
804 lookup: "foo", 815 lookup: "foo",
805 detail: "fn foo(xs: Ve<i128>)", 816 detail: "fn foo(xs: Ve<i128>)",
817 trigger_call_info: true,
806 }, 818 },
807 ] 819 ]
808 "### 820 "###