From 8aa740dab46f138cacdf6391d46c87d6df810161 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 4 Sep 2020 02:25:00 +0300 Subject: Happy path implemented --- crates/vfs/src/file_set.rs | 17 +++++++++++------ crates/vfs/src/vfs_path.rs | 13 ++++++------- 2 files changed, 17 insertions(+), 13 deletions(-) (limited to 'crates/vfs/src') diff --git a/crates/vfs/src/file_set.rs b/crates/vfs/src/file_set.rs index 3f49f31e5..956cffb29 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 f2d07038b..9a3690a89 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, -- cgit v1.2.3