about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crates/completion/src/completions/unqualified_path.rs36
1 files changed, 35 insertions, 1 deletions
diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs
index e2482f9592d..fb67756bbfc 100644
--- a/crates/completion/src/completions/unqualified_path.rs
+++ b/crates/completion/src/completions/unqualified_path.rs
@@ -2,7 +2,7 @@
 
 use std::iter;
 
-use hir::{Adt, ModuleDef, ScopeDef, Type};
+use hir::{known, Adt, ModuleDef, ScopeDef, Type};
 use syntax::AstNode;
 use test_utils::mark;
 
@@ -59,6 +59,18 @@ fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &T
             enum_data.module(ctx.db)
         };
 
+        if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) {
+            if impl_.target_ty(ctx.db) == *ty {
+                for &variant in &variants {
+                    let self_path = hir::ModPath::from_segments(
+                        hir::PathKind::Plain,
+                        iter::once(known::SELF_TYPE).chain(iter::once(variant.name(ctx.db))),
+                    );
+                    acc.add_qualified_enum_variant(ctx, variant, self_path.clone());
+                }
+            }
+        }
+
         for variant in variants {
             if let Some(path) = module.find_use_path(ctx.db, ModuleDef::from(variant)) {
                 // Variants with trivial paths are already added by the existing completion logic,
@@ -729,6 +741,28 @@ fn f() -> m::E { V$0 }
     }
 
     #[test]
+    fn completes_enum_variant_impl() {
+        check(
+            r#"
+enum Foo { Bar, Baz, Quux }
+impl Foo {
+    fn foo() { let foo: Foo = Q$0 }
+}
+"#,
+            expect![[r#"
+                ev Self::Bar  ()
+                ev Self::Baz  ()
+                ev Self::Quux ()
+                ev Foo::Bar   ()
+                ev Foo::Baz   ()
+                ev Foo::Quux  ()
+                sp Self
+                en Foo
+            "#]],
+        )
+    }
+
+    #[test]
     fn dont_complete_attr() {
         check(
             r#"