diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-02-24 18:56:49 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-02-24 18:56:49 +0000 |
commit | 5a684099e9aa3482b408002030fafe1dcd0fa9a9 (patch) | |
tree | 20e86fbc586b350c336277d2b0886a60f9844de8 /crates/ra_ide_api/src/completion/complete_path.rs | |
parent | 67528c4b3943a2027839a25770d079132a9ea130 (diff) | |
parent | d5f6a5f5e29df3005533502b49f78daae314ac5b (diff) |
Merge #893
893: Completion presentation r=matklad a=matklad
Just moves completion code around a bit, to keep logic for producing completions and logic for rendering them into completion items separate.
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_path.rs')
-rw-r--r-- | crates/ra_ide_api/src/completion/complete_path.rs | 93 |
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 @@ | |||
1 | use join_to_string::join; | 1 | use hir::Resolution; |
2 | use hir::{Docs, Resolution}; | 2 | use ra_syntax::AstNode; |
3 | use ra_syntax::{AstNode, ast::NameOwner}; | ||
4 | use test_utils::tested_by; | 3 | use test_utils::tested_by; |
5 | 4 | ||
6 | use crate::completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext}; | 5 | use crate::completion::{Completions, CompletionContext}; |
7 | 6 | ||
8 | pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { | 7 | pub(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)] |
111 | mod tests { | 59 | mod 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 | } |