Skip to content

Commit ee06096

Browse files
bors[bot]Veykril
andauthored
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 <[email protected]>
2 parents 85a2875 + 15a52f6 commit ee06096

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

crates/completion/src/completions/qualified_path.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
118118
_ => return,
119119
};
120120

121+
if let Some(Adt::Enum(e)) = ty.as_adt() {
122+
for variant in e.variants(ctx.db) {
123+
acc.add_enum_variant(ctx, variant, None);
124+
}
125+
}
126+
121127
let traits_in_scope = ctx.scope.traits_in_scope();
122128
let mut seen = FxHashSet::default();
123129
ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
@@ -752,4 +758,27 @@ fn main() {
752758
"#]],
753759
);
754760
}
761+
762+
#[test]
763+
fn completes_self_enum() {
764+
check(
765+
r#"
766+
enum Foo {
767+
Bar,
768+
Baz,
769+
}
770+
771+
impl Foo {
772+
fn foo(self) {
773+
Self::<|>
774+
}
775+
}
776+
"#,
777+
expect![[r#"
778+
ev Bar ()
779+
ev Baz ()
780+
me foo(…) fn foo(self)
781+
"#]],
782+
);
783+
}
755784
}

crates/completion/src/completions/unqualified_path.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Completion of names from the current scope, e.g. locals and imported items.
22
3+
use std::iter;
4+
35
use either::Either;
46
use hir::{Adt, ModPath, ModuleDef, ScopeDef, Type};
57
use ide_db::helpers::insert_use::ImportScope;
@@ -50,7 +52,9 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
5052
}
5153

5254
fn 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+
{
5458
let variants = enum_data.variants(ctx.db);
5559

5660
let module = if let Some(module) = ctx.scope.module() {
@@ -701,6 +705,7 @@ fn main() { <|> }
701705
"#]],
702706
);
703707
}
708+
704709
#[test]
705710
fn completes_enum_variant_matcharm() {
706711
check(
@@ -721,6 +726,26 @@ fn main() {
721726
)
722727
}
723728

729+
#[test]
730+
fn completes_enum_variant_matcharm_ref() {
731+
check(
732+
r#"
733+
enum Foo { Bar, Baz, Quux }
734+
735+
fn 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+
724749
#[test]
725750
fn completes_enum_variant_iflet() {
726751
check(

0 commit comments

Comments
 (0)