aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion/complete_path.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_path.rs')
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs93
1 files changed, 19 insertions, 74 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs
index d337fe970..629a7ee77 100644
--- a/crates/ra_ide_api/src/completion/complete_path.rs
+++ b/crates/ra_ide_api/src/completion/complete_path.rs
@@ -1,9 +1,8 @@
1use join_to_string::join; 1use hir::Resolution;
2use hir::{Docs, Resolution}; 2use ra_syntax::AstNode;
3use ra_syntax::{AstNode, ast::NameOwner};
4use test_utils::tested_by; 3use test_utils::tested_by;
5 4
6use crate::completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext}; 5use crate::completion::{Completions, CompletionContext};
7 6
8pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { 7pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
9 let path = match &ctx.path_prefix { 8 let path = match &ctx.path_prefix {
@@ -28,79 +27,28 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
28 } 27 }
29 } 28 }
30 } 29 }
31 30 acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def));
32 CompletionItem::new(
33 CompletionKind::Reference,
34 ctx.source_range(),
35 name.to_string(),
36 )
37 .from_resolution(ctx, &res.def.map(hir::Resolution::Def))
38 .add_to(acc);
39 } 31 }
40 } 32 }
41 hir::ModuleDef::Enum(e) => { 33 hir::ModuleDef::Enum(e) => {
42 e.variants(ctx.db).into_iter().for_each(|variant| { 34 for variant in e.variants(ctx.db) {
43 if let Some(name) = variant.name(ctx.db) { 35 acc.add_enum_variant(ctx, variant);
44 let detail_types = 36 }
45 variant.fields(ctx.db).into_iter().map(|field| field.ty(ctx.db));
46 let detail =
47 join(detail_types).separator(", ").surround_with("(", ")").to_string();
48
49 CompletionItem::new(
50 CompletionKind::Reference,
51 ctx.source_range(),
52 name.to_string(),
53 )
54 .kind(CompletionItemKind::EnumVariant)
55 .set_documentation(variant.docs(ctx.db))
56 .set_detail(Some(detail))
57 .add_to(acc)
58 }
59 });
60 } 37 }
61 hir::ModuleDef::Struct(s) => { 38 hir::ModuleDef::Struct(s) => {
62 let ty = s.ty(ctx.db); 39 let ty = s.ty(ctx.db);
63 ty.iterate_impl_items(ctx.db, |item| match item { 40 ty.iterate_impl_items(ctx.db, |item| {
64 hir::ImplItem::Method(func) => { 41 match item {
65 let sig = func.signature(ctx.db); 42 hir::ImplItem::Method(func) => {
66 if !sig.has_self_param() { 43 let sig = func.signature(ctx.db);
67 CompletionItem::new( 44 if !sig.has_self_param() {
68 CompletionKind::Reference, 45 acc.add_function(ctx, func);
69 ctx.source_range(), 46 }
70 sig.name().to_string(),
71 )
72 .from_function(ctx, func)
73 .kind(CompletionItemKind::Method)
74 .add_to(acc);
75 }
76 None::<()>
77 }
78 hir::ImplItem::Const(ct) => {
79 let source = ct.source(ctx.db);
80 if let Some(name) = source.1.name() {
81 CompletionItem::new(
82 CompletionKind::Reference,
83 ctx.source_range(),
84 name.text().to_string(),
85 )
86 .from_const(ctx, ct)
87 .add_to(acc);
88 }
89 None::<()>
90 }
91 hir::ImplItem::Type(ty) => {
92 let source = ty.source(ctx.db);
93 if let Some(name) = source.1.name() {
94 CompletionItem::new(
95 CompletionKind::Reference,
96 ctx.source_range(),
97 name.text().to_string(),
98 )
99 .from_type(ctx, ty)
100 .add_to(acc);
101 } 47 }
102 None::<()> 48 hir::ImplItem::Const(ct) => acc.add_const(ctx, ct),
49 hir::ImplItem::Type(ty) => acc.add_type(ctx, ty),
103 } 50 }
51 None::<()>
104 }); 52 });
105 } 53 }
106 _ => return, 54 _ => return,
@@ -109,13 +57,10 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
109 57
110#[cfg(test)] 58#[cfg(test)]
111mod tests { 59mod tests {
112 use crate::completion::{
113 CompletionKind,
114 completion_item::{check_completion, do_completion},
115};
116
117 use test_utils::covers; 60 use test_utils::covers;
118 61
62 use crate::completion::{CompletionKind, check_completion, do_completion};
63
119 fn check_reference_completion(code: &str, expected_completions: &str) { 64 fn check_reference_completion(code: &str, expected_completions: &str) {
120 check_completion(code, expected_completions, CompletionKind::Reference); 65 check_completion(code, expected_completions, CompletionKind::Reference);
121 } 66 }