From 8aa740dab46f138cacdf6391d46c87d6df810161 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 4 Sep 2020 02:25:00 +0300 Subject: [PATCH] Happy path implemented --- crates/base_db/src/lib.rs | 16 ++++++++++++---- crates/hir_def/src/test_db.rs | 4 ++-- crates/hir_expand/src/test_db.rs | 4 ++-- crates/hir_ty/src/test_db.rs | 4 ++-- crates/ide/src/completion/completion_context.rs | 2 +- crates/ide_db/src/lib.rs | 4 ++-- crates/vfs/src/file_set.rs | 17 +++++++++++------ crates/vfs/src/vfs_path.rs | 13 ++++++------- 8 files changed, 38 insertions(+), 26 deletions(-) diff --git a/crates/base_db/src/lib.rs b/crates/base_db/src/lib.rs index 1bc4690c9b..3e0b6637d2 100644 --- a/crates/base_db/src/lib.rs +++ b/crates/base_db/src/lib.rs @@ -96,7 +96,7 @@ 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_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)>; + fn possible_sudmobule_names(&self, module_file: FileId) -> Vec; } /// Database which stores all significant input facts: source code and project @@ -166,11 +166,11 @@ impl FileLoader for FileLoaderDelegate<&'_ T> { self.0.source_root_crates(source_root) } - fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)> { + fn possible_sudmobule_names(&self, module_file: FileId) -> Vec { fn possible_sudmobules_opt( module_files: &FileSet, module_file: FileId, - ) -> Option> { + ) -> Option> { match module_files.file_name_and_extension(module_file)? { ("mod", Some("rs")) | ("lib", Some("rs")) => { module_files.list_files(module_file, None) @@ -181,8 +181,16 @@ impl FileLoader for FileLoaderDelegate<&'_ T> { } } - possible_sudmobules_opt(&self.source_root(module_file).file_set, module_file) + let module_files = &self.source_root(module_file).file_set; + possible_sudmobules_opt(module_files, module_file) .unwrap_or_default() + .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(), + }) + .collect() } } diff --git a/crates/hir_def/src/test_db.rs b/crates/hir_def/src/test_db.rs index a35ed4f3c3..5bcfaf464e 100644 --- a/crates/hir_def/src/test_db.rs +++ b/crates/hir_def/src/test_db.rs @@ -63,8 +63,8 @@ impl FileLoader for TestDB { fn relevant_crates(&self, file_id: FileId) -> Arc> { FileLoaderDelegate(self).relevant_crates(file_id) } - fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)> { - FileLoaderDelegate(self).possible_sudmobules(module_file) + fn possible_sudmobule_names(&self, module_file: FileId) -> Vec { + FileLoaderDelegate(self).possible_sudmobule_names(module_file) } } diff --git a/crates/hir_expand/src/test_db.rs b/crates/hir_expand/src/test_db.rs index a0d1525b00..cf42dde7a4 100644 --- a/crates/hir_expand/src/test_db.rs +++ b/crates/hir_expand/src/test_db.rs @@ -46,7 +46,7 @@ impl FileLoader for TestDB { fn relevant_crates(&self, file_id: FileId) -> Arc> { FileLoaderDelegate(self).relevant_crates(file_id) } - fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)> { - FileLoaderDelegate(self).possible_sudmobules(module_file) + fn possible_sudmobule_names(&self, module_file: FileId) -> Vec { + FileLoaderDelegate(self).possible_sudmobule_names(module_file) } } diff --git a/crates/hir_ty/src/test_db.rs b/crates/hir_ty/src/test_db.rs index 6f61e7dfed..0696f41dd3 100644 --- a/crates/hir_ty/src/test_db.rs +++ b/crates/hir_ty/src/test_db.rs @@ -73,8 +73,8 @@ impl FileLoader for TestDB { fn relevant_crates(&self, file_id: FileId) -> Arc> { FileLoaderDelegate(self).relevant_crates(file_id) } - fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)> { - FileLoaderDelegate(self).possible_sudmobules(module_file) + fn possible_sudmobule_names(&self, module_file: FileId) -> Vec { + FileLoaderDelegate(self).possible_sudmobule_names(module_file) } } diff --git a/crates/ide/src/completion/completion_context.rs b/crates/ide/src/completion/completion_context.rs index cbfc77a461..b4c6eeb35c 100644 --- a/crates/ide/src/completion/completion_context.rs +++ b/crates/ide/src/completion/completion_context.rs @@ -122,7 +122,7 @@ impl<'a> CompletionContext<'a> { } let module_definition_source_file = definition_source.file_id.original_file(db); let mod_declaration_candidates = - db.possible_sudmobules(module_definition_source_file); + db.possible_sudmobule_names(module_definition_source_file); dbg!(mod_declaration_candidates); // TODO kb exlude existing children from the candidates let existing_children = current_module.children(db).collect::>(); diff --git a/crates/ide_db/src/lib.rs b/crates/ide_db/src/lib.rs index dc1d2b9fe2..9f3be8601d 100644 --- a/crates/ide_db/src/lib.rs +++ b/crates/ide_db/src/lib.rs @@ -74,8 +74,8 @@ impl FileLoader for RootDatabase { fn relevant_crates(&self, file_id: FileId) -> Arc> { FileLoaderDelegate(self).relevant_crates(file_id) } - fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)> { - FileLoaderDelegate(self).possible_sudmobules(module_file) + fn possible_sudmobule_names(&self, module_file: FileId) -> Vec { + FileLoaderDelegate(self).possible_sudmobule_names(module_file) } } diff --git a/crates/vfs/src/file_set.rs b/crates/vfs/src/file_set.rs index 3f49f31e57..956cffb29b 100644 --- a/crates/vfs/src/file_set.rs +++ b/crates/vfs/src/file_set.rs @@ -34,22 +34,27 @@ impl FileSet { &self, anchor: FileId, anchor_relative_path: Option<&str>, - ) -> Option> { + ) -> Option> { let anchor_directory = { let path = self.paths[&anchor].clone(); match anchor_relative_path { Some(anchor_relative_path) => path.join(anchor_relative_path), - None => path.join("../"), + None => path.parent(), } }?; Some( self.paths .iter() - .filter(|(_, path)| path.starts_with(&anchor_directory)) - // TODO kb need to ensure that no / exists after the anchor_directory - .filter(|(_, path)| path.ends_with(".rs")) - .map(|(&file_id, path)| (file_id, path.to_string())) + .filter_map(|(&file_id, path)| { + if path.parent()? == anchor_directory + && matches!(path.file_name_and_extension(), Some((_, Some("rs")))) + { + Some(file_id) + } else { + None + } + }) .collect(), ) } diff --git a/crates/vfs/src/vfs_path.rs b/crates/vfs/src/vfs_path.rs index f2d07038bb..9a3690a897 100644 --- a/crates/vfs/src/vfs_path.rs +++ b/crates/vfs/src/vfs_path.rs @@ -48,10 +48,12 @@ impl VfsPath { (VfsPathRepr::VirtualPath(_), _) => false, } } - pub fn ends_with(&self, suffix: &str) -> bool { - match &self.0 { - VfsPathRepr::PathBuf(p) => p.ends_with(suffix), - VfsPathRepr::VirtualPath(p) => p.ends_with(suffix), + pub fn parent(&self) -> Option { + let mut parent = self.clone(); + if parent.pop() { + Some(parent) + } else { + None } } @@ -265,9 +267,6 @@ impl VirtualPath { fn starts_with(&self, other: &VirtualPath) -> bool { self.0.starts_with(&other.0) } - fn ends_with(&self, suffix: &str) -> bool { - self.0.ends_with(suffix) - } fn pop(&mut self) -> bool { let pos = match self.0.rfind('/') { Some(pos) => pos,