Happy path implemented

This commit is contained in:
Kirill Bulatov 2020-09-04 02:25:00 +03:00
parent 0de71f7bc9
commit 8aa740dab4
8 changed files with 38 additions and 26 deletions

View File

@ -96,7 +96,7 @@ pub trait FileLoader {
/// `#[path = "C://no/way"]` /// `#[path = "C://no/way"]`
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>; fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>;
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>; fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)>; fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String>;
} }
/// Database which stores all significant input facts: source code and project /// Database which stores all significant input facts: source code and project
@ -166,11 +166,11 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
self.0.source_root_crates(source_root) 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<String> {
fn possible_sudmobules_opt( fn possible_sudmobules_opt(
module_files: &FileSet, module_files: &FileSet,
module_file: FileId, module_file: FileId,
) -> Option<Vec<(FileId, String)>> { ) -> Option<Vec<FileId>> {
match module_files.file_name_and_extension(module_file)? { match module_files.file_name_and_extension(module_file)? {
("mod", Some("rs")) | ("lib", Some("rs")) => { ("mod", Some("rs")) | ("lib", Some("rs")) => {
module_files.list_files(module_file, None) module_files.list_files(module_file, None)
@ -181,8 +181,16 @@ impl<T: SourceDatabaseExt> 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() .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()
} }
} }

View File

@ -63,8 +63,8 @@ impl FileLoader for TestDB {
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> { fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id) FileLoaderDelegate(self).relevant_crates(file_id)
} }
fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)> { fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
FileLoaderDelegate(self).possible_sudmobules(module_file) FileLoaderDelegate(self).possible_sudmobule_names(module_file)
} }
} }

View File

@ -46,7 +46,7 @@ impl FileLoader for TestDB {
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> { fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id) FileLoaderDelegate(self).relevant_crates(file_id)
} }
fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)> { fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
FileLoaderDelegate(self).possible_sudmobules(module_file) FileLoaderDelegate(self).possible_sudmobule_names(module_file)
} }
} }

View File

@ -73,8 +73,8 @@ impl FileLoader for TestDB {
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> { fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id) FileLoaderDelegate(self).relevant_crates(file_id)
} }
fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)> { fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
FileLoaderDelegate(self).possible_sudmobules(module_file) FileLoaderDelegate(self).possible_sudmobule_names(module_file)
} }
} }

View File

@ -122,7 +122,7 @@ impl<'a> CompletionContext<'a> {
} }
let module_definition_source_file = definition_source.file_id.original_file(db); let module_definition_source_file = definition_source.file_id.original_file(db);
let mod_declaration_candidates = let mod_declaration_candidates =
db.possible_sudmobules(module_definition_source_file); db.possible_sudmobule_names(module_definition_source_file);
dbg!(mod_declaration_candidates); dbg!(mod_declaration_candidates);
// TODO kb exlude existing children from the candidates // TODO kb exlude existing children from the candidates
let existing_children = current_module.children(db).collect::<Vec<_>>(); let existing_children = current_module.children(db).collect::<Vec<_>>();

View File

@ -74,8 +74,8 @@ impl FileLoader for RootDatabase {
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> { fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id) FileLoaderDelegate(self).relevant_crates(file_id)
} }
fn possible_sudmobules(&self, module_file: FileId) -> Vec<(FileId, String)> { fn possible_sudmobule_names(&self, module_file: FileId) -> Vec<String> {
FileLoaderDelegate(self).possible_sudmobules(module_file) FileLoaderDelegate(self).possible_sudmobule_names(module_file)
} }
} }

View File

@ -34,22 +34,27 @@ impl FileSet {
&self, &self,
anchor: FileId, anchor: FileId,
anchor_relative_path: Option<&str>, anchor_relative_path: Option<&str>,
) -> Option<Vec<(FileId, String)>> { ) -> Option<Vec<FileId>> {
let anchor_directory = { let anchor_directory = {
let path = self.paths[&anchor].clone(); let path = self.paths[&anchor].clone();
match anchor_relative_path { match anchor_relative_path {
Some(anchor_relative_path) => path.join(anchor_relative_path), Some(anchor_relative_path) => path.join(anchor_relative_path),
None => path.join("../"), None => path.parent(),
} }
}?; }?;
Some( Some(
self.paths self.paths
.iter() .iter()
.filter(|(_, path)| path.starts_with(&anchor_directory)) .filter_map(|(&file_id, path)| {
// TODO kb need to ensure that no / exists after the anchor_directory if path.parent()? == anchor_directory
.filter(|(_, path)| path.ends_with(".rs")) && matches!(path.file_name_and_extension(), Some((_, Some("rs"))))
.map(|(&file_id, path)| (file_id, path.to_string())) {
Some(file_id)
} else {
None
}
})
.collect(), .collect(),
) )
} }

View File

@ -48,10 +48,12 @@ impl VfsPath {
(VfsPathRepr::VirtualPath(_), _) => false, (VfsPathRepr::VirtualPath(_), _) => false,
} }
} }
pub fn ends_with(&self, suffix: &str) -> bool { pub fn parent(&self) -> Option<VfsPath> {
match &self.0 { let mut parent = self.clone();
VfsPathRepr::PathBuf(p) => p.ends_with(suffix), if parent.pop() {
VfsPathRepr::VirtualPath(p) => p.ends_with(suffix), Some(parent)
} else {
None
} }
} }
@ -265,9 +267,6 @@ impl VirtualPath {
fn starts_with(&self, other: &VirtualPath) -> bool { fn starts_with(&self, other: &VirtualPath) -> bool {
self.0.starts_with(&other.0) self.0.starts_with(&other.0)
} }
fn ends_with(&self, suffix: &str) -> bool {
self.0.ends_with(suffix)
}
fn pop(&mut self) -> bool { fn pop(&mut self) -> bool {
let pos = match self.0.rfind('/') { let pos = match self.0.rfind('/') {
Some(pos) => pos, Some(pos) => pos,