]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/render/function.rs
use references in CompletionItem's builder
[rust.git] / crates / ide_completion / src / render / function.rs
1 //! Renderer for function calls.
2
3 use hir::{HasSource, HirDisplay, Type};
4 use ide_db::SymbolKind;
5 use syntax::ast::Fn;
6
7 use crate::{
8     item::{CompletionItem, CompletionItemKind, CompletionKind, ImportEdit},
9     render::{builder_ext::Params, RenderContext},
10 };
11
12 pub(crate) fn render_fn<'a>(
13     ctx: RenderContext<'a>,
14     import_to_add: Option<ImportEdit>,
15     local_name: Option<String>,
16     fn_: hir::Function,
17 ) -> Option<CompletionItem> {
18     let _p = profile::span("render_fn");
19     Some(FunctionRender::new(ctx, local_name, fn_)?.render(import_to_add))
20 }
21
22 #[derive(Debug)]
23 struct FunctionRender<'a> {
24     ctx: RenderContext<'a>,
25     name: String,
26     func: hir::Function,
27     ast_node: Fn,
28 }
29
30 impl<'a> FunctionRender<'a> {
31     fn new(
32         ctx: RenderContext<'a>,
33         local_name: Option<String>,
34         fn_: hir::Function,
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;
38
39         Some(FunctionRender { ctx, name, func: fn_, ast_node })
40     }
41
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(),
47             self.name.clone(),
48         );
49         builder
50             .kind(self.kind())
51             .set_documentation(self.ctx.docs(self.func))
52             .set_deprecated(
53                 self.ctx.is_deprecated(self.func) || self.ctx.is_deprecated_assoc_item(self.func),
54             )
55             .detail(self.detail())
56             .add_call_parens(self.ctx.completion, self.name, params)
57             .add_import(import_to_add);
58
59         builder.build()
60     }
61
62     fn detail(&self) -> String {
63         let ty = self.func.ret_type(self.ctx.db());
64         format!("-> {}", ty.display(self.ctx.db()))
65     }
66
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);
73                 }
74             }
75         }
76         arg.to_string()
77     }
78
79     fn params(&self) -> Params {
80         let ast_params = match self.ast_node.param_list() {
81             Some(it) => it,
82             None => return Params::Named(Vec::new()),
83         };
84
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()
88         } else {
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()));
92             }
93             self.func.assoc_fn_params(self.ctx.db())
94         };
95         params_pats
96             .extend(ast_params.params().into_iter().map(|it| it.pat().map(|it| it.to_string())));
97
98         let params = params_pats
99             .into_iter()
100             .zip(params_ty)
101             .flat_map(|(pat, param_ty)| {
102                 let pat = pat?;
103                 let name = pat;
104                 let arg = name.trim_start_matches("mut ").trim_start_matches('_');
105                 Some(self.add_arg(arg, param_ty.ty()))
106             })
107             .collect();
108         Params::Named(params)
109     }
110
111     fn kind(&self) -> CompletionItemKind {
112         if self.func.self_param(self.ctx.db()).is_some() {
113             CompletionItemKind::Method
114         } else {
115             SymbolKind::Function.into()
116         }
117     }
118 }
119
120 #[cfg(test)]
121 mod tests {
122     use crate::{
123         test_utils::{check_edit, check_edit_with_config, TEST_CONFIG},
124         CompletionConfig,
125     };
126
127     #[test]
128     fn inserts_parens_for_function_calls() {
129         cov_mark::check!(inserts_parens_for_function_calls);
130         check_edit(
131             "no_args",
132             r#"
133 fn no_args() {}
134 fn main() { no_$0 }
135 "#,
136             r#"
137 fn no_args() {}
138 fn main() { no_args()$0 }
139 "#,
140         );
141
142         check_edit(
143             "with_args",
144             r#"
145 fn with_args(x: i32, y: String) {}
146 fn main() { with_$0 }
147 "#,
148             r#"
149 fn with_args(x: i32, y: String) {}
150 fn main() { with_args(${1:x}, ${2:y})$0 }
151 "#,
152         );
153
154         check_edit(
155             "foo",
156             r#"
157 struct S;
158 impl S {
159     fn foo(&self) {}
160 }
161 fn bar(s: &S) { s.f$0 }
162 "#,
163             r#"
164 struct S;
165 impl S {
166     fn foo(&self) {}
167 }
168 fn bar(s: &S) { s.foo()$0 }
169 "#,
170         );
171
172         check_edit(
173             "foo",
174             r#"
175 struct S {}
176 impl S {
177     fn foo(&self, x: i32) {}
178 }
179 fn bar(s: &S) {
180     s.f$0
181 }
182 "#,
183             r#"
184 struct S {}
185 impl S {
186     fn foo(&self, x: i32) {}
187 }
188 fn bar(s: &S) {
189     s.foo(${1:x})$0
190 }
191 "#,
192         );
193     }
194
195     #[test]
196     fn parens_for_method_call_as_assoc_fn() {
197         cov_mark::check!(parens_for_method_call_as_assoc_fn);
198         check_edit(
199             "foo",
200             r#"
201 struct S;
202 impl S {
203     fn foo(&self) {}
204 }
205 fn main() { S::f$0 }
206 "#,
207             r#"
208 struct S;
209 impl S {
210     fn foo(&self) {}
211 }
212 fn main() { S::foo(${1:&self})$0 }
213 "#,
214         );
215     }
216
217     #[test]
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 },
222             "with_args",
223             r#"
224 fn with_args(x: i32, y: String) {}
225 fn main() { with_$0 }
226 "#,
227             r#"
228 fn with_args(x: i32, y: String) {}
229 fn main() { with_args($0) }
230 "#,
231         );
232     }
233
234     #[test]
235     fn strips_underscores_from_args() {
236         check_edit(
237             "foo",
238             r#"
239 fn foo(_foo: i32, ___bar: bool, ho_ge_: String) {}
240 fn main() { f$0 }
241 "#,
242             r#"
243 fn foo(_foo: i32, ___bar: bool, ho_ge_: String) {}
244 fn main() { foo(${1:foo}, ${2:bar}, ${3:ho_ge_})$0 }
245 "#,
246         );
247     }
248
249     #[test]
250     fn insert_ref_when_matching_local_in_scope() {
251         check_edit(
252             "ref_arg",
253             r#"
254 struct Foo {}
255 fn ref_arg(x: &Foo) {}
256 fn main() {
257     let x = Foo {};
258     ref_ar$0
259 }
260 "#,
261             r#"
262 struct Foo {}
263 fn ref_arg(x: &Foo) {}
264 fn main() {
265     let x = Foo {};
266     ref_arg(${1:&x})$0
267 }
268 "#,
269         );
270     }
271
272     #[test]
273     fn insert_mut_ref_when_matching_local_in_scope() {
274         check_edit(
275             "ref_arg",
276             r#"
277 struct Foo {}
278 fn ref_arg(x: &mut Foo) {}
279 fn main() {
280     let x = Foo {};
281     ref_ar$0
282 }
283 "#,
284             r#"
285 struct Foo {}
286 fn ref_arg(x: &mut Foo) {}
287 fn main() {
288     let x = Foo {};
289     ref_arg(${1:&mut x})$0
290 }
291 "#,
292         );
293     }
294
295     #[test]
296     fn insert_ref_when_matching_local_in_scope_for_method() {
297         check_edit(
298             "apply_foo",
299             r#"
300 struct Foo {}
301 struct Bar {}
302 impl Bar {
303     fn apply_foo(&self, x: &Foo) {}
304 }
305
306 fn main() {
307     let x = Foo {};
308     let y = Bar {};
309     y.$0
310 }
311 "#,
312             r#"
313 struct Foo {}
314 struct Bar {}
315 impl Bar {
316     fn apply_foo(&self, x: &Foo) {}
317 }
318
319 fn main() {
320     let x = Foo {};
321     let y = Bar {};
322     y.apply_foo(${1:&x})$0
323 }
324 "#,
325         );
326     }
327
328     #[test]
329     fn trim_mut_keyword_in_func_completion() {
330         check_edit(
331             "take_mutably",
332             r#"
333 fn take_mutably(mut x: &i32) {}
334
335 fn main() {
336     take_m$0
337 }
338 "#,
339             r#"
340 fn take_mutably(mut x: &i32) {}
341
342 fn main() {
343     take_mutably(${1:x})$0
344 }
345 "#,
346         );
347     }
348 }