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') 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