Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only complete traits in impl .. for #16544

Merged
merged 4 commits into from Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/ide-completion/src/completions/flyimport.rs
Expand Up @@ -238,6 +238,8 @@ fn import_on_the_fly(
(PathKind::Type { location }, ItemInNs::Types(ty)) => {
if matches!(location, TypeLocation::TypeBound) {
matches!(ty, ModuleDef::Trait(_))
} else if matches!(location, TypeLocation::ImplTrait) {
matches!(ty, ModuleDef::Trait(_)) || matches!(ty, ModuleDef::Module(_))
Veykril marked this conversation as resolved.
Show resolved Hide resolved
} else {
true
}
Expand Down
20 changes: 20 additions & 0 deletions crates/ide-completion/src/completions/type.rs
Expand Up @@ -31,6 +31,11 @@ pub(crate) fn complete_type_path(
ScopeDef::ImplSelfType(_) => location.complete_self_type(),
// Don't suggest attribute macros and derives.
ScopeDef::ModuleDef(Macro(mac)) => mac.is_fn_like(ctx.db),
ScopeDef::ModuleDef(Trait(_) | Module(_))
if matches!(location, TypeLocation::ImplTrait) =>
{
true
}
dfireBird marked this conversation as resolved.
Show resolved Hide resolved
// Type things are fine
ScopeDef::ModuleDef(
BuiltinType(_) | Adt(_) | Module(_) | Trait(_) | TraitAlias(_) | TypeAlias(_),
Expand Down Expand Up @@ -184,6 +189,21 @@ pub(crate) fn complete_type_path(
}
}
}
TypeLocation::ImplTrait => {
acc.add_nameref_keywords_with_colon(ctx);
ctx.process_all_names(&mut |name, def, doc_aliases| {
let is_trait_or_module = matches!(
def,
ScopeDef::ModuleDef(
hir::ModuleDef::Module(_) | hir::ModuleDef::Trait(_)
)
);
if is_trait_or_module {
acc.add_path_resolution(ctx, path_ctx, name, def, doc_aliases);
}
});
return;
}
_ => {}
};

Expand Down
1 change: 1 addition & 0 deletions crates/ide-completion/src/context.rs
Expand Up @@ -202,6 +202,7 @@ impl TypeLocation {
}
TypeLocation::AssocConstEq => false,
TypeLocation::AssocTypeEq => true,
TypeLocation::ImplTrait => false,
_ => true,
}
}
Expand Down
19 changes: 19 additions & 0 deletions crates/ide-completion/src/tests/flyimport.rs
Expand Up @@ -1397,3 +1397,22 @@ pub use bridge2::server2::Span2;
"#]],
);
}

#[test]
fn flyimport_only_traits_in_impl_trait_block() {
check(
r#"
//- /main.rs crate:main deps:dep
pub struct Bar;

impl Foo$0 for Bar { }
//- /lib.rs crate:dep
pub trait FooTrait;

pub struct FooStruct;
"#,
expect![[r#"
tt FooTrait (use dep::FooTrait)
"#]],
);
}
40 changes: 40 additions & 0 deletions crates/ide-completion/src/tests/type_pos.rs
Expand Up @@ -989,3 +989,43 @@ fn foo<'a>() { S::<'static, F$0, _, _>; }
"#]],
);
}

#[test]
fn complete_traits_on_impl_trait_block() {
check(
r#"
trait Foo {}

struct Bar;

impl $0 for Bar { }
"#,
expect![[r#"
md module
tt Foo
tt Trait
kw crate::
kw self::
"#]],
);
}

#[test]
fn complete_traits_with_path_on_impl_trait_block() {
check(
r#"
mod outer {
pub trait Foo {}
pub struct Bar;
pub mod inner {
}
}

impl outer::$0 for Bar { }
"#,
expect![[r#"
md inner
tt Foo
"#]],
);
}