aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-09-04 11:33:07 +0100
committerKirill Bulatov <[email protected]>2020-09-09 23:42:20 +0100
commitd163f9f114c8180bec6285ad9962fabbf3af5b18 (patch)
treeb88940186b9539465a70ea20503cf969b645cc04 /crates
parent8aa740dab46f138cacdf6391d46c87d6df810161 (diff)
Small refactoring
Diffstat (limited to 'crates')
-rw-r--r--crates/base_db/src/lib.rs36
-rw-r--r--crates/vfs/src/file_set.rs22
2 files changed, 26 insertions, 32 deletions
diff --git a/crates/base_db/src/lib.rs b/crates/base_db/src/lib.rs
index 3e0b6637d..55ef9fc24 100644
--- a/crates/base_db/src/lib.rs
+++ b/crates/base_db/src/lib.rs
@@ -167,29 +167,23 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
167 } 167 }
168 168
169 fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> { 169 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
184 let module_files = &self.source_root(module_file).file_set; 170 let module_files = &self.source_root(module_file).file_set;
185 possible_sudmobules_opt(module_files, module_file) 171 let possible_submodule_files = match module_files.file_name_and_extension(module_file) {
186 .unwrap_or_default() 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
187 .into_iter() 184 .into_iter()
188 .filter_map(|submodule_file| module_files.file_name_and_extension(submodule_file)) 185 .filter(|(_, extension)| extension == &Some("rs"))
189 .map(|(file_name, extension)| match extension { 186 .map(|(file_name, _)| file_name.to_owned())
190 Some(extension) => format!("{}.{}", file_name, extension),
191 None => file_name.to_owned(),
192 })
193 .collect() 187 .collect()
194 } 188 }
195} 189}
diff --git a/crates/vfs/src/file_set.rs b/crates/vfs/src/file_set.rs
index 956cffb29..3085fd818 100644
--- a/crates/vfs/src/file_set.rs
+++ b/crates/vfs/src/file_set.rs
@@ -30,33 +30,33 @@ impl FileSet {
30 self.paths[&file].file_name_and_extension() 30 self.paths[&file].file_name_and_extension()
31 } 31 }
32 32
33 pub fn list_files( 33 pub fn list_files_with_extensions(
34 &self, 34 &self,
35 anchor: FileId, 35 anchor: FileId,
36 anchor_relative_path: Option<&str>, 36 anchor_relative_path: Option<&str>,
37 ) -> Option<Vec<FileId>> { 37 ) -> Vec<(&str, Option<&str>)> {
38 let anchor_directory = { 38 let anchor_directory = {
39 let path = self.paths[&anchor].clone(); 39 let path = self.paths[&anchor].clone();
40 match anchor_relative_path { 40 match anchor_relative_path {
41 Some(anchor_relative_path) => path.join(anchor_relative_path), 41 Some(anchor_relative_path) => path.join(anchor_relative_path),
42 None => path.parent(), 42 None => path.parent(),
43 } 43 }
44 }?; 44 };
45 45
46 Some( 46 if let Some(anchor_directory) = anchor_directory {
47 self.paths 47 self.paths
48 .iter() 48 .iter()
49 .filter_map(|(&file_id, path)| { 49 .filter_map(|(_, path)| {
50 if path.parent()? == anchor_directory 50 if path.parent()? == anchor_directory {
51 && matches!(path.file_name_and_extension(), Some((_, Some("rs")))) 51 path.file_name_and_extension()
52 {
53 Some(file_id)
54 } else { 52 } else {
55 None 53 None
56 } 54 }
57 }) 55 })
58 .collect(), 56 .collect()
59 ) 57 } else {
58 Vec::new()
59 }
60 } 60 }
61 pub fn insert(&mut self, file_id: FileId, path: VfsPath) { 61 pub fn insert(&mut self, file_id: FileId, path: VfsPath) {
62 self.files.insert(path.clone(), file_id); 62 self.files.insert(path.clone(), file_id);