Skip to content

Commit d163f9f

Browse files
Small refactoring
1 parent 8aa740d commit d163f9f

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

crates/base_db/src/lib.rs

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

169169
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
170-
fn possible_sudmobules_opt(
171-
module_files: &FileSet,
172-
module_file: FileId,
173-
) -> Option<Vec<FileId>> {
174-
match module_files.file_name_and_extension(module_file)? {
175-
("mod", Some("rs")) | ("lib", Some("rs")) => {
176-
module_files.list_files(module_file, None)
177-
}
178-
(directory_with_module_name, Some("rs")) => module_files
179-
.list_files(module_file, Some(&format!("../{}/", directory_with_module_name))),
180-
_ => None,
181-
}
182-
}
183-
184170
let module_files = &self.source_root(module_file).file_set;
185-
possible_sudmobules_opt(module_files, module_file)
186-
.unwrap_or_default()
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+
Some((directory_with_module_name, Some("rs"))) => module_files
176+
.list_files_with_extensions(
177+
module_file,
178+
Some(&format!("../{}/", directory_with_module_name)),
179+
),
180+
_ => Vec::new(),
181+
};
182+
183+
possible_submodule_files
187184
.into_iter()
188-
.filter_map(|submodule_file| module_files.file_name_and_extension(submodule_file))
189-
.map(|(file_name, extension)| match extension {
190-
Some(extension) => format!("{}.{}", file_name, extension),
191-
None => file_name.to_owned(),
192-
})
185+
.filter(|(_, extension)| extension == &Some("rs"))
186+
.map(|(file_name, _)| file_name.to_owned())
193187
.collect()
194188
}
195189
}

crates/vfs/src/file_set.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,33 @@ impl FileSet {
3030
self.paths[&file].file_name_and_extension()
3131
}
3232

33-
pub fn list_files(
33+
pub fn list_files_with_extensions(
3434
&self,
3535
anchor: FileId,
3636
anchor_relative_path: Option<&str>,
37-
) -> Option<Vec<FileId>> {
37+
) -> Vec<(&str, Option<&str>)> {
3838
let anchor_directory = {
3939
let path = self.paths[&anchor].clone();
4040
match anchor_relative_path {
4141
Some(anchor_relative_path) => path.join(anchor_relative_path),
4242
None => path.parent(),
4343
}
44-
}?;
44+
};
4545

46-
Some(
46+
if let Some(anchor_directory) = anchor_directory {
4747
self.paths
4848
.iter()
49-
.filter_map(|(&file_id, path)| {
50-
if path.parent()? == anchor_directory
51-
&& matches!(path.file_name_and_extension(), Some((_, Some("rs"))))
52-
{
53-
Some(file_id)
49+
.filter_map(|(_, path)| {
50+
if path.parent()? == anchor_directory {
51+
path.file_name_and_extension()
5452
} else {
5553
None
5654
}
5755
})
58-
.collect(),
59-
)
56+
.collect()
57+
} else {
58+
Vec::new()
59+
}
6060
}
6161
pub fn insert(&mut self, file_id: FileId, path: VfsPath) {
6262
self.files.insert(path.clone(), file_id);

0 commit comments

Comments
 (0)