]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions/postfix.rs
Merge #11276
[rust.git] / crates / ide_completion / src / completions / postfix.rs
1 //! Postfix completions, like `Ok(10).ifl$0` => `if let Ok() = Ok(10) { $0 }`.
2
3 mod format_like;
4
5 use hir::{Documentation, HasAttrs};
6 use ide_db::{
7     helpers::{insert_use::ImportScope, SnippetCap},
8     ty_filter::TryEnum,
9 };
10 use syntax::{
11     ast::{self, AstNode, AstToken},
12     SyntaxKind::{EXPR_STMT, STMT_LIST},
13     TextRange, TextSize,
14 };
15 use text_edit::TextEdit;
16
17 use crate::{
18     completions::postfix::format_like::add_format_like_completions, context::CompletionContext,
19     item::Builder, patterns::ImmediateLocation, CompletionItem, CompletionItemKind,
20     CompletionRelevance, Completions, SnippetScope,
21 };
22
23 pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
24     if !ctx.config.enable_postfix_completions {
25         return;
26     }
27
28     let (dot_receiver, receiver_is_ambiguous_float_literal) = match &ctx.completion_location {
29         Some(ImmediateLocation::MethodCall { receiver: Some(it), .. }) => (it, false),
30         Some(ImmediateLocation::FieldAccess {
31             receiver: Some(it),
32             receiver_is_ambiguous_float_literal,
33         }) => (it, *receiver_is_ambiguous_float_literal),
34         _ => return,
35     };
36
37     let receiver_text = get_receiver_text(dot_receiver, receiver_is_ambiguous_float_literal);
38
39     let receiver_ty = match ctx.sema.type_of_expr(dot_receiver) {
40         Some(it) => it.original,
41         None => return,
42     };
43
44     // Suggest .await syntax for types that implement Future trait
45     if receiver_ty.impls_future(ctx.db) {
46         let mut item =
47             CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), "await");
48         item.detail("expr.await");
49         item.add_to(acc);
50     }
51
52     let cap = match ctx.config.snippet_cap {
53         Some(it) => it,
54         None => return,
55     };
56
57     let postfix_snippet = match build_postfix_snippet_builder(ctx, cap, &dot_receiver) {
58         Some(it) => it,
59         None => return,
60     };
61
62     if let Some(drop_trait) = ctx.famous_defs().core_ops_Drop() {
63         if receiver_ty.impls_trait(ctx.db, drop_trait, &[]) {
64             if let &[hir::AssocItem::Function(drop_fn)] = &*drop_trait.items(ctx.db) {
65                 cov_mark::hit!(postfix_drop_completion);
66                 // FIXME: check that `drop` is in scope, use fully qualified path if it isn't/if shadowed
67                 let mut item = postfix_snippet(
68                     "drop",
69                     "fn drop(&mut self)",
70                     &format!("drop($0{})", receiver_text),
71                 );
72                 item.set_documentation(drop_fn.docs(ctx.db));
73                 item.add_to(acc);
74             }
75         }
76     }
77
78     if !ctx.config.snippets.is_empty() {
79         add_custom_postfix_completions(acc, ctx, &postfix_snippet, &receiver_text);
80     }
81
82     let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty.strip_references());
83     if let Some(try_enum) = &try_enum {
84         match try_enum {
85             TryEnum::Result => {
86                 postfix_snippet(
87                     "ifl",
88                     "if let Ok {}",
89                     &format!("if let Ok($1) = {} {{\n    $0\n}}", receiver_text),
90                 )
91                 .add_to(acc);
92
93                 postfix_snippet(
94                     "while",
95                     "while let Ok {}",
96                     &format!("while let Ok($1) = {} {{\n    $0\n}}", receiver_text),
97                 )
98                 .add_to(acc);
99             }
100             TryEnum::Option => {
101                 postfix_snippet(
102                     "ifl",
103                     "if let Some {}",
104                     &format!("if let Some($1) = {} {{\n    $0\n}}", receiver_text),
105                 )
106                 .add_to(acc);
107
108                 postfix_snippet(
109                     "while",
110                     "while let Some {}",
111                     &format!("while let Some($1) = {} {{\n    $0\n}}", receiver_text),
112                 )
113                 .add_to(acc);
114             }
115         }
116     } else if receiver_ty.is_bool() || receiver_ty.is_unknown() {
117         postfix_snippet("if", "if expr {}", &format!("if {} {{\n    $0\n}}", receiver_text))
118             .add_to(acc);
119         postfix_snippet(
120             "while",
121             "while expr {}",
122             &format!("while {} {{\n    $0\n}}", receiver_text),
123         )
124         .add_to(acc);
125         postfix_snippet("not", "!expr", &format!("!{}", receiver_text)).add_to(acc);
126     } else if let Some(trait_) = ctx.famous_defs().core_iter_IntoIterator() {
127         if receiver_ty.impls_trait(ctx.db, trait_, &[]) {
128             postfix_snippet(
129                 "for",
130                 "for ele in expr {}",
131                 &format!("for ele in {} {{\n    $0\n}}", receiver_text),
132             )
133             .add_to(acc);
134         }
135     }
136
137     postfix_snippet("ref", "&expr", &format!("&{}", receiver_text)).add_to(acc);
138     postfix_snippet("refm", "&mut expr", &format!("&mut {}", receiver_text)).add_to(acc);
139
140     // The rest of the postfix completions create an expression that moves an argument,
141     // so it's better to consider references now to avoid breaking the compilation
142     let dot_receiver = include_references(dot_receiver);
143     let receiver_text = get_receiver_text(&dot_receiver, receiver_is_ambiguous_float_literal);
144     let postfix_snippet = match build_postfix_snippet_builder(ctx, cap, &dot_receiver) {
145         Some(it) => it,
146         None => return,
147     };
148
149     match try_enum {
150         Some(try_enum) => match try_enum {
151             TryEnum::Result => {
152                 postfix_snippet(
153                     "match",
154                     "match expr {}",
155                     &format!("match {} {{\n    Ok(${{1:_}}) => {{$2}},\n    Err(${{3:_}}) => {{$0}},\n}}", receiver_text),
156                 )
157                 .add_to(acc);
158             }
159             TryEnum::Option => {
160                 postfix_snippet(
161                     "match",
162                     "match expr {}",
163                     &format!(
164                         "match {} {{\n    Some(${{1:_}}) => {{$2}},\n    None => {{$0}},\n}}",
165                         receiver_text
166                     ),
167                 )
168                 .add_to(acc);
169             }
170         },
171         None => {
172             postfix_snippet(
173                 "match",
174                 "match expr {}",
175                 &format!("match {} {{\n    ${{1:_}} => {{$0}},\n}}", receiver_text),
176             )
177             .add_to(acc);
178         }
179     }
180
181     postfix_snippet("box", "Box::new(expr)", &format!("Box::new({})", receiver_text)).add_to(acc);
182     postfix_snippet("dbg", "dbg!(expr)", &format!("dbg!({})", receiver_text)).add_to(acc);
183     postfix_snippet("dbgr", "dbg!(&expr)", &format!("dbg!(&{})", receiver_text)).add_to(acc);
184     postfix_snippet("call", "function(expr)", &format!("${{1}}({})", receiver_text)).add_to(acc);
185
186     if let Some(parent) = dot_receiver.syntax().parent().and_then(|p| p.parent()) {
187         if matches!(parent.kind(), STMT_LIST | EXPR_STMT) {
188             postfix_snippet("let", "let", &format!("let $0 = {};", receiver_text)).add_to(acc);
189             postfix_snippet("letm", "let mut", &format!("let mut $0 = {};", receiver_text))
190                 .add_to(acc);
191         }
192     }
193
194     if let ast::Expr::Literal(literal) = dot_receiver.clone() {
195         if let Some(literal_text) = ast::String::cast(literal.token()) {
196             add_format_like_completions(acc, ctx, &dot_receiver, cap, &literal_text);
197         }
198     }
199 }
200
201 fn get_receiver_text(receiver: &ast::Expr, receiver_is_ambiguous_float_literal: bool) -> String {
202     if receiver_is_ambiguous_float_literal {
203         let text = receiver.syntax().text();
204         let without_dot = ..text.len() - TextSize::of('.');
205         text.slice(without_dot).to_string()
206     } else {
207         receiver.to_string()
208     }
209 }
210
211 fn include_references(initial_element: &ast::Expr) -> ast::Expr {
212     let mut resulting_element = initial_element.clone();
213     while let Some(parent_ref_element) =
214         resulting_element.syntax().parent().and_then(ast::RefExpr::cast)
215     {
216         resulting_element = ast::Expr::from(parent_ref_element);
217     }
218     resulting_element
219 }
220
221 fn build_postfix_snippet_builder<'ctx>(
222     ctx: &'ctx CompletionContext,
223     cap: SnippetCap,
224     receiver: &'ctx ast::Expr,
225 ) -> Option<impl Fn(&str, &str, &str) -> Builder + 'ctx> {
226     let receiver_syntax = receiver.syntax();
227     let receiver_range = ctx.sema.original_range_opt(receiver_syntax)?.range;
228     if ctx.source_range().end() < receiver_range.start() {
229         // This shouldn't happen, yet it does. I assume this might be due to an incorrect token mapping.
230         return None;
231     }
232     let delete_range = TextRange::new(receiver_range.start(), ctx.source_range().end());
233
234     // Wrapping impl Fn in an option ruins lifetime inference for the parameters in a way that
235     // can't be annotated for the closure, hence fix it by constructing it without the Option first
236     fn build<'ctx>(
237         ctx: &'ctx CompletionContext,
238         cap: SnippetCap,
239         delete_range: TextRange,
240     ) -> impl Fn(&str, &str, &str) -> Builder + 'ctx {
241         move |label, detail, snippet| {
242             let edit = TextEdit::replace(delete_range, snippet.to_string());
243             let mut item =
244                 CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), label);
245             item.detail(detail).snippet_edit(cap, edit);
246             if ctx.original_token.text() == label {
247                 let relevance =
248                     CompletionRelevance { exact_postfix_snippet_match: true, ..Default::default() };
249                 item.set_relevance(relevance);
250             }
251
252             item
253         }
254     }
255     Some(build(ctx, cap, delete_range))
256 }
257
258 fn add_custom_postfix_completions(
259     acc: &mut Completions,
260     ctx: &CompletionContext,
261     postfix_snippet: impl Fn(&str, &str, &str) -> Builder,
262     receiver_text: &str,
263 ) -> Option<()> {
264     let import_scope = ImportScope::find_insert_use_container(&ctx.token.parent()?, &ctx.sema)?;
265     ctx.config.postfix_snippets().filter(|(_, snip)| snip.scope == SnippetScope::Expr).for_each(
266         |(trigger, snippet)| {
267             let imports = match snippet.imports(ctx, &import_scope) {
268                 Some(imports) => imports,
269                 None => return,
270             };
271             let body = snippet.postfix_snippet(&receiver_text);
272             let mut builder =
273                 postfix_snippet(trigger, snippet.description.as_deref().unwrap_or_default(), &body);
274             builder.documentation(Documentation::new(format!("```rust\n{}\n```", body)));
275             for import in imports.into_iter() {
276                 builder.add_import(import);
277             }
278             builder.add_to(acc);
279         },
280     );
281     None
282 }
283
284 #[cfg(test)]
285 mod tests {
286     use expect_test::{expect, Expect};
287
288     use crate::{
289         tests::{check_edit, check_edit_with_config, completion_list, TEST_CONFIG},
290         CompletionConfig, Snippet,
291     };
292
293     fn check(ra_fixture: &str, expect: Expect) {
294         let actual = completion_list(ra_fixture);
295         expect.assert_eq(&actual)
296     }
297
298     #[test]
299     fn postfix_completion_works_for_trivial_path_expression() {
300         check(
301             r#"
302 fn main() {
303     let bar = true;
304     bar.$0
305 }
306 "#,
307             expect![[r#"
308                 sn if    if expr {}
309                 sn while while expr {}
310                 sn not   !expr
311                 sn ref   &expr
312                 sn refm  &mut expr
313                 sn match match expr {}
314                 sn box   Box::new(expr)
315                 sn dbg   dbg!(expr)
316                 sn dbgr  dbg!(&expr)
317                 sn call  function(expr)
318                 sn let   let
319                 sn letm  let mut
320             "#]],
321         );
322     }
323
324     #[test]
325     fn postfix_completion_works_for_function_calln() {
326         check(
327             r#"
328 fn foo(elt: bool) -> bool {
329     !elt
330 }
331
332 fn main() {
333     let bar = true;
334     foo(bar.$0)
335 }
336 "#,
337             expect![[r#"
338                 sn if    if expr {}
339                 sn while while expr {}
340                 sn not   !expr
341                 sn ref   &expr
342                 sn refm  &mut expr
343                 sn match match expr {}
344                 sn box   Box::new(expr)
345                 sn dbg   dbg!(expr)
346                 sn dbgr  dbg!(&expr)
347                 sn call  function(expr)
348             "#]],
349         );
350     }
351
352     #[test]
353     fn postfix_type_filtering() {
354         check(
355             r#"
356 fn main() {
357     let bar: u8 = 12;
358     bar.$0
359 }
360 "#,
361             expect![[r#"
362                 sn ref   &expr
363                 sn refm  &mut expr
364                 sn match match expr {}
365                 sn box   Box::new(expr)
366                 sn dbg   dbg!(expr)
367                 sn dbgr  dbg!(&expr)
368                 sn call  function(expr)
369                 sn let   let
370                 sn letm  let mut
371             "#]],
372         )
373     }
374
375     #[test]
376     fn let_middle_block() {
377         check(
378             r#"
379 fn main() {
380     baz.l$0
381     res
382 }
383 "#,
384             expect![[r#"
385                 sn if    if expr {}
386                 sn while while expr {}
387                 sn not   !expr
388                 sn ref   &expr
389                 sn refm  &mut expr
390                 sn match match expr {}
391                 sn box   Box::new(expr)
392                 sn dbg   dbg!(expr)
393                 sn dbgr  dbg!(&expr)
394                 sn call  function(expr)
395                 sn let   let
396                 sn letm  let mut
397             "#]],
398         );
399     }
400
401     #[test]
402     fn option_iflet() {
403         check_edit(
404             "ifl",
405             r#"
406 //- minicore: option
407 fn main() {
408     let bar = Some(true);
409     bar.$0
410 }
411 "#,
412             r#"
413 fn main() {
414     let bar = Some(true);
415     if let Some($1) = bar {
416     $0
417 }
418 }
419 "#,
420         );
421     }
422
423     #[test]
424     fn result_match() {
425         check_edit(
426             "match",
427             r#"
428 //- minicore: result
429 fn main() {
430     let bar = Ok(true);
431     bar.$0
432 }
433 "#,
434             r#"
435 fn main() {
436     let bar = Ok(true);
437     match bar {
438     Ok(${1:_}) => {$2},
439     Err(${3:_}) => {$0},
440 }
441 }
442 "#,
443         );
444     }
445
446     #[test]
447     fn postfix_completion_works_for_ambiguous_float_literal() {
448         check_edit("refm", r#"fn main() { 42.$0 }"#, r#"fn main() { &mut 42 }"#)
449     }
450
451     #[test]
452     fn works_in_simple_macro() {
453         check_edit(
454             "dbg",
455             r#"
456 macro_rules! m { ($e:expr) => { $e } }
457 fn main() {
458     let bar: u8 = 12;
459     m!(bar.d$0)
460 }
461 "#,
462             r#"
463 macro_rules! m { ($e:expr) => { $e } }
464 fn main() {
465     let bar: u8 = 12;
466     m!(dbg!(bar))
467 }
468 "#,
469         );
470     }
471
472     #[test]
473     fn postfix_completion_for_references() {
474         check_edit("dbg", r#"fn main() { &&42.$0 }"#, r#"fn main() { dbg!(&&42) }"#);
475         check_edit("refm", r#"fn main() { &&42.$0 }"#, r#"fn main() { &&&mut 42 }"#);
476         check_edit(
477             "ifl",
478             r#"
479 //- minicore: option
480 fn main() {
481     let bar = &Some(true);
482     bar.$0
483 }
484 "#,
485             r#"
486 fn main() {
487     let bar = &Some(true);
488     if let Some($1) = bar {
489     $0
490 }
491 }
492 "#,
493         )
494     }
495
496     #[test]
497     fn custom_postfix_completion() {
498         check_edit_with_config(
499             CompletionConfig {
500                 snippets: vec![Snippet::new(
501                     &[],
502                     &["break".into()],
503                     &["ControlFlow::Break(${receiver})".into()],
504                     "",
505                     &["core::ops::ControlFlow".into()],
506                     crate::SnippetScope::Expr,
507                 )
508                 .unwrap()],
509                 ..TEST_CONFIG
510             },
511             "break",
512             r#"
513 //- minicore: try
514 fn main() { 42.$0 }
515 "#,
516             r#"
517 use core::ops::ControlFlow;
518
519 fn main() { ControlFlow::Break(42) }
520 "#,
521         );
522     }
523
524     #[test]
525     fn postfix_completion_for_format_like_strings() {
526         check_edit(
527             "format",
528             r#"fn main() { "{some_var:?}".$0 }"#,
529             r#"fn main() { format!("{:?}", some_var) }"#,
530         );
531         check_edit(
532             "panic",
533             r#"fn main() { "Panic with {a}".$0 }"#,
534             r#"fn main() { panic!("Panic with {}", a) }"#,
535         );
536         check_edit(
537             "println",
538             r#"fn main() { "{ 2+2 } { SomeStruct { val: 1, other: 32 } :?}".$0 }"#,
539             r#"fn main() { println!("{} {:?}", 2+2, SomeStruct { val: 1, other: 32 }) }"#,
540         );
541         check_edit(
542             "loge",
543             r#"fn main() { "{2+2}".$0 }"#,
544             r#"fn main() { log::error!("{}", 2+2) }"#,
545         );
546         check_edit(
547             "logt",
548             r#"fn main() { "{2+2}".$0 }"#,
549             r#"fn main() { log::trace!("{}", 2+2) }"#,
550         );
551         check_edit(
552             "logd",
553             r#"fn main() { "{2+2}".$0 }"#,
554             r#"fn main() { log::debug!("{}", 2+2) }"#,
555         );
556         check_edit("logi", r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { log::info!("{}", 2+2) }"#);
557         check_edit("logw", r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { log::warn!("{}", 2+2) }"#);
558         check_edit(
559             "loge",
560             r#"fn main() { "{2+2}".$0 }"#,
561             r#"fn main() { log::error!("{}", 2+2) }"#,
562         );
563     }
564 }