From b04cadc02c72055ed97eb5b3c1318f1c3b5602dc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 19:34:27 +0300 Subject: move function rendering to presentation --- crates/ra_ide_api/src/completion/complete_dot.rs | 11 ++---- crates/ra_ide_api/src/completion/complete_path.rs | 9 +---- crates/ra_ide_api/src/completion/presentation.rs | 41 +++++++++++++++++++++- ...r_function_parens_if_already_call_assoc_fn.snap | 6 ++-- .../completion_item__struct_associated_method.snap | 6 ++-- 5 files changed, 49 insertions(+), 24 deletions(-) (limited to 'crates') 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 @@ use hir::{Ty, AdtDef}; -use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind, CompletionKind}; +use crate::completion::{CompletionContext, Completions, CompletionKind}; /// Complete dot accesses, i.e. fields or methods (currently only fields). pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { @@ -50,14 +50,7 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty receiver.iterate_methods(ctx.db, |_ty, func| { let sig = func.signature(ctx.db); if sig.has_self_param() { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - sig.name().to_string(), - ) - .from_function(ctx, func) - .kind(CompletionItemKind::Method) - .add_to(acc); + acc.add_function(CompletionKind::Reference, ctx, func); } None::<()> }); 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) { hir::ImplItem::Method(func) => { let sig = func.signature(ctx.db); if !sig.has_self_param() { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - sig.name().to_string(), - ) - .from_function(ctx, func) - .kind(CompletionItemKind::Method) - .add_to(acc); + acc.add_function(CompletionKind::Reference, ctx, func); } None::<()> } 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 @@ //! This modules takes care of rendering various defenitions as completion items. +use test_utils::tested_by; use hir::Docs; -use crate::completion::{Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem}; +use crate::completion::{ + Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem, + function_label, +}; impl Completions { pub(crate) fn add_field( @@ -30,4 +34,39 @@ impl Completions { .detail(ty.to_string()) .add_to(self); } + + pub(crate) fn add_function( + &mut self, + kind: CompletionKind, + ctx: &CompletionContext, + func: hir::Function, + ) { + let sig = func.signature(ctx.db); + + let mut builder = CompletionItem::new(kind, ctx.source_range(), sig.name().to_string()) + .kind(if sig.has_self_param() { + CompletionItemKind::Method + } else { + CompletionItemKind::Function + }) + .set_documentation(func.docs(ctx.db)) + .set_detail(function_item_label(ctx, func)); + // If not an import, add parenthesis automatically. + if ctx.use_item_syntax.is_none() && !ctx.is_call { + tested_by!(inserts_parens_for_function_calls); + let snippet = + if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 { + format!("{}()$0", sig.name()) + } else { + format!("{}($0)", sig.name()) + }; + builder = builder.insert_snippet(snippet); + } + self.add(builder) + } +} + +fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option { + let node = function.source(ctx.db).1; + function_label(&node) } 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 @@ --- -created: "2019-02-18T09:22:24.268227065Z" -creator: insta@0.6.2 +created: "2019-02-24T16:33:48.008220694Z" +creator: insta@0.6.3 source: crates/ra_ide_api/src/completion/completion_item.rs expression: kind_completions --- @@ -10,7 +10,7 @@ expression: kind_completions source_range: [67; 69), delete: [67; 69), insert: "new", - kind: Method, + kind: Function, detail: "fn new() -> Foo" } ] 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 @@ --- -created: "2019-02-18T09:22:24.093082839Z" -creator: insta@0.6.2 +created: "2019-02-24T16:33:47.990111169Z" +creator: insta@0.6.3 source: crates/ra_ide_api/src/completion/completion_item.rs expression: kind_completions --- @@ -10,7 +10,7 @@ expression: kind_completions source_range: [100; 100), delete: [100; 100), insert: "m()$0", - kind: Method, + kind: Function, detail: "fn m()", documentation: Documentation( "An associated method" -- cgit v1.2.3