Skip to content

Commit b2bcc52

Browse files
Properly handle special cases (binaries, mod.rs)
1 parent 486c5c3 commit b2bcc52

File tree

3 files changed

+60
-52
lines changed

3 files changed

+60
-52
lines changed

crates/base_db/src/lib.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -167,29 +167,7 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
167167
}
168168

169169
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
170-
let module_files = &self.source_root(module_file).file_set;
171-
let possible_submodule_files = match module_files.file_name_and_extension(module_file) {
172-
Some(("mod", Some("rs"))) | Some(("lib", Some("rs"))) => {
173-
module_files.list_files_with_extensions(module_file, None)
174-
}
175-
// TODO kb for `src/bin/foo.rs`, we need to check for modules in `src/bin/`
176-
Some((directory_with_module_name, Some("rs"))) => module_files
177-
.list_files_with_extensions(
178-
module_file,
179-
Some(&format!("../{}/", directory_with_module_name)),
180-
),
181-
// TODO kb also consider the case when there's no `../module_name.rs`, but `../module_name/mod.rs`
182-
_ => Vec::new(),
183-
};
184-
185-
possible_submodule_files
186-
.into_iter()
187-
.filter(|(_, extension)| extension == &Some("rs"))
188-
.filter(|(file_name, _)| file_name != &"mod")
189-
.filter(|(file_name, _)| file_name != &"lib")
190-
.filter(|(file_name, _)| file_name != &"main")
191-
.map(|(file_name, _)| file_name.to_owned())
192-
.collect()
170+
self.source_root(module_file).file_set.possible_sudmobule_names(module_file)
193171
}
194172
}
195173

crates/ide/src/completion/completion_context.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! FIXME: write short doc here
22
33
use base_db::{FileLoader, SourceDatabase};
4-
use hir::{ModuleSource, Semantics, SemanticsScope, Type};
4+
use hir::{Semantics, SemanticsScope, Type};
55
use ide_db::RootDatabase;
66
use syntax::{
77
algo::{find_covering_element, find_node_at_offset},
@@ -123,11 +123,9 @@ impl<'a> CompletionContext<'a> {
123123
dbg!(mod_declaration_candidates);
124124
// TODO kb exlude existing children from the candidates
125125
let existing_children = current_module.children(db).collect::<Vec<_>>();
126-
dbg!(existing_children);
127126
None::<Vec<String>>
128127
})
129128
.unwrap_or_default();
130-
dbg!(module_names_for_import);
131129
};
132130

133131
let krate = sema.to_module_def(position.file_id).map(|m| m.krate());

crates/vfs/src/file_set.rs

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,70 @@ impl FileSet {
2626
self.files.get(&path).copied()
2727
}
2828

29-
pub fn file_name_and_extension(&self, file: FileId) -> Option<(&str, Option<&str>)> {
30-
self.paths[&file].file_name_and_extension()
31-
}
32-
33-
pub fn list_files_with_extensions(
34-
&self,
35-
anchor: FileId,
36-
anchor_relative_path: Option<&str>,
37-
) -> Vec<(&str, Option<&str>)> {
38-
let anchor_directory = {
39-
let path = self.paths[&anchor].clone();
40-
match anchor_relative_path {
41-
Some(anchor_relative_path) => path.join(anchor_relative_path),
42-
None => path.parent(),
43-
}
29+
pub fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
30+
let directory_to_look_for_submodules = match self.get_directory_with_submodules(module_file)
31+
{
32+
Some(directory) => directory,
33+
None => return Vec::new(),
4434
};
45-
46-
if let Some(anchor_directory) = anchor_directory {
47-
self.paths
48-
.iter()
49-
.filter_map(|(_, path)| {
50-
if path.parent()? == anchor_directory {
51-
path.file_name_and_extension()
35+
self.paths
36+
.iter()
37+
.filter_map(|(_, path)| {
38+
if path.parent()? == directory_to_look_for_submodules {
39+
path.file_name_and_extension()
40+
} else {
41+
None
42+
}
43+
})
44+
.filter_map(|file_name_and_extension| match file_name_and_extension {
45+
// TODO kb do not include the module file name itself, if present
46+
// TODO kb wrong resolution for nesСпаted non-file modules (mod tests {mod <|>)
47+
// TODO kb in src/bin when a module is included into another,
48+
// the included file gets "moved" into a directory below and now cannot add any other modules
49+
("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => None,
50+
(file_name, Some("rs")) => Some(file_name.to_owned()),
51+
(subdirectory_name, None) => {
52+
let mod_rs_path =
53+
directory_to_look_for_submodules.join(subdirectory_name)?.join("mod.rs")?;
54+
if self.files.contains_key(&mod_rs_path) {
55+
Some(subdirectory_name.to_owned())
5256
} else {
5357
None
5458
}
55-
})
56-
.collect()
57-
} else {
58-
Vec::new()
59+
}
60+
_ => None,
61+
})
62+
.collect()
63+
}
64+
65+
fn get_directory_with_submodules(&self, module_file: FileId) -> Option<VfsPath> {
66+
let module_file_path = &self.paths[&module_file];
67+
let module_directory_path = module_file_path.parent()?;
68+
match module_file_path.file_name_and_extension() {
69+
Some(("mod", Some("rs"))) | Some(("lib", Some("rs"))) | Some(("main", Some("rs"))) => {
70+
Some(module_directory_path)
71+
}
72+
Some((regular_rust_file_name, Some("rs"))) => {
73+
if matches!(
74+
(
75+
module_directory_path
76+
.parent()
77+
.as_ref()
78+
.and_then(|path| path.file_name_and_extension()),
79+
module_directory_path.file_name_and_extension(),
80+
),
81+
(Some(("src", None)), Some(("bin", None)))
82+
) {
83+
// files in /src/bin/ can import each other directly
84+
Some(module_directory_path)
85+
} else {
86+
module_directory_path.join(regular_rust_file_name)
87+
}
88+
}
89+
_ => None,
5990
}
6091
}
92+
6193
pub fn insert(&mut self, file_id: FileId, path: VfsPath) {
6294
self.files.insert(path.clone(), file_id);
6395
self.paths.insert(file_id, path);

0 commit comments

Comments
 (0)