about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-12-23 09:48:04 +0000
committerGitHub <noreply@github.com>2020-12-23 09:48:04 +0000
commitee06096b0c40f4eba5ecd3c3e80f7951587d8047 (patch)
tree9977054269fe85f2901b92f795b567eb13221839
parent85a28751611e564eb4cdde28ca953407f551b696 (diff)
parent15a52f69d9522442866307254cc712a308688516 (diff)
downloadrust-ee06096b0c40f4eba5ecd3c3e80f7951587d8047.tar.gz
rust-ee06096b0c40f4eba5ecd3c3e80f7951587d8047.zip
Merge #6960
6960: Show enum variant on Self qualified paths r=matklad a=Veykril

Fixes first part of #6549
Fixes #6550

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
-rw-r--r--crates/completion/src/completions/qualified_path.rs29
-rw-r--r--crates/completion/src/completions/unqualified_path.rs27
2 files changed, 55 insertions, 1 deletions
diff --git a/crates/completion/src/completions/qualified_path.rs b/crates/completion/src/completions/qualified_path.rs
index 1300f00b2f2..882c4dcbce9 100644
--- a/crates/completion/src/completions/qualified_path.rs
+++ b/crates/completion/src/completions/qualified_path.rs
@@ -118,6 +118,12 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
                     _ => return,
                 };
 
+                if let Some(Adt::Enum(e)) = ty.as_adt() {
+                    for variant in e.variants(ctx.db) {
+                        acc.add_enum_variant(ctx, variant, None);
+                    }
+                }
+
                 let traits_in_scope = ctx.scope.traits_in_scope();
                 let mut seen = FxHashSet::default();
                 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
@@ -752,4 +758,27 @@ fn main() {
             "#]],
         );
     }
+
+    #[test]
+    fn completes_self_enum() {
+        check(
+            r#"
+enum Foo {
+    Bar,
+    Baz,
+}
+
+impl Foo {
+    fn foo(self) {
+        Self::<|>
+    }
+}
+"#,
+            expect![[r#"
+                ev Bar    ()
+                ev Baz    ()
+                me foo(…) fn foo(self)
+            "#]],
+        );
+    }
 }
diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs
index 099ffb4d48b..d0984975206 100644
--- a/crates/completion/src/completions/unqualified_path.rs
+++ b/crates/completion/src/completions/unqualified_path.rs
@@ -1,5 +1,7 @@
 //! Completion of names from the current scope, e.g. locals and imported items.
 
+use std::iter;
+
 use either::Either;
 use hir::{Adt, ModPath, ModuleDef, ScopeDef, Type};
 use ide_db::helpers::insert_use::ImportScope;
@@ -50,7 +52,9 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
 }
 
 fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) {
-    if let Some(Adt::Enum(enum_data)) = ty.as_adt() {
+    if let Some(Adt::Enum(enum_data)) =
+        iter::successors(Some(ty.clone()), |ty| ty.remove_ref()).last().and_then(|ty| ty.as_adt())
+    {
         let variants = enum_data.variants(ctx.db);
 
         let module = if let Some(module) = ctx.scope.module() {
@@ -701,6 +705,7 @@ fn main() { <|> }
             "#]],
         );
     }
+
     #[test]
     fn completes_enum_variant_matcharm() {
         check(
@@ -722,6 +727,26 @@ fn main() {
     }
 
     #[test]
+    fn completes_enum_variant_matcharm_ref() {
+        check(
+            r#"
+enum Foo { Bar, Baz, Quux }
+
+fn main() {
+    let foo = Foo::Quux;
+    match &foo { Qu<|> }
+}
+"#,
+            expect![[r#"
+                ev Foo::Bar  ()
+                ev Foo::Baz  ()
+                ev Foo::Quux ()
+                en Foo
+            "#]],
+        )
+    }
+
+    #[test]
     fn completes_enum_variant_iflet() {
         check(
             r#"