From 4bed588001a1d6cd5c83a3eefc6ef77c439de40b Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 28 Aug 2020 21:28:30 +0300 Subject: First steps for mod<|> completion --- crates/vfs/src/file_set.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'crates/vfs/src') diff --git a/crates/vfs/src/file_set.rs b/crates/vfs/src/file_set.rs index e9196fcd2..8bce17bc0 100644 --- a/crates/vfs/src/file_set.rs +++ b/crates/vfs/src/file_set.rs @@ -23,8 +23,31 @@ impl FileSet { let mut base = self.paths[&anchor].clone(); base.pop(); let path = base.join(path)?; - let res = self.files.get(&path).copied(); - res + self.files.get(&path).copied() + } + pub fn list_some_random_files_todo(&self, anchor: FileId) -> Vec<(FileId, String)> { + let anchor_path = self.paths[&anchor].clone(); + /* + [crates/vfs/src/file_set.rs:30] anchor_path = "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/lib.rs" + [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", + ] + */ + + // 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? + + // Need to get the following things: + // * name of the anchor_path file (file_name, validate that it's a file!) + // * list of all files in the file's contai/ning directory (file_dir) + // * list of all files in `file_dir/file_name` or just `file_dir/`, for lib.rs or mod.rs + // * consider special case for /src/bin/foo.rs as a mod<|> source + Vec::new() } pub fn insert(&mut self, file_id: FileId, path: VfsPath) { self.files.insert(path.clone(), file_id); -- cgit v1.2.3 From 17870a3e2c39770a99f9ab5ce090abbe1dc334d2 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Thu, 3 Sep 2020 23:18:23 +0300 Subject: Better API --- crates/vfs/src/file_set.rs | 28 ++++++++++++++-------------- crates/vfs/src/vfs_path.rs | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 14 deletions(-) (limited to 'crates/vfs/src') diff --git a/crates/vfs/src/file_set.rs b/crates/vfs/src/file_set.rs index 8bce17bc0..0caddc3bc 100644 --- a/crates/vfs/src/file_set.rs +++ b/crates/vfs/src/file_set.rs @@ -20,15 +20,24 @@ impl FileSet { self.files.len() } pub fn resolve_path(&self, anchor: FileId, path: &str) -> Option { - let mut base = self.paths[&anchor].clone(); + let mut base = dbg!(self.paths[&anchor].clone()); base.pop(); - let path = base.join(path)?; + let path = dbg!(base).join(dbg!(path))?; self.files.get(&path).copied() } - pub fn list_some_random_files_todo(&self, anchor: FileId) -> Vec<(FileId, String)> { - let anchor_path = self.paths[&anchor].clone(); + + pub fn file_name_and_extension(&self, file: FileId) -> Option<(&str, &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] anchor_path = "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/lib.rs" + [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", @@ -38,15 +47,6 @@ impl FileSet { "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_3.rs", ] */ - - // 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? - - // Need to get the following things: - // * name of the anchor_path file (file_name, validate that it's a file!) - // * list of all files in the file's contai/ning directory (file_dir) - // * list of all files in `file_dir/file_name` or just `file_dir/`, for lib.rs or mod.rs - // * consider special case for /src/bin/foo.rs as a mod<|> source Vec::new() } pub fn insert(&mut self, file_id: FileId, path: VfsPath) { diff --git a/crates/vfs/src/vfs_path.rs b/crates/vfs/src/vfs_path.rs index 944a702df..7b965bb4c 100644 --- a/crates/vfs/src/vfs_path.rs +++ b/crates/vfs/src/vfs_path.rs @@ -49,6 +49,16 @@ impl VfsPath { } } + pub fn file_name_and_extension(&self) -> Option<(&str, &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::VirtualPath(p) => p.file_name_and_extension(), + } + } + // Don't make this `pub` pub(crate) fn encode(&self, buf: &mut Vec) { let tag = match &self.0 { @@ -268,4 +278,9 @@ impl VirtualPath { res.0 = format!("{}/{}", res.0, path); Some(res) } + + pub fn file_name_and_extension(&self) -> Option<(&str, &str)> { + // TODO kb check if is a file + Some(("test_mod_1", "rs")) + } } -- cgit v1.2.3 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/src') 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 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 From d163f9f114c8180bec6285ad9962fabbf3af5b18 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 4 Sep 2020 13:33:07 +0300 Subject: Small refactoring --- crates/vfs/src/file_set.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'crates/vfs/src') diff --git a/crates/vfs/src/file_set.rs b/crates/vfs/src/file_set.rs index 956cffb29..3085fd818 100644 --- a/crates/vfs/src/file_set.rs +++ b/crates/vfs/src/file_set.rs @@ -30,33 +30,33 @@ impl FileSet { self.paths[&file].file_name_and_extension() } - pub fn list_files( + pub fn list_files_with_extensions( &self, anchor: FileId, anchor_relative_path: Option<&str>, - ) -> Option> { + ) -> Vec<(&str, Option<&str>)> { let anchor_directory = { let path = self.paths[&anchor].clone(); match anchor_relative_path { Some(anchor_relative_path) => path.join(anchor_relative_path), None => path.parent(), } - }?; + }; - Some( + if let Some(anchor_directory) = anchor_directory { self.paths .iter() - .filter_map(|(&file_id, path)| { - if path.parent()? == anchor_directory - && matches!(path.file_name_and_extension(), Some((_, Some("rs")))) - { - Some(file_id) + .filter_map(|(_, path)| { + if path.parent()? == anchor_directory { + path.file_name_and_extension() } else { None } }) - .collect(), - ) + .collect() + } else { + Vec::new() + } } pub fn insert(&mut self, file_id: FileId, path: VfsPath) { self.files.insert(path.clone(), file_id); -- cgit v1.2.3 From 897a4c702e3d6fa9156ea0bc34af9d397fae3440 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 4 Sep 2020 15:05:56 +0300 Subject: Implement file name & extension retrieval method for VirtualPath --- crates/vfs/src/vfs_path.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'crates/vfs/src') diff --git a/crates/vfs/src/vfs_path.rs b/crates/vfs/src/vfs_path.rs index 9a3690a89..113c2e4e6 100644 --- a/crates/vfs/src/vfs_path.rs +++ b/crates/vfs/src/vfs_path.rs @@ -287,8 +287,26 @@ impl VirtualPath { Some(res) } + // FIXME: Currently VirtualPath does is unable to distinguish a directory from a file + // hence this method will return `Some("directory_name", None)` for a directory pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> { - // TODO kb check if is a file - Some(("test_mod_1", Some("rs"))) + let file_name = match self.0.rfind('/') { + Some(position) => &self.0[position + 1..], + None => &self.0, + }; + + if file_name.is_empty() { + None + } else { + let mut file_stem_and_extension = file_name.rsplitn(2, '.'); + let extension = file_stem_and_extension.next(); + let file_stem = file_stem_and_extension.next(); + + match (file_stem, extension) { + (None, None) => None, + (None, Some(_)) | (Some(""), Some(_)) => Some((file_name, None)), + (Some(file_stem), extension) => Some((file_stem, extension)), + } + } } } -- cgit v1.2.3 From b2bcc5278db23c3ba0a4f47a3ef6ee411aaaa8dc Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 6 Sep 2020 01:41:18 +0300 Subject: Properly handle special cases (binaries, mod.rs) --- crates/vfs/src/file_set.rs | 84 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 26 deletions(-) (limited to 'crates/vfs/src') diff --git a/crates/vfs/src/file_set.rs b/crates/vfs/src/file_set.rs index 3085fd818..cb65c17e0 100644 --- a/crates/vfs/src/file_set.rs +++ b/crates/vfs/src/file_set.rs @@ -26,38 +26,70 @@ impl FileSet { self.files.get(&path).copied() } - pub fn file_name_and_extension(&self, file: FileId) -> Option<(&str, Option<&str>)> { - self.paths[&file].file_name_and_extension() - } - - pub fn list_files_with_extensions( - &self, - anchor: FileId, - anchor_relative_path: Option<&str>, - ) -> Vec<(&str, Option<&str>)> { - let anchor_directory = { - let path = self.paths[&anchor].clone(); - match anchor_relative_path { - Some(anchor_relative_path) => path.join(anchor_relative_path), - None => path.parent(), - } + pub fn possible_sudmobule_names(&self, module_file: FileId) -> Vec { + let directory_to_look_for_submodules = match self.get_directory_with_submodules(module_file) + { + Some(directory) => directory, + None => return Vec::new(), }; - - if let Some(anchor_directory) = anchor_directory { - self.paths - .iter() - .filter_map(|(_, path)| { - if path.parent()? == anchor_directory { - path.file_name_and_extension() + self.paths + .iter() + .filter_map(|(_, path)| { + if path.parent()? == directory_to_look_for_submodules { + path.file_name_and_extension() + } else { + None + } + }) + .filter_map(|file_name_and_extension| match file_name_and_extension { + // TODO kb do not include the module file name itself, if present + // TODO kb wrong resolution for nesСпаted 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 self.files.contains_key(&mod_rs_path) { + Some(subdirectory_name.to_owned()) } else { None } - }) - .collect() - } else { - Vec::new() + } + _ => None, + }) + .collect() + } + + fn get_directory_with_submodules(&self, module_file: FileId) -> Option { + let module_file_path = &self.paths[&module_file]; + let module_directory_path = module_file_path.parent()?; + match module_file_path.file_name_and_extension() { + Some(("mod", Some("rs"))) | Some(("lib", Some("rs"))) | Some(("main", Some("rs"))) => { + Some(module_directory_path) + } + Some((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, } } + pub fn insert(&mut self, file_id: FileId, path: VfsPath) { self.files.insert(path.clone(), file_id); self.paths.insert(file_id, path); -- cgit v1.2.3 From 33179a0ae1ba9a908cc34a4cf87599ed779b9886 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 7 Sep 2020 16:17:50 +0300 Subject: Move rust-related logic from vfs to base_db level --- crates/vfs/src/file_set.rs | 65 ++++------------------------------------------ 1 file changed, 5 insertions(+), 60 deletions(-) (limited to 'crates/vfs/src') diff --git a/crates/vfs/src/file_set.rs b/crates/vfs/src/file_set.rs index cb65c17e0..4aa2d6526 100644 --- a/crates/vfs/src/file_set.rs +++ b/crates/vfs/src/file_set.rs @@ -26,74 +26,19 @@ impl FileSet { self.files.get(&path).copied() } - pub fn possible_sudmobule_names(&self, module_file: FileId) -> Vec { - let directory_to_look_for_submodules = match self.get_directory_with_submodules(module_file) - { - Some(directory) => directory, - None => return Vec::new(), - }; - self.paths - .iter() - .filter_map(|(_, path)| { - if path.parent()? == directory_to_look_for_submodules { - path.file_name_and_extension() - } else { - None - } - }) - .filter_map(|file_name_and_extension| match file_name_and_extension { - // TODO kb do not include the module file name itself, if present - // TODO kb wrong resolution for nesСпаted 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 self.files.contains_key(&mod_rs_path) { - Some(subdirectory_name.to_owned()) - } else { - None - } - } - _ => None, - }) - .collect() + pub fn file_for_path(&self, path: &VfsPath) -> Option<&FileId> { + self.files.get(path) } - fn get_directory_with_submodules(&self, module_file: FileId) -> Option { - let module_file_path = &self.paths[&module_file]; - let module_directory_path = module_file_path.parent()?; - match module_file_path.file_name_and_extension() { - Some(("mod", Some("rs"))) | Some(("lib", Some("rs"))) | Some(("main", Some("rs"))) => { - Some(module_directory_path) - } - Some((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, - } + pub fn path_for_file(&self, file: &FileId) -> Option<&VfsPath> { + self.paths.get(file) } pub fn insert(&mut self, file_id: FileId, path: VfsPath) { self.files.insert(path.clone(), file_id); self.paths.insert(file_id, path); } + pub fn iter(&self) -> impl Iterator + '_ { self.paths.keys().copied() } -- cgit v1.2.3 From f4ee885b3b3019b32d5c481bddf1b2667fba7fb3 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 8 Sep 2020 02:42:45 +0300 Subject: Add VirtualPath tests --- crates/vfs/src/vfs_path.rs | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'crates/vfs/src') diff --git a/crates/vfs/src/vfs_path.rs b/crates/vfs/src/vfs_path.rs index 113c2e4e6..dac8393ea 100644 --- a/crates/vfs/src/vfs_path.rs +++ b/crates/vfs/src/vfs_path.rs @@ -290,9 +290,10 @@ impl VirtualPath { // FIXME: Currently VirtualPath does is unable to distinguish a directory from a file // hence this method will return `Some("directory_name", None)` for a directory pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> { - let file_name = match self.0.rfind('/') { - Some(position) => &self.0[position + 1..], - None => &self.0, + let file_path = if self.0.ends_with('/') { &self.0[..&self.0.len() - 1] } else { &self.0 }; + let file_name = match file_path.rfind('/') { + Some(position) => &file_path[position + 1..], + None => file_path, }; if file_name.is_empty() { @@ -310,3 +311,37 @@ impl VirtualPath { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn virtual_path_extensions() { + assert_eq!(VirtualPath("/".to_string()).file_name_and_extension(), None); + assert_eq!( + VirtualPath("/directory".to_string()).file_name_and_extension(), + Some(("directory", None)) + ); + assert_eq!( + VirtualPath("/directory/".to_string()).file_name_and_extension(), + Some(("directory", None)) + ); + assert_eq!( + VirtualPath("/directory/file".to_string()).file_name_and_extension(), + Some(("file", None)) + ); + assert_eq!( + VirtualPath("/directory/.file".to_string()).file_name_and_extension(), + Some((".file", None)) + ); + assert_eq!( + VirtualPath("/directory/.file.rs".to_string()).file_name_and_extension(), + Some((".file", Some("rs"))) + ); + assert_eq!( + VirtualPath("/directory/file.rs".to_string()).file_name_and_extension(), + Some(("file", Some("rs"))) + ); + } +} -- cgit v1.2.3 From 9863798480aa9642e31bfd41ee899d2e7329b5e5 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Thu, 10 Sep 2020 01:45:49 +0300 Subject: Rename the method to avoid false promises --- crates/vfs/src/vfs_path.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'crates/vfs/src') diff --git a/crates/vfs/src/vfs_path.rs b/crates/vfs/src/vfs_path.rs index dac8393ea..022a0be1e 100644 --- a/crates/vfs/src/vfs_path.rs +++ b/crates/vfs/src/vfs_path.rs @@ -57,13 +57,13 @@ impl VfsPath { } } - pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> { + pub fn name_and_extension(&self) -> Option<(&str, Option<&str>)> { match &self.0 { 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(), + VfsPathRepr::VirtualPath(p) => p.name_and_extension(), } } @@ -287,9 +287,7 @@ impl VirtualPath { Some(res) } - // FIXME: Currently VirtualPath does is unable to distinguish a directory from a file - // hence this method will return `Some("directory_name", None)` for a directory - pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> { + pub fn name_and_extension(&self) -> Option<(&str, Option<&str>)> { let file_path = if self.0.ends_with('/') { &self.0[..&self.0.len() - 1] } else { &self.0 }; let file_name = match file_path.rfind('/') { Some(position) => &file_path[position + 1..], @@ -318,29 +316,29 @@ mod tests { #[test] fn virtual_path_extensions() { - assert_eq!(VirtualPath("/".to_string()).file_name_and_extension(), None); + assert_eq!(VirtualPath("/".to_string()).name_and_extension(), None); assert_eq!( - VirtualPath("/directory".to_string()).file_name_and_extension(), + VirtualPath("/directory".to_string()).name_and_extension(), Some(("directory", None)) ); assert_eq!( - VirtualPath("/directory/".to_string()).file_name_and_extension(), + VirtualPath("/directory/".to_string()).name_and_extension(), Some(("directory", None)) ); assert_eq!( - VirtualPath("/directory/file".to_string()).file_name_and_extension(), + VirtualPath("/directory/file".to_string()).name_and_extension(), Some(("file", None)) ); assert_eq!( - VirtualPath("/directory/.file".to_string()).file_name_and_extension(), + VirtualPath("/directory/.file".to_string()).name_and_extension(), Some((".file", None)) ); assert_eq!( - VirtualPath("/directory/.file.rs".to_string()).file_name_and_extension(), + VirtualPath("/directory/.file.rs".to_string()).name_and_extension(), Some((".file", Some("rs"))) ); assert_eq!( - VirtualPath("/directory/file.rs".to_string()).file_name_and_extension(), + VirtualPath("/directory/file.rs".to_string()).name_and_extension(), Some(("file", Some("rs"))) ); } -- cgit v1.2.3