1 //! Renderer for function calls.
3 use hir::{HasSource, HirDisplay, Type};
4 use ide_db::SymbolKind;
8 item::{CompletionItem, CompletionItemKind, CompletionKind, ImportEdit},
9 render::{builder_ext::Params, RenderContext},
12 pub(crate) fn render_fn<'a>(
13 ctx: RenderContext<'a>,
14 import_to_add: Option<ImportEdit>,
15 local_name: Option<String>,
17 ) -> Option<CompletionItem> {
18 let _p = profile::span("render_fn");
19 Some(FunctionRender::new(ctx, local_name, fn_)?.render(import_to_add))
23 struct FunctionRender<'a> {
24 ctx: RenderContext<'a>,
30 impl<'a> FunctionRender<'a> {
32 ctx: RenderContext<'a>,
33 local_name: Option<String>,
35 ) -> Option<FunctionRender<'a>> {
36 let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string());
37 let ast_node = fn_.source(ctx.db())?.value;
39 Some(FunctionRender { ctx, name, func: fn_, ast_node })
42 fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
43 let params = self.params();
44 let mut builder = CompletionItem::new(
45 CompletionKind::Reference,
46 self.ctx.source_range(),
51 .set_documentation(self.ctx.docs(self.func))
53 self.ctx.is_deprecated(self.func) || self.ctx.is_deprecated_assoc_item(self.func),
55 .detail(self.detail())
56 .add_call_parens(self.ctx.completion, self.name, params)
57 .add_import(import_to_add);
62 fn detail(&self) -> String {
63 let ty = self.func.ret_type(self.ctx.db());
64 format!("-> {}", ty.display(self.ctx.db()))
67 fn add_arg(&self, arg: &str, ty: &Type) -> String {
68 if let Some(derefed_ty) = ty.remove_ref() {
69 for (name, local) in self.ctx.completion.locals.iter() {
70 if name == arg && local.ty(self.ctx.db()) == derefed_ty {
71 let mutability = if ty.is_mutable_reference() { "&mut " } else { "&" };
72 return format!("{}{}", mutability, arg);
79 fn params(&self) -> Params {
80 let ast_params = match self.ast_node.param_list() {
82 None => return Params::Named(Vec::new()),
85 let mut params_pats = Vec::new();
86 let params_ty = if self.ctx.completion.dot_receiver.is_some() {
87 self.func.method_params(self.ctx.db()).unwrap_or_default()
89 if let Some(s) = ast_params.self_param() {
90 cov_mark::hit!(parens_for_method_call_as_assoc_fn);
91 params_pats.push(Some(s.to_string()));
93 self.func.assoc_fn_params(self.ctx.db())
96 .extend(ast_params.params().into_iter().map(|it| it.pat().map(|it| it.to_string())));
98 let params = params_pats
101 .flat_map(|(pat, param_ty)| {
104 let arg = name.trim_start_matches("mut ").trim_start_matches('_');
105 Some(self.add_arg(arg, param_ty.ty()))
108 Params::Named(params)
111 fn kind(&self) -> CompletionItemKind {
112 if self.func.self_param(self.ctx.db()).is_some() {
113 CompletionItemKind::Method
115 SymbolKind::Function.into()
123 test_utils::{check_edit, check_edit_with_config, TEST_CONFIG},
128 fn inserts_parens_for_function_calls() {
129 cov_mark::check!(inserts_parens_for_function_calls);
138 fn main() { no_args()$0 }
145 fn with_args(x: i32, y: String) {}
146 fn main() { with_$0 }
149 fn with_args(x: i32, y: String) {}
150 fn main() { with_args(${1:x}, ${2:y})$0 }
161 fn bar(s: &S) { s.f$0 }
168 fn bar(s: &S) { s.foo()$0 }
177 fn foo(&self, x: i32) {}
186 fn foo(&self, x: i32) {}
196 fn parens_for_method_call_as_assoc_fn() {
197 cov_mark::check!(parens_for_method_call_as_assoc_fn);
212 fn main() { S::foo(${1:&self})$0 }
218 fn suppress_arg_snippets() {
219 cov_mark::check!(suppress_arg_snippets);
220 check_edit_with_config(
221 CompletionConfig { add_call_argument_snippets: false, ..TEST_CONFIG },
224 fn with_args(x: i32, y: String) {}
225 fn main() { with_$0 }
228 fn with_args(x: i32, y: String) {}
229 fn main() { with_args($0) }
235 fn strips_underscores_from_args() {
239 fn foo(_foo: i32, ___bar: bool, ho_ge_: String) {}
243 fn foo(_foo: i32, ___bar: bool, ho_ge_: String) {}
244 fn main() { foo(${1:foo}, ${2:bar}, ${3:ho_ge_})$0 }
250 fn insert_ref_when_matching_local_in_scope() {
255 fn ref_arg(x: &Foo) {}
263 fn ref_arg(x: &Foo) {}
273 fn insert_mut_ref_when_matching_local_in_scope() {
278 fn ref_arg(x: &mut Foo) {}
286 fn ref_arg(x: &mut Foo) {}
289 ref_arg(${1:&mut x})$0
296 fn insert_ref_when_matching_local_in_scope_for_method() {
303 fn apply_foo(&self, x: &Foo) {}
316 fn apply_foo(&self, x: &Foo) {}
322 y.apply_foo(${1:&x})$0
329 fn trim_mut_keyword_in_func_completion() {
333 fn take_mutably(mut x: &i32) {}
340 fn take_mutably(mut x: &i32) {}
343 take_mutably(${1:x})$0