1 //! Postfix completions, like `Ok(10).ifl$0` => `if let Ok() = Ok(10) { $0 }`.
5 use ide_db::{helpers::SnippetCap, ty_filter::TryEnum};
7 ast::{self, AstNode, AstToken},
8 SyntaxKind::{BLOCK_EXPR, EXPR_STMT},
11 use text_edit::TextEdit;
14 completions::postfix::format_like::add_format_like_completions,
15 context::CompletionContext,
16 item::{Builder, CompletionKind},
17 patterns::ImmediateLocation,
18 CompletionItem, CompletionItemKind, Completions,
21 pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
22 if !ctx.config.enable_postfix_completions {
26 let (dot_receiver, receiver_is_ambiguous_float_literal) = match &ctx.completion_location {
27 Some(ImmediateLocation::MethodCall { receiver: Some(it), .. }) => (it, false),
28 Some(ImmediateLocation::FieldAccess {
30 receiver_is_ambiguous_float_literal,
31 }) => (it, *receiver_is_ambiguous_float_literal),
35 let receiver_text = get_receiver_text(dot_receiver, receiver_is_ambiguous_float_literal);
37 let receiver_ty = match ctx.sema.type_of_expr(dot_receiver) {
42 let cap = match ctx.config.snippet_cap {
46 let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty.strip_references());
47 if let Some(try_enum) = &try_enum {
56 &format!("if let Ok($1) = {} {{\n $0\n}}", receiver_text),
66 &format!("while let Ok($1) = {} {{\n $0\n}}", receiver_text),
77 &format!("if let Some($1) = {} {{\n $0\n}}", receiver_text),
87 &format!("while let Some($1) = {} {{\n $0\n}}", receiver_text),
92 } else if receiver_ty.is_bool() || receiver_ty.is_unknown() {
99 &format!("if {} {{\n $0\n}}", receiver_text),
108 &format!("while {} {{\n $0\n}}", receiver_text),
111 postfix_snippet(ctx, cap, dot_receiver, "not", "!expr", &format!("!{}", receiver_text))
115 postfix_snippet(ctx, cap, dot_receiver, "ref", "&expr", &format!("&{}", receiver_text))
123 &format!("&mut {}", receiver_text),
127 // The rest of the postfix completions create an expression that moves an argument,
128 // so it's better to consider references now to avoid breaking the compilation
129 let dot_receiver = include_references(dot_receiver);
130 let receiver_text = get_receiver_text(&dot_receiver, receiver_is_ambiguous_float_literal);
133 Some(try_enum) => match try_enum {
141 &format!("match {} {{\n Ok(${{1:_}}) => {{$2}},\n Err(${{3:_}}) => {{$0}},\n}}", receiver_text),
153 "match {} {{\n Some(${{1:_}}) => {{$2}},\n None => {{$0}},\n}}",
167 &format!("match {} {{\n ${{1:_}} => {{$0}},\n}}", receiver_text),
179 &format!("Box::new({})", receiver_text),
183 postfix_snippet(ctx, cap, &dot_receiver, "ok", "Ok(expr)", &format!("Ok({})", receiver_text))
192 &format!("Err({})", receiver_text),
202 &format!("Some({})", receiver_text),
212 &format!("dbg!({})", receiver_text),
222 &format!("dbg!(&{})", receiver_text),
232 &format!("${{1}}({})", receiver_text),
236 if let Some(parent) = dot_receiver.syntax().parent().and_then(|p| p.parent()) {
237 if matches!(parent.kind(), BLOCK_EXPR | EXPR_STMT) {
244 &format!("let $0 = {};", receiver_text),
253 &format!("let mut $0 = {};", receiver_text),
259 if let ast::Expr::Literal(literal) = dot_receiver.clone() {
260 if let Some(literal_text) = ast::String::cast(literal.token()) {
261 add_format_like_completions(acc, ctx, &dot_receiver, cap, &literal_text);
266 fn get_receiver_text(receiver: &ast::Expr, receiver_is_ambiguous_float_literal: bool) -> String {
267 if receiver_is_ambiguous_float_literal {
268 let text = receiver.syntax().text();
269 let without_dot = ..text.len() - TextSize::of('.');
270 text.slice(without_dot).to_string()
276 fn include_references(initial_element: &ast::Expr) -> ast::Expr {
277 let mut resulting_element = initial_element.clone();
278 while let Some(parent_ref_element) =
279 resulting_element.syntax().parent().and_then(ast::RefExpr::cast)
281 resulting_element = ast::Expr::from(parent_ref_element);
287 ctx: &CompletionContext,
289 receiver: &ast::Expr,
295 let receiver_syntax = receiver.syntax();
296 let receiver_range = ctx.sema.original_range(receiver_syntax).range;
297 let delete_range = TextRange::new(receiver_range.start(), ctx.source_range().end());
298 TextEdit::replace(delete_range, snippet.to_string())
300 let mut item = CompletionItem::new(CompletionKind::Postfix, ctx.source_range(), label);
301 item.detail(detail).kind(CompletionItemKind::Snippet).snippet_edit(cap, edit);
307 use expect_test::{expect, Expect};
310 test_utils::{check_edit, completion_list},
314 fn check(ra_fixture: &str, expect: Expect) {
315 let actual = completion_list(ra_fixture, CompletionKind::Postfix);
316 expect.assert_eq(&actual)
320 fn postfix_completion_works_for_trivial_path_expression() {
330 sn while while expr {}
334 sn match match expr {}
335 sn box Box::new(expr)
341 sn call function(expr)
349 fn postfix_completion_works_for_function_calln() {
352 fn foo(elt: bool) -> bool {
363 sn while while expr {}
367 sn match match expr {}
368 sn box Box::new(expr)
374 sn call function(expr)
380 fn postfix_type_filtering() {
391 sn match match expr {}
392 sn box Box::new(expr)
398 sn call function(expr)
406 fn let_middle_block() {
416 sn while while expr {}
420 sn match match expr {}
421 sn box Box::new(expr)
427 sn call function(expr)
439 enum Option<T> { Some(T), None }
442 let bar = Option::Some(true);
447 enum Option<T> { Some(T), None }
450 let bar = Option::Some(true);
451 if let Some($1) = bar {
464 enum Result<T, E> { Ok(T), Err(E) }
467 let bar = Result::Ok(true);
472 enum Result<T, E> { Ok(T), Err(E) }
475 let bar = Result::Ok(true);
486 fn postfix_completion_works_for_ambiguous_float_literal() {
487 check_edit("refm", r#"fn main() { 42.$0 }"#, r#"fn main() { &mut 42 }"#)
491 fn works_in_simple_macro() {
495 macro_rules! m { ($e:expr) => { $e } }
502 macro_rules! m { ($e:expr) => { $e } }
512 fn postfix_completion_for_references() {
513 check_edit("dbg", r#"fn main() { &&42.$0 }"#, r#"fn main() { dbg!(&&42) }"#);
514 check_edit("refm", r#"fn main() { &&42.$0 }"#, r#"fn main() { &&&mut 42 }"#);
518 enum Option<T> { Some(T), None }
521 let bar = &Option::Some(true);
526 enum Option<T> { Some(T), None }
529 let bar = &Option::Some(true);
530 if let Some($1) = bar {
539 fn postfix_completion_for_format_like_strings() {
542 r#"fn main() { "{some_var:?}".$0 }"#,
543 r#"fn main() { format!("{:?}", some_var) }"#,
547 r#"fn main() { "Panic with {a}".$0 }"#,
548 r#"fn main() { panic!("Panic with {}", a) }"#,
552 r#"fn main() { "{ 2+2 } { SomeStruct { val: 1, other: 32 } :?}".$0 }"#,
553 r#"fn main() { println!("{} {:?}", 2+2, SomeStruct { val: 1, other: 32 }) }"#,
557 r#"fn main() { "{2+2}".$0 }"#,
558 r#"fn main() { log::error!("{}", 2+2) }"#,
562 r#"fn main() { "{2+2}".$0 }"#,
563 r#"fn main() { log::trace!("{}", 2+2) }"#,
567 r#"fn main() { "{2+2}".$0 }"#,
568 r#"fn main() { log::debug!("{}", 2+2) }"#,
570 check_edit("logi", r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { log::info!("{}", 2+2) }"#);
571 check_edit("logw", r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { log::warn!("{}", 2+2) }"#);
574 r#"fn main() { "{2+2}".$0 }"#,
575 r#"fn main() { log::error!("{}", 2+2) }"#,