From 0de71f7bc9482c9d1ef7e9d36ec5d6c5fd378781 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 4 Sep 2020 01:32:06 +0300 Subject: Properly use FileSet API --- crates/vfs/src/file_set.rs | 46 +++++++++++++++++++++++++--------------------- crates/vfs/src/vfs_path.rs | 23 ++++++++++++++++------- 2 files changed, 41 insertions(+), 28 deletions(-) (limited to 'crates/vfs') diff --git a/crates/vfs/src/file_set.rs b/crates/vfs/src/file_set.rs index 0caddc3bc..3f49f31e5 100644 --- a/crates/vfs/src/file_set.rs +++ b/crates/vfs/src/file_set.rs @@ -20,34 +20,38 @@ impl FileSet { self.files.len() } pub fn resolve_path(&self, anchor: FileId, path: &str) -> Option { - let mut base = dbg!(self.paths[&anchor].clone()); + let mut base = self.paths[&anchor].clone(); base.pop(); - let path = dbg!(base).join(dbg!(path))?; + let path = base.join(path)?; self.files.get(&path).copied() } - pub fn file_name_and_extension(&self, file: FileId) -> Option<(&str, &str)> { + pub fn file_name_and_extension(&self, file: FileId) -> Option<(&str, Option<&str>)> { self.paths[&file].file_name_and_extension() } - pub fn list_files(&self, directory: FileId) -> Vec<(FileId, String)> { - // TODO kb determine the ways to list all applicable files - // Maybe leave list directory here only and the move the rest of the logic into the database impl? - // cache results in Salsa? - - dbg!(directory); - /* - [crates/vfs/src/file_set.rs:30] directory = "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/" - [crates/vfs/src/file_set.rs:31] self.files.keys() = [ - "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_1/test_mod_2/test_mod_3.rs", - "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_1/test_mod_2.rs", - "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_1.rs", - "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/lib.rs", - "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_3/test_mod_3_1.rs", - "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_3.rs", - ] - */ - Vec::new() + pub fn list_files( + &self, + anchor: FileId, + anchor_relative_path: Option<&str>, + ) -> 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("../"), + } + }?; + + 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())) + .collect(), + ) } pub fn insert(&mut self, file_id: FileId, path: VfsPath) { self.files.insert(path.clone(), file_id); diff --git a/crates/vfs/src/vfs_path.rs b/crates/vfs/src/vfs_path.rs index 7b965bb4c..f2d07038b 100644 --- a/crates/vfs/src/vfs_path.rs +++ b/crates/vfs/src/vfs_path.rs @@ -48,13 +48,19 @@ 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 file_name_and_extension(&self) -> Option<(&str, &str)> { + pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> { match &self.0 { - VfsPathRepr::PathBuf(p) => p - .file_stem() - .zip(p.extension()) - .and_then(|(name, extension)| Some((name.to_str()?, extension.to_str()?))), + VfsPathRepr::PathBuf(p) => Some(( + p.file_stem()?.to_str()?, + p.extension().and_then(|extension| extension.to_str()), + )), VfsPathRepr::VirtualPath(p) => p.file_name_and_extension(), } } @@ -259,6 +265,9 @@ 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, @@ -279,8 +288,8 @@ impl VirtualPath { Some(res) } - pub fn file_name_and_extension(&self) -> Option<(&str, &str)> { + pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> { // TODO kb check if is a file - Some(("test_mod_1", "rs")) + Some(("test_mod_1", Some("rs"))) } } -- cgit v1.2.3