Small refactoring

This commit is contained in:
Kirill Bulatov 2020-09-04 13:33:07 +03:00
parent 8aa740dab4
commit d163f9f114
2 changed files with 26 additions and 32 deletions

View File

@ -167,29 +167,23 @@ fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
}
fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
fn possible_sudmobules_opt(
module_files: &FileSet,
module_file: FileId,
) -> Option<Vec<FileId>> {
match module_files.file_name_and_extension(module_file)? {
("mod", Some("rs")) | ("lib", Some("rs")) => {
module_files.list_files(module_file, None)
}
(directory_with_module_name, Some("rs")) => module_files
.list_files(module_file, Some(&format!("../{}/", directory_with_module_name))),
_ => None,
}
}
let module_files = &self.source_root(module_file).file_set;
possible_sudmobules_opt(module_files, module_file)
.unwrap_or_default()
let possible_submodule_files = match module_files.file_name_and_extension(module_file) {
Some(("mod", Some("rs"))) | Some(("lib", Some("rs"))) => {
module_files.list_files_with_extensions(module_file, None)
}
Some((directory_with_module_name, Some("rs"))) => module_files
.list_files_with_extensions(
module_file,
Some(&format!("../{}/", directory_with_module_name)),
),
_ => Vec::new(),
};
possible_submodule_files
.into_iter()
.filter_map(|submodule_file| module_files.file_name_and_extension(submodule_file))
.map(|(file_name, extension)| match extension {
Some(extension) => format!("{}.{}", file_name, extension),
None => file_name.to_owned(),
})
.filter(|(_, extension)| extension == &Some("rs"))
.map(|(file_name, _)| file_name.to_owned())
.collect()
}
}

View File

@ -30,33 +30,33 @@ pub fn file_name_and_extension(&self, file: FileId) -> Option<(&str, Option<&str
self.paths[&file].file_name_and_extension()
}
pub fn list_files(
pub fn list_files_with_extensions(
&self,
anchor: FileId,
anchor_relative_path: Option<&str>,
) -> Option<Vec<FileId>> {
) -> Vec<(&str, Option<&str>)> {
let anchor_directory = {
let path = self.paths[&anchor].clone();
match anchor_relative_path {
Some(anchor_relative_path) => path.join(anchor_relative_path),
None => path.parent(),
}
}?;
};
Some(
if let Some(anchor_directory) = anchor_directory {
self.paths
.iter()
.filter_map(|(&file_id, path)| {
if path.parent()? == anchor_directory
&& matches!(path.file_name_and_extension(), Some((_, Some("rs"))))
{
Some(file_id)
.filter_map(|(_, path)| {
if path.parent()? == anchor_directory {
path.file_name_and_extension()
} else {
None
}
})
.collect(),
)
.collect()
} else {
Vec::new()
}
}
pub fn insert(&mut self, file_id: FileId, path: VfsPath) {
self.files.insert(path.clone(), file_id);