Skip to content

Commit dbf70cd

Browse files
Properly handle mod.rs imports
1 parent 9fb8321 commit dbf70cd

File tree

1 file changed

+40
-47
lines changed

1 file changed

+40
-47
lines changed

crates/ide/src/completion/complete_mod.rs

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,26 @@ pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
5151
})
5252
.filter_map(|submodule_file| {
5353
let submodule_path = source_root.path_for_file(&submodule_file)?;
54-
if !is_special_rust_file_path(&submodule_path)
55-
&& submodule_path.parent()? == directory_to_look_for_submodules
56-
{
57-
submodule_path.file_name_and_extension()
58-
} else {
59-
None
60-
}
61-
})
62-
.filter_map(|submodule_file_name_and_extension| match submodule_file_name_and_extension {
63-
(file_name, Some("rs")) => Some(file_name.to_owned()),
64-
(subdirectory_name, None) => {
65-
let mod_rs_path =
66-
directory_to_look_for_submodules.join(subdirectory_name)?.join("mod.rs")?;
67-
if source_root.file_for_path(&mod_rs_path).is_some() {
68-
Some(subdirectory_name.to_owned())
69-
} else {
70-
None
54+
let directory_with_submodule = submodule_path.parent()?;
55+
match submodule_path.file_name_and_extension()? {
56+
("lib", Some("rs")) | ("main", Some("rs")) => None,
57+
("mod", Some("rs")) => {
58+
if directory_with_submodule.parent()? == directory_to_look_for_submodules {
59+
match directory_with_submodule.file_name_and_extension()? {
60+
(directory_name, None) => Some(directory_name.to_owned()),
61+
_ => None,
62+
}
63+
} else {
64+
None
65+
}
7166
}
67+
(file_name, Some("rs"))
68+
if directory_with_submodule == directory_to_look_for_submodules =>
69+
{
70+
Some(file_name.to_owned())
71+
}
72+
_ => None,
7273
}
73-
_ => None,
7474
})
7575
.filter(|name| !existing_mod_declarations.contains(name))
7676
.for_each(|submodule_name| {
@@ -87,41 +87,34 @@ pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
8787
Some(())
8888
}
8989

90-
fn is_special_rust_file_path(path: &VfsPath) -> bool {
91-
matches!(
92-
path.file_name_and_extension(),
93-
Some(("mod", Some("rs"))) | Some(("lib", Some("rs"))) | Some(("main", Some("rs")))
94-
)
95-
}
96-
9790
fn directory_to_look_for_submodules(
9891
module: Module,
9992
db: &RootDatabase,
10093
module_file_path: &VfsPath,
10194
) -> Option<VfsPath> {
102-
let module_directory_path = module_file_path.parent()?;
103-
let base_directory = if is_special_rust_file_path(module_file_path) {
104-
Some(module_directory_path)
105-
} else if let (regular_rust_file_name, Some("rs")) =
106-
module_file_path.file_name_and_extension()?
107-
{
108-
if matches!(
109-
(
110-
module_directory_path
111-
.parent()
112-
.as_ref()
113-
.and_then(|path| path.file_name_and_extension()),
114-
module_directory_path.file_name_and_extension(),
115-
),
116-
(Some(("src", None)), Some(("bin", None)))
117-
) {
118-
// files in /src/bin/ can import each other directly
119-
Some(module_directory_path)
120-
} else {
121-
module_directory_path.join(regular_rust_file_name)
95+
let directory_with_module_path = module_file_path.parent()?;
96+
let base_directory = match module_file_path.file_name_and_extension()? {
97+
("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => {
98+
Some(directory_with_module_path)
99+
}
100+
(regular_rust_file_name, Some("rs")) => {
101+
if matches!(
102+
(
103+
directory_with_module_path
104+
.parent()
105+
.as_ref()
106+
.and_then(|path| path.file_name_and_extension()),
107+
directory_with_module_path.file_name_and_extension(),
108+
),
109+
(Some(("src", None)), Some(("bin", None)))
110+
) {
111+
// files in /src/bin/ can import each other directly
112+
Some(directory_with_module_path)
113+
} else {
114+
directory_with_module_path.join(regular_rust_file_name)
115+
}
122116
}
123-
} else {
124-
None
117+
_ => None,
125118
}?;
126119

127120
let mut resulting_path = base_directory;

0 commit comments

Comments
 (0)