aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-02-24 16:34:27 +0000
committerAleksey Kladov <[email protected]>2019-02-24 16:34:27 +0000
commitb04cadc02c72055ed97eb5b3c1318f1c3b5602dc (patch)
treec30b82d12240e7564104ec3c467e47ce30a3bfc3 /crates
parentd0a261468e67be9213a3fae20f1511e303cac064 (diff)
move function rendering to presentation
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide_api/src/completion/complete_dot.rs11
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs9
-rw-r--r--crates/ra_ide_api/src/completion/presentation.rs41
-rw-r--r--crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_if_already_call_assoc_fn.snap6
-rw-r--r--crates/ra_ide_api/src/completion/snapshots/completion_item__struct_associated_method.snap6
5 files changed, 49 insertions, 24 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs
index c9099bcd7..a6b988062 100644
--- a/crates/ra_ide_api/src/completion/complete_dot.rs
+++ b/crates/ra_ide_api/src/completion/complete_dot.rs
@@ -1,6 +1,6 @@
1use hir::{Ty, AdtDef}; 1use hir::{Ty, AdtDef};
2 2
3use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind, CompletionKind}; 3use crate::completion::{CompletionContext, Completions, CompletionKind};
4 4
5/// Complete dot accesses, i.e. fields or methods (currently only fields). 5/// Complete dot accesses, i.e. fields or methods (currently only fields).
6pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { 6pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
@@ -50,14 +50,7 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty
50 receiver.iterate_methods(ctx.db, |_ty, func| { 50 receiver.iterate_methods(ctx.db, |_ty, func| {
51 let sig = func.signature(ctx.db); 51 let sig = func.signature(ctx.db);
52 if sig.has_self_param() { 52 if sig.has_self_param() {
53 CompletionItem::new( 53 acc.add_function(CompletionKind::Reference, ctx, func);
54 CompletionKind::Reference,
55 ctx.source_range(),
56 sig.name().to_string(),
57 )
58 .from_function(ctx, func)
59 .kind(CompletionItemKind::Method)
60 .add_to(acc);
61 } 54 }
62 None::<()> 55 None::<()>
63 }); 56 });
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs
index d337fe970..f8595b5c4 100644
--- a/crates/ra_ide_api/src/completion/complete_path.rs
+++ b/crates/ra_ide_api/src/completion/complete_path.rs
@@ -64,14 +64,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
64 hir::ImplItem::Method(func) => { 64 hir::ImplItem::Method(func) => {
65 let sig = func.signature(ctx.db); 65 let sig = func.signature(ctx.db);
66 if !sig.has_self_param() { 66 if !sig.has_self_param() {
67 CompletionItem::new( 67 acc.add_function(CompletionKind::Reference, ctx, func);
68 CompletionKind::Reference,
69 ctx.source_range(),
70 sig.name().to_string(),
71 )
72 .from_function(ctx, func)
73 .kind(CompletionItemKind::Method)
74 .add_to(acc);
75 } 68 }
76 None::<()> 69 None::<()>
77 } 70 }
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.
2use test_utils::tested_by;
2use hir::Docs; 3use hir::Docs;
3 4
4use crate::completion::{Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem}; 5use crate::completion::{
6 Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem,
7 function_label,
8};
5 9
6impl Completions { 10impl 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
69fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option<String> {
70 let node = function.source(ctx.db).1;
71 function_label(&node)
33} 72}
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_if_already_call_assoc_fn.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_if_already_call_assoc_fn.snap
index 39fff2cb7..19375ea95 100644
--- a/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_if_already_call_assoc_fn.snap
+++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_if_already_call_assoc_fn.snap
@@ -1,6 +1,6 @@
1--- 1---
2created: "2019-02-18T09:22:24.268227065Z" 2created: "2019-02-24T16:33:48.008220694Z"
3creator: [email protected].2 3creator: [email protected].3
4source: crates/ra_ide_api/src/completion/completion_item.rs 4source: crates/ra_ide_api/src/completion/completion_item.rs
5expression: kind_completions 5expression: kind_completions
6--- 6---
@@ -10,7 +10,7 @@ expression: kind_completions
10 source_range: [67; 69), 10 source_range: [67; 69),
11 delete: [67; 69), 11 delete: [67; 69),
12 insert: "new", 12 insert: "new",
13 kind: Method, 13 kind: Function,
14 detail: "fn new() -> Foo" 14 detail: "fn new() -> Foo"
15 } 15 }
16] 16]
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_associated_method.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_associated_method.snap
index 163b325c3..7c69eebeb 100644
--- a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_associated_method.snap
+++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_associated_method.snap
@@ -1,6 +1,6 @@
1--- 1---
2created: "2019-02-18T09:22:24.093082839Z" 2created: "2019-02-24T16:33:47.990111169Z"
3creator: [email protected].2 3creator: [email protected].3
4source: crates/ra_ide_api/src/completion/completion_item.rs 4source: crates/ra_ide_api/src/completion/completion_item.rs
5expression: kind_completions 5expression: kind_completions
6--- 6---
@@ -10,7 +10,7 @@ expression: kind_completions
10 source_range: [100; 100), 10 source_range: [100; 100),
11 delete: [100; 100), 11 delete: [100; 100),
12 insert: "m()$0", 12 insert: "m()$0",
13 kind: Method, 13 kind: Function,
14 detail: "fn m()", 14 detail: "fn m()",
15 documentation: Documentation( 15 documentation: Documentation(
16 "An associated method" 16 "An associated method"