Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions crates/completion/src/completions/qualified_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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)
"#]],
);
}
}
27 changes: 26 additions & 1 deletion crates/completion/src/completions/unqualified_path.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -701,6 +705,7 @@ fn main() { <|> }
"#]],
);
}

#[test]
fn completes_enum_variant_matcharm() {
check(
Expand All @@ -721,6 +726,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 ()
Copy link
Member

@lnicola lnicola Dec 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we really suggest Foo::Bar and Foo::Baz after Qu?

I have this nagging feeling that we're including way too much in the autocompletion results.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we make it the clients job to filter out the completions? I'm not sure but most(if not all) completions ignore the already typed parts from what I've seen.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, we should send all completion variants.

Well, actually, now that we want to implement sorting ourselves, I think we should implement sorting ourselves (as a generic filter higher in the stack, not in each specific completion contributor), if LSP allows for that.

ev Foo::Baz ()
ev Foo::Quux ()
en Foo
"#]],
)
}

#[test]
fn completes_enum_variant_iflet() {
check(
Expand Down