]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_completion/src/completions/record.rs
Merge #9005
[rust.git] / crates / ide_completion / src / completions / record.rs
index 0a7927eb8340b025b78948503dbfac3a8fe0d06f..e1526b70b8c89c556cf68ef39527f8bb6c0a691d 100644 (file)
@@ -13,25 +13,21 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
             let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_lit.clone()));
             let default_trait = FamousDefs(&ctx.sema, ctx.krate).core_default_Default();
             let impl_default_trait = default_trait
-                .and_then(|default_trait| ty.map(|ty| ty.impls_trait(ctx.db, default_trait, &[])))
-                .unwrap_or(false);
+                .zip(ty)
+                .map_or(false, |(default_trait, ty)| ty.impls_trait(ctx.db, default_trait, &[]));
 
             let missing_fields = ctx.sema.record_literal_missing_fields(record_lit);
             if impl_default_trait && !missing_fields.is_empty() {
                 let completion_text = "..Default::default()";
-                let completion_text = completion_text
-                    .strip_prefix(ctx.token.to_string().as_str())
-                    .unwrap_or(completion_text);
-                acc.add(
-                    CompletionItem::new(
-                        CompletionKind::Snippet,
-                        ctx.source_range(),
-                        "..Default::default()",
-                    )
-                    .insert_text(completion_text)
-                    .kind(SymbolKind::Field)
-                    .build(),
+                let mut item = CompletionItem::new(
+                    CompletionKind::Snippet,
+                    ctx.source_range(),
+                    completion_text,
                 );
+                let completion_text =
+                    completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
+                item.insert_text(completion_text).kind(SymbolKind::Field);
+                item.add_to(acc);
             }
 
             missing_fields
@@ -111,8 +107,6 @@ fn process(f: S) {
         check_snippet(
             test_code,
             expect![[r#"
-                sn pd
-                sn ppd
                 fd ..Default::default()
             "#]],
         );
@@ -182,13 +176,7 @@ fn process(f: S) {
             "#]],
         );
 
-        check_snippet(
-            test_code,
-            expect![[r#"
-                sn pd
-                sn ppd
-            "#]],
-        );
+        check_snippet(test_code, expect![[r#""#]]);
     }
 
     #[test]