diff options
author | Aleksey Kladov <[email protected]> | 2019-02-24 18:34:38 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-02-24 18:34:38 +0000 |
commit | b7a78729100aa2068827e5812aec03abd3a5df9a (patch) | |
tree | e2607e8a806a1c54a8e9f54a0def129fa9ecd5d0 /crates/ra_ide_api/src/completion/presentation.rs | |
parent | a650a93bf5d0217e6f366ffc91ca8b25bfd37be4 (diff) |
move more code to presentation
Diffstat (limited to 'crates/ra_ide_api/src/completion/presentation.rs')
-rw-r--r-- | crates/ra_ide_api/src/completion/presentation.rs | 72 |
1 files changed, 56 insertions, 16 deletions
diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 232ec80cd..d386288ed 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs | |||
@@ -38,28 +38,68 @@ impl Completions { | |||
38 | &mut self, | 38 | &mut self, |
39 | ctx: &CompletionContext, | 39 | ctx: &CompletionContext, |
40 | local_name: String, | 40 | local_name: String, |
41 | res: &PerNs<Resolution>, | 41 | resolution: &PerNs<Resolution>, |
42 | ) { | 42 | ) { |
43 | use hir::ModuleDef::*; | ||
44 | |||
45 | let def = resolution.as_ref().take_types().or_else(|| resolution.as_ref().take_values()); | ||
46 | let def = match def { | ||
47 | None => { | ||
48 | self.add(CompletionItem::new( | ||
49 | CompletionKind::Reference, | ||
50 | ctx.source_range(), | ||
51 | local_name, | ||
52 | )); | ||
53 | return; | ||
54 | } | ||
55 | Some(it) => it, | ||
56 | }; | ||
57 | let (kind, docs) = match def { | ||
58 | Resolution::Def(Module(it)) => (CompletionItemKind::Module, it.docs(ctx.db)), | ||
59 | Resolution::Def(Function(func)) => { | ||
60 | return self.add_function_with_name(ctx, Some(local_name), *func); | ||
61 | } | ||
62 | Resolution::Def(Struct(it)) => (CompletionItemKind::Struct, it.docs(ctx.db)), | ||
63 | Resolution::Def(Enum(it)) => (CompletionItemKind::Enum, it.docs(ctx.db)), | ||
64 | Resolution::Def(EnumVariant(it)) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)), | ||
65 | Resolution::Def(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)), | ||
66 | Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)), | ||
67 | Resolution::Def(Trait(it)) => (CompletionItemKind::Trait, it.docs(ctx.db)), | ||
68 | Resolution::Def(Type(it)) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)), | ||
69 | Resolution::GenericParam(..) => (CompletionItemKind::TypeParam, None), | ||
70 | Resolution::LocalBinding(..) => (CompletionItemKind::Binding, None), | ||
71 | Resolution::SelfType(..) => ( | ||
72 | CompletionItemKind::TypeParam, // (does this need its own kind?) | ||
73 | None, | ||
74 | ), | ||
75 | }; | ||
43 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name) | 76 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name) |
44 | .from_resolution(ctx, res) | 77 | .kind(kind) |
45 | .add_to(self); | 78 | .set_documentation(docs) |
79 | .add_to(self) | ||
46 | } | 80 | } |
47 | 81 | ||
48 | pub(crate) fn add_function(&mut self, ctx: &CompletionContext, func: hir::Function) { | 82 | pub(crate) fn add_function(&mut self, ctx: &CompletionContext, func: hir::Function) { |
49 | let sig = func.signature(ctx.db); | 83 | self.add_function_with_name(ctx, None, func) |
84 | } | ||
50 | 85 | ||
51 | let mut builder = CompletionItem::new( | 86 | fn add_function_with_name( |
52 | CompletionKind::Reference, | 87 | &mut self, |
53 | ctx.source_range(), | 88 | ctx: &CompletionContext, |
54 | sig.name().to_string(), | 89 | name: Option<String>, |
55 | ) | 90 | func: hir::Function, |
56 | .kind(if sig.has_self_param() { | 91 | ) { |
57 | CompletionItemKind::Method | 92 | let sig = func.signature(ctx.db); |
58 | } else { | 93 | let name = name.unwrap_or_else(|| sig.name().to_string()); |
59 | CompletionItemKind::Function | 94 | |
60 | }) | 95 | let mut builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name) |
61 | .set_documentation(func.docs(ctx.db)) | 96 | .kind(if sig.has_self_param() { |
62 | .set_detail(function_item_label(ctx, func)); | 97 | CompletionItemKind::Method |
98 | } else { | ||
99 | CompletionItemKind::Function | ||
100 | }) | ||
101 | .set_documentation(func.docs(ctx.db)) | ||
102 | .set_detail(function_item_label(ctx, func)); | ||
63 | // If not an import, add parenthesis automatically. | 103 | // If not an import, add parenthesis automatically. |
64 | if ctx.use_item_syntax.is_none() && !ctx.is_call { | 104 | if ctx.use_item_syntax.is_none() && !ctx.is_call { |
65 | tested_by!(inserts_parens_for_function_calls); | 105 | tested_by!(inserts_parens_for_function_calls); |