1 //! Postfix completions, like `Ok(10).ifl$0` => `if let Ok() = Ok(10) { $0 }`.
6 helpers::{import_assets::LocatedImport, insert_use::ImportScope, FamousDefs, SnippetCap},
10 ast::{self, AstNode, AstToken},
11 SyntaxKind::{EXPR_STMT, STMT_LIST},
14 use text_edit::TextEdit;
17 completions::postfix::format_like::add_format_like_completions,
18 context::CompletionContext,
19 item::{Builder, CompletionKind},
20 patterns::ImmediateLocation,
21 CompletionItem, CompletionItemKind, CompletionRelevance, Completions, ImportEdit,
24 pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
25 if !ctx.config.enable_postfix_completions {
29 let (dot_receiver, receiver_is_ambiguous_float_literal) = match &ctx.completion_location {
30 Some(ImmediateLocation::MethodCall { receiver: Some(it), .. }) => (it, false),
31 Some(ImmediateLocation::FieldAccess {
33 receiver_is_ambiguous_float_literal,
34 }) => (it, *receiver_is_ambiguous_float_literal),
38 let receiver_text = get_receiver_text(dot_receiver, receiver_is_ambiguous_float_literal);
40 let receiver_ty = match ctx.sema.type_of_expr(dot_receiver) {
41 Some(it) => it.original,
45 // Suggest .await syntax for types that implement Future trait
46 if receiver_ty.impls_future(ctx.db) {
47 let mut item = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await");
48 item.kind(CompletionItemKind::Keyword).detail("expr.await");
52 let cap = match ctx.config.snippet_cap {
57 let postfix_snippet = build_postfix_snippet_builder(ctx, cap, &dot_receiver);
59 if !ctx.config.postfix_snippets.is_empty() {
60 add_custom_postfix_completions(acc, ctx, &postfix_snippet, &receiver_text);
63 let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty.strip_references());
64 if let Some(try_enum) = &try_enum {
70 &format!("if let Ok($1) = {} {{\n $0\n}}", receiver_text),
77 &format!("while let Ok($1) = {} {{\n $0\n}}", receiver_text),
85 &format!("if let Some($1) = {} {{\n $0\n}}", receiver_text),
92 &format!("while let Some($1) = {} {{\n $0\n}}", receiver_text),
97 } else if receiver_ty.is_bool() || receiver_ty.is_unknown() {
98 postfix_snippet("if", "if expr {}", &format!("if {} {{\n $0\n}}", receiver_text))
103 &format!("while {} {{\n $0\n}}", receiver_text),
106 postfix_snippet("not", "!expr", &format!("!{}", receiver_text)).add_to(acc);
107 } else if let Some(trait_) = FamousDefs(&ctx.sema, ctx.krate).core_iter_IntoIterator() {
108 if receiver_ty.impls_trait(ctx.db, trait_, &[]) {
111 "for ele in expr {}",
112 &format!("for ele in {} {{\n $0\n}}", receiver_text),
118 postfix_snippet("ref", "&expr", &format!("&{}", receiver_text)).add_to(acc);
119 postfix_snippet("refm", "&mut expr", &format!("&mut {}", receiver_text)).add_to(acc);
121 // The rest of the postfix completions create an expression that moves an argument,
122 // so it's better to consider references now to avoid breaking the compilation
123 let dot_receiver = include_references(dot_receiver);
124 let receiver_text = get_receiver_text(&dot_receiver, receiver_is_ambiguous_float_literal);
125 let postfix_snippet = build_postfix_snippet_builder(ctx, cap, &dot_receiver);
128 Some(try_enum) => match try_enum {
133 &format!("match {} {{\n Ok(${{1:_}}) => {{$2}},\n Err(${{3:_}}) => {{$0}},\n}}", receiver_text),
142 "match {} {{\n Some(${{1:_}}) => {{$2}},\n None => {{$0}},\n}}",
153 &format!("match {} {{\n ${{1:_}} => {{$0}},\n}}", receiver_text),
159 postfix_snippet("box", "Box::new(expr)", &format!("Box::new({})", receiver_text)).add_to(acc);
160 postfix_snippet("ok", "Ok(expr)", &format!("Ok({})", receiver_text)).add_to(acc);
161 postfix_snippet("err", "Err(expr)", &format!("Err({})", receiver_text)).add_to(acc);
162 postfix_snippet("some", "Some(expr)", &format!("Some({})", receiver_text)).add_to(acc);
163 postfix_snippet("dbg", "dbg!(expr)", &format!("dbg!({})", receiver_text)).add_to(acc);
164 postfix_snippet("dbgr", "dbg!(&expr)", &format!("dbg!(&{})", receiver_text)).add_to(acc);
165 postfix_snippet("call", "function(expr)", &format!("${{1}}({})", receiver_text)).add_to(acc);
167 if let Some(parent) = dot_receiver.syntax().parent().and_then(|p| p.parent()) {
168 if matches!(parent.kind(), STMT_LIST | EXPR_STMT) {
169 postfix_snippet("let", "let", &format!("let $0 = {};", receiver_text)).add_to(acc);
170 postfix_snippet("letm", "let mut", &format!("let mut $0 = {};", receiver_text))
175 if let ast::Expr::Literal(literal) = dot_receiver.clone() {
176 if let Some(literal_text) = ast::String::cast(literal.token()) {
177 add_format_like_completions(acc, ctx, &dot_receiver, cap, &literal_text);
182 fn get_receiver_text(receiver: &ast::Expr, receiver_is_ambiguous_float_literal: bool) -> String {
183 if receiver_is_ambiguous_float_literal {
184 let text = receiver.syntax().text();
185 let without_dot = ..text.len() - TextSize::of('.');
186 text.slice(without_dot).to_string()
192 fn include_references(initial_element: &ast::Expr) -> ast::Expr {
193 let mut resulting_element = initial_element.clone();
194 while let Some(parent_ref_element) =
195 resulting_element.syntax().parent().and_then(ast::RefExpr::cast)
197 resulting_element = ast::Expr::from(parent_ref_element);
202 fn build_postfix_snippet_builder<'a>(
203 ctx: &'a CompletionContext,
205 receiver: &'a ast::Expr,
206 ) -> impl Fn(&str, &str, &str) -> Builder + 'a {
207 let receiver_syntax = receiver.syntax();
208 let receiver_range = ctx.sema.original_range(receiver_syntax).range;
209 let delete_range = TextRange::new(receiver_range.start(), ctx.source_range().end());
211 move |label, detail, snippet| {
212 let edit = TextEdit::replace(delete_range, snippet.to_string());
213 let mut item = CompletionItem::new(CompletionKind::Postfix, ctx.source_range(), label);
214 item.detail(detail).kind(CompletionItemKind::Snippet).snippet_edit(cap, edit);
215 if ctx.original_token.text() == label {
217 CompletionRelevance { exact_postfix_snippet_match: true, ..Default::default() };
218 item.set_relevance(relevance);
225 fn add_custom_postfix_completions(
226 acc: &mut Completions,
227 ctx: &CompletionContext,
228 postfix_snippet: impl Fn(&str, &str, &str) -> Builder,
232 ImportScope::find_insert_use_container_with_macros(&ctx.token.parent()?, &ctx.sema)?;
233 ctx.config.postfix_snippets.iter().for_each(|snippet| {
234 // FIXME: Support multiple imports
235 let import = match snippet.requires.get(0) {
238 let path = ast::Path::parse(import).ok()?;
239 match ctx.scope.speculative_resolve(&path)? {
240 hir::PathResolution::Macro(_) => None,
241 hir::PathResolution::Def(def) => {
242 let item = def.into();
243 let path = ctx.scope.module()?.find_use_path_prefixed(
246 ctx.config.insert_use.prefix_kind,
248 Some((path.len() > 1).then(|| ImportEdit {
249 import: LocatedImport::new(path.clone(), item, item, None),
250 scope: import_scope.clone(),
263 let mut builder = postfix_snippet(
265 snippet.description.as_deref().unwrap_or_default(),
266 &format!("{}", snippet.snippet(&receiver_text)),
268 builder.add_import(import);
276 use expect_test::{expect, Expect};
279 tests::{check_edit, check_edit_with_config, filtered_completion_list, TEST_CONFIG},
280 CompletionConfig, CompletionKind, PostfixSnippet,
283 fn check(ra_fixture: &str, expect: Expect) {
284 let actual = filtered_completion_list(ra_fixture, CompletionKind::Postfix);
285 expect.assert_eq(&actual)
289 fn postfix_completion_works_for_trivial_path_expression() {
299 sn while while expr {}
303 sn match match expr {}
304 sn box Box::new(expr)
310 sn call function(expr)
318 fn postfix_completion_works_for_function_calln() {
321 fn foo(elt: bool) -> bool {
332 sn while while expr {}
336 sn match match expr {}
337 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)
367 sn call function(expr)
375 fn let_middle_block() {
385 sn while while expr {}
389 sn match match expr {}
390 sn box Box::new(expr)
396 sn call function(expr)
410 let bar = Some(true);
416 let bar = Some(true);
417 if let Some($1) = bar {
449 fn postfix_completion_works_for_ambiguous_float_literal() {
450 check_edit("refm", r#"fn main() { 42.$0 }"#, r#"fn main() { &mut 42 }"#)
454 fn works_in_simple_macro() {
458 macro_rules! m { ($e:expr) => { $e } }
465 macro_rules! m { ($e:expr) => { $e } }
475 fn postfix_completion_for_references() {
476 check_edit("dbg", r#"fn main() { &&42.$0 }"#, r#"fn main() { dbg!(&&42) }"#);
477 check_edit("refm", r#"fn main() { &&42.$0 }"#, r#"fn main() { &&&mut 42 }"#);
483 let bar = &Some(true);
489 let bar = &Some(true);
490 if let Some($1) = bar {
499 fn custom_postfix_completion() {
500 check_edit_with_config(
502 postfix_snippets: vec![PostfixSnippet::new(
504 &["ControlFlow::Break($target)".into()],
506 &["core::ops::ControlFlow".into()],
517 use core::ops::ControlFlow;
519 fn main() { ControlFlow::Break(42) }
525 fn postfix_completion_for_format_like_strings() {
528 r#"fn main() { "{some_var:?}".$0 }"#,
529 r#"fn main() { format!("{:?}", some_var) }"#,
533 r#"fn main() { "Panic with {a}".$0 }"#,
534 r#"fn main() { panic!("Panic with {}", a) }"#,
538 r#"fn main() { "{ 2+2 } { SomeStruct { val: 1, other: 32 } :?}".$0 }"#,
539 r#"fn main() { println!("{} {:?}", 2+2, SomeStruct { val: 1, other: 32 }) }"#,
543 r#"fn main() { "{2+2}".$0 }"#,
544 r#"fn main() { log::error!("{}", 2+2) }"#,
548 r#"fn main() { "{2+2}".$0 }"#,
549 r#"fn main() { log::trace!("{}", 2+2) }"#,
553 r#"fn main() { "{2+2}".$0 }"#,
554 r#"fn main() { log::debug!("{}", 2+2) }"#,
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) }"#);
560 r#"fn main() { "{2+2}".$0 }"#,
561 r#"fn main() { log::error!("{}", 2+2) }"#,