aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-02-09 18:04:39 +0000
committerLukas Wirth <[email protected]>2021-02-09 18:04:39 +0000
commit2f171ca78d306d105a9e36f8e509039d6a034c8e (patch)
tree6a2afbb343a591a42a3892c1075f151f41486259
parent12c7b66a7c6963d42ab5f33a9ac3f0b30e351b69 (diff)
Show completions for enum variant with self prefix if inside impl
-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 e2482f959..fb67756bb 100644
--- a/crates/completion/src/completions/unqualified_path.rs
+++ b/crates/completion/src/completions/unqualified_path.rs
@@ -2,7 +2,7 @@
2 2
3use std::iter; 3use std::iter;
4 4
5use hir::{Adt, ModuleDef, ScopeDef, Type}; 5use hir::{known, Adt, ModuleDef, ScopeDef, Type};
6use syntax::AstNode; 6use syntax::AstNode;
7use test_utils::mark; 7use test_utils::mark;
8 8
@@ -59,6 +59,18 @@ fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &T
59 enum_data.module(ctx.db) 59 enum_data.module(ctx.db)
60 }; 60 };
61 61
62 if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) {
63 if impl_.target_ty(ctx.db) == *ty {
64 for &variant in &variants {
65 let self_path = hir::ModPath::from_segments(
66 hir::PathKind::Plain,
67 iter::once(known::SELF_TYPE).chain(iter::once(variant.name(ctx.db))),
68 );
69 acc.add_qualified_enum_variant(ctx, variant, self_path.clone());
70 }
71 }
72 }
73
62 for variant in variants { 74 for variant in variants {
63 if let Some(path) = module.find_use_path(ctx.db, ModuleDef::from(variant)) { 75 if let Some(path) = module.find_use_path(ctx.db, ModuleDef::from(variant)) {
64 // Variants with trivial paths are already added by the existing completion logic, 76 // Variants with trivial paths are already added by the existing completion logic,
@@ -729,6 +741,28 @@ fn f() -> m::E { V$0 }
729 } 741 }
730 742
731 #[test] 743 #[test]
744 fn completes_enum_variant_impl() {
745 check(
746 r#"
747enum Foo { Bar, Baz, Quux }
748impl Foo {
749 fn foo() { let foo: Foo = Q$0 }
750}
751"#,
752 expect![[r#"
753 ev Self::Bar ()
754 ev Self::Baz ()
755 ev Self::Quux ()
756 ev Foo::Bar ()
757 ev Foo::Baz ()
758 ev Foo::Quux ()
759 sp Self
760 en Foo
761 "#]],
762 )
763 }
764
765 #[test]
732 fn dont_complete_attr() { 766 fn dont_complete_attr() {
733 check( 767 check(
734 r#" 768 r#"