]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_completion/src/completions/record.rs
Merge #9845
[rust.git] / crates / ide_completion / src / completions / record.rs
index c7cf5e3e4b14ca82c4d3af9da86774d504cd3bbf..acce10addf72c38de9b64846d56f2f7251372858 100644 (file)
@@ -12,9 +12,9 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
         Some(ImmediateLocation::RecordExpr(record_expr)) => {
             let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_expr.clone()));
             let default_trait = FamousDefs(&ctx.sema, ctx.krate).core_default_Default();
-            let impl_default_trait = default_trait
-                .zip(ty)
-                .map_or(false, |(default_trait, ty)| ty.impls_trait(ctx.db, default_trait, &[]));
+            let impl_default_trait = default_trait.zip(ty).map_or(false, |(default_trait, ty)| {
+                ty.original.impls_trait(ctx.db, default_trait, &[])
+            });
 
             let missing_fields = ctx.sema.record_literal_missing_fields(record_expr);
             if impl_default_trait && !missing_fields.is_empty() {
@@ -45,11 +45,81 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
     Some(())
 }
 
+pub(crate) fn complete_record_literal(
+    acc: &mut Completions,
+    ctx: &CompletionContext,
+) -> Option<()> {
+    if !ctx.expects_expression() {
+        return None;
+    }
+
+    if let hir::Adt::Struct(strukt) = ctx.expected_type.as_ref()?.as_adt()? {
+        acc.add_struct_literal(ctx, strukt, None);
+    }
+
+    Some(())
+}
+
 #[cfg(test)]
 mod tests {
-
     use crate::tests::check_edit;
 
+    #[test]
+    fn literal_struct_completion_edit() {
+        check_edit(
+            "FooDesc {…}",
+            r#"
+struct FooDesc { pub bar: bool }
+
+fn create_foo(foo_desc: &FooDesc) -> () { () }
+
+fn baz() {
+    let foo = create_foo(&$0);
+}
+            "#,
+            r#"
+struct FooDesc { pub bar: bool }
+
+fn create_foo(foo_desc: &FooDesc) -> () { () }
+
+fn baz() {
+    let foo = create_foo(&FooDesc { bar: ${1:()} }$0);
+}
+            "#,
+        )
+    }
+
+    #[test]
+    fn literal_struct_complexion_module() {
+        check_edit(
+            "FooDesc {…}",
+            r#"
+mod _69latrick {
+    pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
+    pub fn create_foo(foo_desc: &FooDesc) -> () { () }
+}
+
+fn baz() {
+    use _69latrick::*;
+
+    let foo = create_foo(&$0);
+}
+            "#,
+            r#"
+mod _69latrick {
+    pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
+    pub fn create_foo(foo_desc: &FooDesc) -> () { () }
+}
+
+fn baz() {
+    use _69latrick::*;
+
+    let foo = create_foo(&FooDesc { six: ${1:()}, neuf: ${2:()}, bar: ${3:()} }$0);
+}
+            "#,
+        );
+    }
+
     #[test]
     fn default_completion_edit() {
         check_edit(
@@ -108,6 +178,38 @@ impl Default for Struct {
     fn default() -> Self {}
 }
 
+fn foo() {
+    let other = Struct {
+        foo: 5,
+        ..Default::default()
+    };
+}
+"#,
+        );
+        check_edit(
+            "..Default::default()",
+            r#"
+//- minicore: default
+struct Struct { foo: u32, bar: usize }
+
+impl Default for Struct {
+    fn default() -> Self {}
+}
+
+fn foo() {
+    let other = Struct {
+        foo: 5,
+        ..$0
+    };
+}
+"#,
+            r#"
+struct Struct { foo: u32, bar: usize }
+
+impl Default for Struct {
+    fn default() -> Self {}
+}
+
 fn foo() {
     let other = Struct {
         foo: 5,