diff options
Diffstat (limited to 'crates/ra_ide_api/src/completion/presentation.rs')
-rw-r--r-- | crates/ra_ide_api/src/completion/presentation.rs | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 6b2de56bc..9c31a3801 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs | |||
@@ -1,7 +1,11 @@ | |||
1 | //! This modules takes care of rendering various defenitions as completion items. | 1 | //! This modules takes care of rendering various defenitions as completion items. |
2 | use test_utils::tested_by; | ||
2 | use hir::Docs; | 3 | use hir::Docs; |
3 | 4 | ||
4 | use crate::completion::{Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem}; | 5 | use crate::completion::{ |
6 | Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem, | ||
7 | function_label, | ||
8 | }; | ||
5 | 9 | ||
6 | impl Completions { | 10 | impl Completions { |
7 | pub(crate) fn add_field( | 11 | pub(crate) fn add_field( |
@@ -30,4 +34,39 @@ impl Completions { | |||
30 | .detail(ty.to_string()) | 34 | .detail(ty.to_string()) |
31 | .add_to(self); | 35 | .add_to(self); |
32 | } | 36 | } |
37 | |||
38 | pub(crate) fn add_function( | ||
39 | &mut self, | ||
40 | kind: CompletionKind, | ||
41 | ctx: &CompletionContext, | ||
42 | func: hir::Function, | ||
43 | ) { | ||
44 | let sig = func.signature(ctx.db); | ||
45 | |||
46 | let mut builder = CompletionItem::new(kind, ctx.source_range(), sig.name().to_string()) | ||
47 | .kind(if sig.has_self_param() { | ||
48 | CompletionItemKind::Method | ||
49 | } else { | ||
50 | CompletionItemKind::Function | ||
51 | }) | ||
52 | .set_documentation(func.docs(ctx.db)) | ||
53 | .set_detail(function_item_label(ctx, func)); | ||
54 | // If not an import, add parenthesis automatically. | ||
55 | if ctx.use_item_syntax.is_none() && !ctx.is_call { | ||
56 | tested_by!(inserts_parens_for_function_calls); | ||
57 | let snippet = | ||
58 | if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 { | ||
59 | format!("{}()$0", sig.name()) | ||
60 | } else { | ||
61 | format!("{}($0)", sig.name()) | ||
62 | }; | ||
63 | builder = builder.insert_snippet(snippet); | ||
64 | } | ||
65 | self.add(builder) | ||
66 | } | ||
67 | } | ||
68 | |||
69 | fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option<String> { | ||
70 | let node = function.source(ctx.db).1; | ||
71 | function_label(&node) | ||
33 | } | 72 | } |