]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_completion/src/completions/record.rs
fix: Do not show functional update completion when already qualified
[rust.git] / crates / ide_completion / src / completions / record.rs
index c9c09551f9c2f2c4e9effd2d9f240a28d9c942d1..80132c2566aea4f4fa460f112a7e431fef06a03a 100644 (file)
@@ -1,15 +1,18 @@
 //! Complete fields in record literals and patterns.
 use ide_db::{helpers::FamousDefs, SymbolKind};
-use syntax::ast::Expr;
+use syntax::{ast::Expr, T};
 
 use crate::{
     item::CompletionKind, patterns::ImmediateLocation, CompletionContext, CompletionItem,
-    Completions,
+    CompletionItemKind, Completions,
 };
 
 pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
     let missing_fields = match &ctx.completion_location {
-        Some(ImmediateLocation::RecordExpr(record_expr)) => {
+        Some(
+            ImmediateLocation::RecordExpr(record_expr)
+            | ImmediateLocation::RecordExprUpdate(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)| {
@@ -17,7 +20,7 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
             });
 
             let missing_fields = ctx.sema.record_literal_missing_fields(record_expr);
-            if impl_default_trait && !missing_fields.is_empty() {
+            if impl_default_trait && !missing_fields.is_empty() && ctx.path_qual().is_none() {
                 let completion_text = "..Default::default()";
                 let mut item = CompletionItem::new(
                     CompletionKind::Snippet,
@@ -29,7 +32,13 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
                 item.insert_text(completion_text).kind(SymbolKind::Field);
                 item.add_to(acc);
             }
-
+            if ctx.previous_token_is(T![.]) {
+                let mut item =
+                    CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), "..");
+                item.insert_text(".").kind(CompletionItemKind::Snippet);
+                item.add_to(acc);
+                return None;
+            }
             missing_fields
         }
         Some(ImmediateLocation::RecordPat(record_pat)) => {
@@ -178,6 +187,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,