aboutsummaryrefslogtreecommitdiff
path: root/crates/completion
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2020-12-20 11:32:53 +0000
committerLukas Wirth <[email protected]>2020-12-20 11:32:53 +0000
commit15a52f69d9522442866307254cc712a308688516 (patch)
tree3838149a6c5680bd6520a3258febb9d75c5c55cf /crates/completion
parent943e4faceb4491b83cf05cfceedbdd5f469b9270 (diff)
Show enum variant completions for ref scrutinee
Diffstat (limited to 'crates/completion')
-rw-r--r--crates/completion/src/completions/unqualified_path.rs27
1 files changed, 26 insertions, 1 deletions
diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs
index 099ffb4d4..d09849752 100644
--- a/crates/completion/src/completions/unqualified_path.rs
+++ b/crates/completion/src/completions/unqualified_path.rs
@@ -1,5 +1,7 @@
1//! Completion of names from the current scope, e.g. locals and imported items. 1//! Completion of names from the current scope, e.g. locals and imported items.
2 2
3use std::iter;
4
3use either::Either; 5use either::Either;
4use hir::{Adt, ModPath, ModuleDef, ScopeDef, Type}; 6use hir::{Adt, ModPath, ModuleDef, ScopeDef, Type};
5use ide_db::helpers::insert_use::ImportScope; 7use ide_db::helpers::insert_use::ImportScope;
@@ -50,7 +52,9 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
50} 52}
51 53
52fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) { 54fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) {
53 if let Some(Adt::Enum(enum_data)) = ty.as_adt() { 55 if let Some(Adt::Enum(enum_data)) =
56 iter::successors(Some(ty.clone()), |ty| ty.remove_ref()).last().and_then(|ty| ty.as_adt())
57 {
54 let variants = enum_data.variants(ctx.db); 58 let variants = enum_data.variants(ctx.db);
55 59
56 let module = if let Some(module) = ctx.scope.module() { 60 let module = if let Some(module) = ctx.scope.module() {
@@ -701,6 +705,7 @@ fn main() { <|> }
701 "#]], 705 "#]],
702 ); 706 );
703 } 707 }
708
704 #[test] 709 #[test]
705 fn completes_enum_variant_matcharm() { 710 fn completes_enum_variant_matcharm() {
706 check( 711 check(
@@ -722,6 +727,26 @@ fn main() {
722 } 727 }
723 728
724 #[test] 729 #[test]
730 fn completes_enum_variant_matcharm_ref() {
731 check(
732 r#"
733enum Foo { Bar, Baz, Quux }
734
735fn main() {
736 let foo = Foo::Quux;
737 match &foo { Qu<|> }
738}
739"#,
740 expect![[r#"
741 ev Foo::Bar ()
742 ev Foo::Baz ()
743 ev Foo::Quux ()
744 en Foo
745 "#]],
746 )
747 }
748
749 #[test]
725 fn completes_enum_variant_iflet() { 750 fn completes_enum_variant_iflet() {
726 check( 751 check(
727 r#" 752 r#"