Skip to content

Commit 4d457e2

Browse files
bors[bot]Veykril
andauthored
Merge #9665
9665: Only complete ancestors and self in visibility path completions r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 99451a5 + ccde0bc commit 4d457e2

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

crates/ide_completion/src/completions/qualified_path.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,40 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
2828

2929
let context_module = ctx.scope.module();
3030

31-
if let Some(ImmediateLocation::ItemList | ImmediateLocation::Trait | ImmediateLocation::Impl) =
32-
ctx.completion_location
33-
{
34-
if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
35-
for (name, def) in module.scope(ctx.db, context_module) {
36-
if let hir::ScopeDef::MacroDef(macro_def) = def {
37-
if macro_def.is_fn_like() {
38-
acc.add_macro(ctx, Some(name.clone()), macro_def);
31+
match ctx.completion_location {
32+
Some(ImmediateLocation::ItemList | ImmediateLocation::Trait | ImmediateLocation::Impl) => {
33+
if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
34+
for (name, def) in module.scope(ctx.db, context_module) {
35+
if let hir::ScopeDef::MacroDef(macro_def) = def {
36+
if macro_def.is_fn_like() {
37+
acc.add_macro(ctx, Some(name.clone()), macro_def);
38+
}
39+
}
40+
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def {
41+
acc.add_resolution(ctx, name, &def);
3942
}
4043
}
41-
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def {
42-
acc.add_resolution(ctx, name, &def);
44+
}
45+
return;
46+
}
47+
Some(ImmediateLocation::Visibility(_)) => {
48+
if let hir::PathResolution::Def(hir::ModuleDef::Module(resolved)) = resolution {
49+
if let Some(current_module) = ctx.scope.module() {
50+
if let Some(next) = current_module
51+
.path_to_root(ctx.db)
52+
.into_iter()
53+
.take_while(|&it| it != resolved)
54+
.next()
55+
{
56+
if let Some(name) = next.name(ctx.db) {
57+
acc.add_resolution(ctx, name, &hir::ScopeDef::ModuleDef(next.into()));
58+
}
59+
}
4360
}
4461
}
62+
return;
4563
}
46-
return;
64+
_ => (),
4765
}
4866

4967
if ctx.in_use_tree() {

crates/ide_completion/src/tests/visibility.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ pub(in $0)
4040

4141
#[test]
4242
fn qualified() {
43-
// FIXME: only show parent modules
4443
check(
4544
r#"
4645
mod foo {
@@ -50,7 +49,21 @@ mod foo {
5049
mod bar {}
5150
"#,
5251
expect![[r#"
53-
md bar
52+
md foo
53+
"#]],
54+
);
55+
check(
56+
r#"
57+
mod qux {
58+
mod foo {
59+
pub(in crate::qux::$0)
60+
}
61+
mod baz {}
62+
}
63+
64+
mod bar {}
65+
"#,
66+
expect![[r#"
5467
md foo
5568
"#]],
5669
);

0 commit comments

Comments
 (0)