aboutsummaryrefslogtreecommitdiff
path: root/crates/base_db
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-09-07 18:52:37 +0100
committerKirill Bulatov <[email protected]>2020-09-09 23:42:20 +0100
commitf9c14ac7204c38633e70b3efd47a5b1f9056afd0 (patch)
tree60d375f39f4737630e9a8166975c71f3ec5f2f9f /crates/base_db
parent6ba479cd058aa54a9f161085c7ff9ac1f12d8df3 (diff)
Move most of the logic into the completion module
Diffstat (limited to 'crates/base_db')
-rw-r--r--crates/base_db/src/input.rs8
-rw-r--r--crates/base_db/src/lib.rs84
2 files changed, 9 insertions, 83 deletions
diff --git a/crates/base_db/src/input.rs b/crates/base_db/src/input.rs
index f3d65cdf0..9a61f1d56 100644
--- a/crates/base_db/src/input.rs
+++ b/crates/base_db/src/input.rs
@@ -12,7 +12,7 @@ use cfg::CfgOptions;
12use rustc_hash::{FxHashMap, FxHashSet}; 12use rustc_hash::{FxHashMap, FxHashSet};
13use syntax::SmolStr; 13use syntax::SmolStr;
14use tt::TokenExpander; 14use tt::TokenExpander;
15use vfs::file_set::FileSet; 15use vfs::{file_set::FileSet, VfsPath};
16 16
17pub use vfs::FileId; 17pub use vfs::FileId;
18 18
@@ -43,6 +43,12 @@ impl SourceRoot {
43 pub fn new_library(file_set: FileSet) -> SourceRoot { 43 pub fn new_library(file_set: FileSet) -> SourceRoot {
44 SourceRoot { is_library: true, file_set } 44 SourceRoot { is_library: true, file_set }
45 } 45 }
46 pub fn path_for_file(&self, file: &FileId) -> Option<&VfsPath> {
47 self.file_set.path_for_file(file)
48 }
49 pub fn file_for_path(&self, path: &VfsPath) -> Option<&FileId> {
50 self.file_set.file_for_path(path)
51 }
46 pub fn iter(&self) -> impl Iterator<Item = FileId> + '_ { 52 pub fn iter(&self) -> impl Iterator<Item = FileId> + '_ {
47 self.file_set.iter() 53 self.file_set.iter()
48 } 54 }
diff --git a/crates/base_db/src/lib.rs b/crates/base_db/src/lib.rs
index 321007d33..ee3415850 100644
--- a/crates/base_db/src/lib.rs
+++ b/crates/base_db/src/lib.rs
@@ -96,7 +96,6 @@ pub trait FileLoader {
96 /// `#[path = "C://no/way"]` 96 /// `#[path = "C://no/way"]`
97 fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>; 97 fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>;
98 fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>; 98 fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
99 fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String>;
100} 99}
101 100
102/// Database which stores all significant input facts: source code and project 101/// Database which stores all significant input facts: source code and project
@@ -156,8 +155,8 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
156 } 155 }
157 fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> { 156 fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
158 // FIXME: this *somehow* should be platform agnostic... 157 // FIXME: this *somehow* should be platform agnostic...
159 // self.source_root(anchor) 158 let source_root = self.0.file_source_root(anchor);
160 let source_root = self.source_root(anchor); 159 let source_root = self.0.source_root(source_root);
161 source_root.file_set.resolve_path(anchor, path) 160 source_root.file_set.resolve_path(anchor, path)
162 } 161 }
163 162
@@ -165,83 +164,4 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
165 let source_root = self.0.file_source_root(file_id); 164 let source_root = self.0.file_source_root(file_id);
166 self.0.source_root_crates(source_root) 165 self.0.source_root_crates(source_root)
167 } 166 }
168
169 fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
170 possible_sudmobule_names(&self.source_root(module_file).file_set, module_file)
171 }
172}
173
174impl<T: SourceDatabaseExt> FileLoaderDelegate<&'_ T> {
175 fn source_root(&self, anchor: FileId) -> Arc<SourceRoot> {
176 let source_root = self.0.file_source_root(anchor);
177 self.0.source_root(source_root)
178 }
179}
180
181fn possible_sudmobule_names(module_files: &FileSet, module_file: FileId) -> Vec<String> {
182 let directory_to_look_for_submodules = match module_files
183 .path_for_file(&module_file)
184 .and_then(|module_file_path| get_directory_with_submodules(module_file_path))
185 {
186 Some(directory) => directory,
187 None => return Vec::new(),
188 };
189 module_files
190 .iter()
191 .filter(|submodule_file| submodule_file != &module_file)
192 .filter_map(|submodule_file| {
193 let submodule_path = module_files.path_for_file(&submodule_file)?;
194 if submodule_path.parent()? == directory_to_look_for_submodules {
195 submodule_path.file_name_and_extension()
196 } else {
197 None
198 }
199 })
200 .filter_map(|file_name_and_extension| {
201 match file_name_and_extension {
202 // TODO kb wrong resolution for nested non-file modules (mod tests { mod <|> })
203 // TODO kb in src/bin when a module is included into another,
204 // the included file gets "moved" into a directory below and now cannot add any other modules
205 ("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => None,
206 (file_name, Some("rs")) => Some(file_name.to_owned()),
207 (subdirectory_name, None) => {
208 let mod_rs_path =
209 directory_to_look_for_submodules.join(subdirectory_name)?.join("mod.rs")?;
210 if module_files.file_for_path(&mod_rs_path).is_some() {
211 Some(subdirectory_name.to_owned())
212 } else {
213 None
214 }
215 }
216 _ => None,
217 }
218 })
219 .collect()
220}
221
222fn get_directory_with_submodules(module_file_path: &VfsPath) -> Option<VfsPath> {
223 let module_directory_path = module_file_path.parent()?;
224 match module_file_path.file_name_and_extension()? {
225 ("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => {
226 Some(module_directory_path)
227 }
228 (regular_rust_file_name, Some("rs")) => {
229 if matches!(
230 (
231 module_directory_path
232 .parent()
233 .as_ref()
234 .and_then(|path| path.file_name_and_extension()),
235 module_directory_path.file_name_and_extension(),
236 ),
237 (Some(("src", None)), Some(("bin", None)))
238 ) {
239 // files in /src/bin/ can import each other directly
240 Some(module_directory_path)
241 } else {
242 module_directory_path.join(regular_rust_file_name)
243 }
244 }
245 _ => None,
246 }
247} 167}