1 //! Postfix completions, like `Ok(10).ifl$0` => `if let Ok() = Ok(10) { $0 }`.
5 use hir::{Documentation, HasAttrs};
7 helpers::{insert_use::ImportScope, SnippetCap},
11 ast::{self, AstNode, AstToken},
12 SyntaxKind::{EXPR_STMT, STMT_LIST},
15 use text_edit::TextEdit;
18 completions::postfix::format_like::add_format_like_completions, context::CompletionContext,
19 item::Builder, patterns::ImmediateLocation, CompletionItem, CompletionItemKind,
20 CompletionRelevance, Completions, SnippetScope,
23 pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
24 if !ctx.config.enable_postfix_completions {
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 {
32 receiver_is_ambiguous_float_literal,
33 }) => (it, *receiver_is_ambiguous_float_literal),
37 let receiver_text = get_receiver_text(dot_receiver, receiver_is_ambiguous_float_literal);
39 let receiver_ty = match ctx.sema.type_of_expr(dot_receiver) {
40 Some(it) => it.original,
44 // Suggest .await syntax for types that implement Future trait
45 if receiver_ty.impls_future(ctx.db) {
47 CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), "await");
48 item.detail("expr.await");
52 let cap = match ctx.config.snippet_cap {
57 let postfix_snippet = match build_postfix_snippet_builder(ctx, cap, &dot_receiver) {
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(
70 &format!("drop($0{})", receiver_text),
72 item.set_documentation(drop_fn.docs(ctx.db));
78 if !ctx.config.snippets.is_empty() {
79 add_custom_postfix_completions(acc, ctx, &postfix_snippet, &receiver_text);
82 let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty.strip_references());
83 if let Some(try_enum) = &try_enum {
89 &format!("if let Ok($1) = {} {{\n $0\n}}", receiver_text),
96 &format!("while let Ok($1) = {} {{\n $0\n}}", receiver_text),
104 &format!("if let Some($1) = {} {{\n $0\n}}", receiver_text),
111 &format!("while let Some($1) = {} {{\n $0\n}}", receiver_text),
116 } else if receiver_ty.is_bool() || receiver_ty.is_unknown() {
117 postfix_snippet("if", "if expr {}", &format!("if {} {{\n $0\n}}", receiver_text))
122 &format!("while {} {{\n $0\n}}", receiver_text),
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_, &[]) {
130 "for ele in expr {}",
131 &format!("for ele in {} {{\n $0\n}}", receiver_text),
137 postfix_snippet("ref", "&expr", &format!("&{}", receiver_text)).add_to(acc);
138 postfix_snippet("refm", "&mut expr", &format!("&mut {}", receiver_text)).add_to(acc);
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) {
150 Some(try_enum) => match try_enum {
155 &format!("match {} {{\n Ok(${{1:_}}) => {{$2}},\n Err(${{3:_}}) => {{$0}},\n}}", receiver_text),
164 "match {} {{\n Some(${{1:_}}) => {{$2}},\n None => {{$0}},\n}}",
175 &format!("match {} {{\n ${{1:_}} => {{$0}},\n}}", receiver_text),
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);
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))
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);
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()
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)
216 resulting_element = ast::Expr::from(parent_ref_element);
221 fn build_postfix_snippet_builder<'ctx>(
222 ctx: &'ctx CompletionContext,
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 let delete_range = TextRange::new(receiver_range.start(), ctx.source_range().end());
230 // Wrapping impl Fn in an option ruins lifetime inference for the parameters in a way that
231 // can't be annotated for the closure, hence fix it by constructing it without the Option first
233 ctx: &'ctx CompletionContext,
235 delete_range: TextRange,
236 ) -> impl Fn(&str, &str, &str) -> Builder + 'ctx {
237 move |label, detail, snippet| {
238 let edit = TextEdit::replace(delete_range, snippet.to_string());
240 CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), label);
241 item.detail(detail).snippet_edit(cap, edit);
242 if ctx.original_token.text() == label {
244 CompletionRelevance { exact_postfix_snippet_match: true, ..Default::default() };
245 item.set_relevance(relevance);
251 Some(build(ctx, cap, delete_range))
254 fn add_custom_postfix_completions(
255 acc: &mut Completions,
256 ctx: &CompletionContext,
257 postfix_snippet: impl Fn(&str, &str, &str) -> Builder,
260 let import_scope = ImportScope::find_insert_use_container(&ctx.token.parent()?, &ctx.sema)?;
261 ctx.config.postfix_snippets().filter(|(_, snip)| snip.scope == SnippetScope::Expr).for_each(
262 |(trigger, snippet)| {
263 let imports = match snippet.imports(ctx, &import_scope) {
264 Some(imports) => imports,
267 let body = snippet.postfix_snippet(&receiver_text);
269 postfix_snippet(trigger, snippet.description.as_deref().unwrap_or_default(), &body);
270 builder.documentation(Documentation::new(format!("```rust\n{}\n```", body)));
271 for import in imports.into_iter() {
272 builder.add_import(import);
282 use expect_test::{expect, Expect};
285 tests::{check_edit, check_edit_with_config, completion_list, TEST_CONFIG},
286 CompletionConfig, Snippet,
289 fn check(ra_fixture: &str, expect: Expect) {
290 let actual = completion_list(ra_fixture);
291 expect.assert_eq(&actual)
295 fn postfix_completion_works_for_trivial_path_expression() {
305 sn while while expr {}
309 sn match match expr {}
310 sn box Box::new(expr)
313 sn call function(expr)
321 fn postfix_completion_works_for_function_calln() {
324 fn foo(elt: bool) -> bool {
335 sn while while expr {}
339 sn match match expr {}
340 sn box Box::new(expr)
343 sn call function(expr)
349 fn postfix_type_filtering() {
360 sn match match expr {}
361 sn box Box::new(expr)
364 sn call function(expr)
372 fn let_middle_block() {
382 sn while while expr {}
386 sn match match expr {}
387 sn box Box::new(expr)
390 sn call function(expr)
404 let bar = Some(true);
410 let bar = Some(true);
411 if let Some($1) = bar {
443 fn postfix_completion_works_for_ambiguous_float_literal() {
444 check_edit("refm", r#"fn main() { 42.$0 }"#, r#"fn main() { &mut 42 }"#)
448 fn works_in_simple_macro() {
452 macro_rules! m { ($e:expr) => { $e } }
459 macro_rules! m { ($e:expr) => { $e } }
469 fn postfix_completion_for_references() {
470 check_edit("dbg", r#"fn main() { &&42.$0 }"#, r#"fn main() { dbg!(&&42) }"#);
471 check_edit("refm", r#"fn main() { &&42.$0 }"#, r#"fn main() { &&&mut 42 }"#);
477 let bar = &Some(true);
483 let bar = &Some(true);
484 if let Some($1) = bar {
493 fn custom_postfix_completion() {
494 check_edit_with_config(
496 snippets: vec![Snippet::new(
499 &["ControlFlow::Break(${receiver})".into()],
501 &["core::ops::ControlFlow".into()],
502 crate::SnippetScope::Expr,
513 use core::ops::ControlFlow;
515 fn main() { ControlFlow::Break(42) }
521 fn postfix_completion_for_format_like_strings() {
524 r#"fn main() { "{some_var:?}".$0 }"#,
525 r#"fn main() { format!("{:?}", some_var) }"#,
529 r#"fn main() { "Panic with {a}".$0 }"#,
530 r#"fn main() { panic!("Panic with {}", a) }"#,
534 r#"fn main() { "{ 2+2 } { SomeStruct { val: 1, other: 32 } :?}".$0 }"#,
535 r#"fn main() { println!("{} {:?}", 2+2, SomeStruct { val: 1, other: 32 }) }"#,
539 r#"fn main() { "{2+2}".$0 }"#,
540 r#"fn main() { log::error!("{}", 2+2) }"#,
544 r#"fn main() { "{2+2}".$0 }"#,
545 r#"fn main() { log::trace!("{}", 2+2) }"#,
549 r#"fn main() { "{2+2}".$0 }"#,
550 r#"fn main() { log::debug!("{}", 2+2) }"#,
552 check_edit("logi", r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { log::info!("{}", 2+2) }"#);
553 check_edit("logw", r#"fn main() { "{2+2}".$0 }"#, r#"fn main() { log::warn!("{}", 2+2) }"#);
556 r#"fn main() { "{2+2}".$0 }"#,
557 r#"fn main() { log::error!("{}", 2+2) }"#,