From f9c14ac7204c38633e70b3efd47a5b1f9056afd0 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 7 Sep 2020 20:52:37 +0300 Subject: Move most of the logic into the completion module --- crates/base_db/src/input.rs | 8 ++++- crates/base_db/src/lib.rs | 84 ++------------------------------------------- 2 files changed, 9 insertions(+), 83 deletions(-) (limited to 'crates/base_db/src') 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; use rustc_hash::{FxHashMap, FxHashSet}; use syntax::SmolStr; use tt::TokenExpander; -use vfs::file_set::FileSet; +use vfs::{file_set::FileSet, VfsPath}; pub use vfs::FileId; @@ -43,6 +43,12 @@ impl SourceRoot { pub fn new_library(file_set: FileSet) -> SourceRoot { SourceRoot { is_library: true, file_set } } + pub fn path_for_file(&self, file: &FileId) -> Option<&VfsPath> { + self.file_set.path_for_file(file) + } + pub fn file_for_path(&self, path: &VfsPath) -> Option<&FileId> { + self.file_set.file_for_path(path) + } pub fn iter(&self) -> impl Iterator + '_ { self.file_set.iter() } 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 { /// `#[path = "C://no/way"]` fn resolve_path(&self, anchor: FileId, path: &str) -> Option; fn relevant_crates(&self, file_id: FileId) -> Arc>; - fn possible_sudmobule_names(&self, module_file: FileId) -> Vec; } /// Database which stores all significant input facts: source code and project @@ -156,8 +155,8 @@ impl FileLoader for FileLoaderDelegate<&'_ T> { } fn resolve_path(&self, anchor: FileId, path: &str) -> Option { // FIXME: this *somehow* should be platform agnostic... - // self.source_root(anchor) - let source_root = self.source_root(anchor); + let source_root = self.0.file_source_root(anchor); + let source_root = self.0.source_root(source_root); source_root.file_set.resolve_path(anchor, path) } @@ -165,83 +164,4 @@ impl FileLoader for FileLoaderDelegate<&'_ T> { let source_root = self.0.file_source_root(file_id); self.0.source_root_crates(source_root) } - - fn possible_sudmobule_names(&self, module_file: FileId) -> Vec { - possible_sudmobule_names(&self.source_root(module_file).file_set, module_file) - } -} - -impl FileLoaderDelegate<&'_ T> { - fn source_root(&self, anchor: FileId) -> Arc { - let source_root = self.0.file_source_root(anchor); - self.0.source_root(source_root) - } -} - -fn possible_sudmobule_names(module_files: &FileSet, module_file: FileId) -> Vec { - let directory_to_look_for_submodules = match module_files - .path_for_file(&module_file) - .and_then(|module_file_path| get_directory_with_submodules(module_file_path)) - { - Some(directory) => directory, - None => return Vec::new(), - }; - module_files - .iter() - .filter(|submodule_file| submodule_file != &module_file) - .filter_map(|submodule_file| { - let submodule_path = module_files.path_for_file(&submodule_file)?; - if submodule_path.parent()? == directory_to_look_for_submodules { - submodule_path.file_name_and_extension() - } else { - None - } - }) - .filter_map(|file_name_and_extension| { - match file_name_and_extension { - // TODO kb wrong resolution for nested non-file modules (mod tests { mod <|> }) - // TODO kb in src/bin when a module is included into another, - // the included file gets "moved" into a directory below and now cannot add any other modules - ("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => None, - (file_name, Some("rs")) => Some(file_name.to_owned()), - (subdirectory_name, None) => { - let mod_rs_path = - directory_to_look_for_submodules.join(subdirectory_name)?.join("mod.rs")?; - if module_files.file_for_path(&mod_rs_path).is_some() { - Some(subdirectory_name.to_owned()) - } else { - None - } - } - _ => None, - } - }) - .collect() -} - -fn get_directory_with_submodules(module_file_path: &VfsPath) -> Option { - let module_directory_path = module_file_path.parent()?; - match module_file_path.file_name_and_extension()? { - ("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => { - Some(module_directory_path) - } - (regular_rust_file_name, Some("rs")) => { - if matches!( - ( - module_directory_path - .parent() - .as_ref() - .and_then(|path| path.file_name_and_extension()), - module_directory_path.file_name_and_extension(), - ), - (Some(("src", None)), Some(("bin", None))) - ) { - // files in /src/bin/ can import each other directly - Some(module_directory_path) - } else { - module_directory_path.join(regular_rust_file_name) - } - } - _ => None, - } } -- cgit v1.2.3