Skip to content

Commit ea251cb

Browse files
committed
Complete modules in item lists
1 parent 7ad378f commit ea251cb

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

crates/ide_completion/src/completions/macro_in_item_position.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use crate::{CompletionContext, Completions};
44

5+
// Ideally this should be removed and moved into `(un)qualified_path` respectively
56
pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
67
// Show only macros in top level.
78
if !ctx.is_new_item {
@@ -12,6 +13,10 @@ pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &Compl
1213
if let hir::ScopeDef::MacroDef(mac) = res {
1314
acc.add_macro(ctx, Some(name.to_string()), mac);
1415
}
16+
// FIXME: This should be done in qualified_path/unqualified_path instead?
17+
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
18+
acc.add_resolution(ctx, name.to_string(), &res);
19+
}
1520
})
1621
}
1722

crates/ide_completion/src/completions/qualified_path.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use syntax::AstNode;
77
use crate::{CompletionContext, Completions};
88

99
pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) {
10-
if ctx.is_path_disallowed() {
10+
if ctx.is_path_disallowed() || ctx.expects_item() {
1111
return;
1212
}
1313
let path = match &ctx.path_qual {
@@ -20,7 +20,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
2020
None => return,
2121
};
2222
let context_module = ctx.scope.module();
23-
if ctx.expects_item() || ctx.expects_assoc_item() {
23+
if ctx.expects_assoc_item() {
2424
if let PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
2525
let module_scope = module.scope(ctx.db, context_module);
2626
for (name, def) in module_scope {
@@ -636,6 +636,24 @@ impl MyStruct {
636636
);
637637
}
638638

639+
#[test]
640+
#[ignore] // FIXME doesn't complete anything atm
641+
fn completes_in_item_list() {
642+
check(
643+
r#"
644+
struct MyStruct {}
645+
macro_rules! foo {}
646+
mod bar {}
647+
648+
crate::$0
649+
"#,
650+
expect![[r#"
651+
md bar
652+
ma foo! macro_rules! foo
653+
"#]],
654+
)
655+
}
656+
639657
#[test]
640658
fn test_super_super_completion() {
641659
check(

crates/ide_completion/src/completions/unqualified_path.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
99
if !ctx.is_trivial_path {
1010
return;
1111
}
12-
if ctx.is_path_disallowed() {
12+
if ctx.is_path_disallowed() || ctx.expects_item() {
1313
return;
1414
}
15-
if ctx.expects_item() || ctx.expects_assoc_item() {
15+
if ctx.expects_assoc_item() {
1616
ctx.scope.process_all_names(&mut |name, def| {
1717
if let ScopeDef::MacroDef(macro_def) = def {
1818
acc.add_macro(ctx, Some(name.to_string()), macro_def);
@@ -692,4 +692,22 @@ impl MyStruct {
692692
"#]],
693693
)
694694
}
695+
696+
// FIXME: The completions here currently come from `macro_in_item_position`, but they shouldn't
697+
#[test]
698+
fn completes_in_item_list() {
699+
check(
700+
r#"
701+
struct MyStruct {}
702+
macro_rules! foo {}
703+
mod bar {}
704+
705+
$0
706+
"#,
707+
expect![[r#"
708+
md bar
709+
ma foo!(…) macro_rules! foo
710+
"#]],
711+
)
712+
}
695713
}

0 commit comments

Comments
 (0)