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 | |
parent | a650a93bf5d0217e6f366ffc91ca8b25bfd37be4 (diff) |
move more code to presentation
Diffstat (limited to 'crates/ra_ide_api/src/completion')
-rw-r--r-- | crates/ra_ide_api/src/completion/completion_item.rs | 73 | ||||
-rw-r--r-- | crates/ra_ide_api/src/completion/presentation.rs | 72 |
2 files changed, 57 insertions, 88 deletions
diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index 91e32b3c8..cb880d92c 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs | |||
@@ -1,13 +1,11 @@ | |||
1 | use std::fmt; | 1 | use std::fmt; |
2 | 2 | ||
3 | use hir::{Docs, Documentation, PerNs, Resolution}; | 3 | use hir::{Docs, Documentation}; |
4 | use ra_syntax::TextRange; | 4 | use ra_syntax::TextRange; |
5 | use ra_text_edit::{ TextEditBuilder, TextEdit}; | 5 | use ra_text_edit::{ TextEditBuilder, TextEdit}; |
6 | use test_utils::tested_by; | ||
7 | 6 | ||
8 | use crate::completion::{ | 7 | use crate::completion::{ |
9 | completion_context::CompletionContext, | 8 | completion_context::CompletionContext, |
10 | function_label, | ||
11 | const_label, | 9 | const_label, |
12 | type_label | 10 | type_label |
13 | }; | 11 | }; |
@@ -255,70 +253,6 @@ impl Builder { | |||
255 | self.documentation = docs.map(Into::into); | 253 | self.documentation = docs.map(Into::into); |
256 | self | 254 | self |
257 | } | 255 | } |
258 | pub(super) fn from_resolution( | ||
259 | mut self, | ||
260 | ctx: &CompletionContext, | ||
261 | resolution: &PerNs<Resolution>, | ||
262 | ) -> Builder { | ||
263 | use hir::ModuleDef::*; | ||
264 | |||
265 | let def = resolution.as_ref().take_types().or_else(|| resolution.as_ref().take_values()); | ||
266 | let def = match def { | ||
267 | None => return self, | ||
268 | Some(it) => it, | ||
269 | }; | ||
270 | let (kind, docs) = match def { | ||
271 | Resolution::Def(Module(it)) => (CompletionItemKind::Module, it.docs(ctx.db)), | ||
272 | Resolution::Def(Function(func)) => return self.from_function(ctx, *func), | ||
273 | Resolution::Def(Struct(it)) => (CompletionItemKind::Struct, it.docs(ctx.db)), | ||
274 | Resolution::Def(Enum(it)) => (CompletionItemKind::Enum, it.docs(ctx.db)), | ||
275 | Resolution::Def(EnumVariant(it)) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)), | ||
276 | Resolution::Def(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)), | ||
277 | Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)), | ||
278 | Resolution::Def(Trait(it)) => (CompletionItemKind::Trait, it.docs(ctx.db)), | ||
279 | Resolution::Def(Type(it)) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)), | ||
280 | Resolution::GenericParam(..) => (CompletionItemKind::TypeParam, None), | ||
281 | Resolution::LocalBinding(..) => (CompletionItemKind::Binding, None), | ||
282 | Resolution::SelfType(..) => ( | ||
283 | CompletionItemKind::TypeParam, // (does this need its own kind?) | ||
284 | None, | ||
285 | ), | ||
286 | }; | ||
287 | self.kind = Some(kind); | ||
288 | self.documentation = docs; | ||
289 | |||
290 | self | ||
291 | } | ||
292 | |||
293 | pub(super) fn from_function( | ||
294 | mut self, | ||
295 | ctx: &CompletionContext, | ||
296 | function: hir::Function, | ||
297 | ) -> Builder { | ||
298 | // If not an import, add parenthesis automatically. | ||
299 | if ctx.use_item_syntax.is_none() && !ctx.is_call { | ||
300 | tested_by!(inserts_parens_for_function_calls); | ||
301 | let sig = function.signature(ctx.db); | ||
302 | if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 { | ||
303 | self.insert_text = Some(format!("{}()$0", self.label)); | ||
304 | } else { | ||
305 | self.insert_text = Some(format!("{}($0)", self.label)); | ||
306 | } | ||
307 | self.insert_text_format = InsertTextFormat::Snippet; | ||
308 | } | ||
309 | |||
310 | if let Some(docs) = function.docs(ctx.db) { | ||
311 | self.documentation = Some(docs); | ||
312 | } | ||
313 | |||
314 | if let Some(label) = function_item_label(ctx, function) { | ||
315 | self.detail = Some(label); | ||
316 | } | ||
317 | |||
318 | self.kind = Some(CompletionItemKind::Function); | ||
319 | self | ||
320 | } | ||
321 | |||
322 | pub(super) fn from_const(mut self, ctx: &CompletionContext, ct: hir::Const) -> Builder { | 256 | pub(super) fn from_const(mut self, ctx: &CompletionContext, ct: hir::Const) -> Builder { |
323 | if let Some(docs) = ct.docs(ctx.db) { | 257 | if let Some(docs) = ct.docs(ctx.db) { |
324 | self.documentation = Some(docs); | 258 | self.documentation = Some(docs); |
@@ -373,11 +307,6 @@ impl Into<Vec<CompletionItem>> for Completions { | |||
373 | } | 307 | } |
374 | } | 308 | } |
375 | 309 | ||
376 | fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option<String> { | ||
377 | let node = function.source(ctx.db).1; | ||
378 | function_label(&node) | ||
379 | } | ||
380 | |||
381 | fn const_item_label(ctx: &CompletionContext, ct: hir::Const) -> String { | 310 | fn const_item_label(ctx: &CompletionContext, ct: hir::Const) -> String { |
382 | let node = ct.source(ctx.db).1; | 311 | let node = ct.source(ctx.db).1; |
383 | const_label(&node) | 312 | const_label(&node) |
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); |