diff options
author | Kirill Bulatov <[email protected]> | 2020-09-03 23:32:06 +0100 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2020-09-09 23:42:20 +0100 |
commit | 0de71f7bc9482c9d1ef7e9d36ec5d6c5fd378781 (patch) | |
tree | 476d805b91fde4be5801c50dbb696b49eb92abbc /crates/vfs | |
parent | 17870a3e2c39770a99f9ab5ce090abbe1dc334d2 (diff) |
Properly use FileSet API
Diffstat (limited to 'crates/vfs')
-rw-r--r-- | crates/vfs/src/file_set.rs | 46 | ||||
-rw-r--r-- | crates/vfs/src/vfs_path.rs | 23 |
2 files changed, 41 insertions, 28 deletions
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 { | |||
20 | self.files.len() | 20 | self.files.len() |
21 | } | 21 | } |
22 | pub fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> { | 22 | pub fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> { |
23 | let mut base = dbg!(self.paths[&anchor].clone()); | 23 | let mut base = self.paths[&anchor].clone(); |
24 | base.pop(); | 24 | base.pop(); |
25 | let path = dbg!(base).join(dbg!(path))?; | 25 | let path = base.join(path)?; |
26 | self.files.get(&path).copied() | 26 | self.files.get(&path).copied() |
27 | } | 27 | } |
28 | 28 | ||
29 | pub fn file_name_and_extension(&self, file: FileId) -> Option<(&str, &str)> { | 29 | pub fn file_name_and_extension(&self, file: FileId) -> Option<(&str, Option<&str>)> { |
30 | self.paths[&file].file_name_and_extension() | 30 | self.paths[&file].file_name_and_extension() |
31 | } | 31 | } |
32 | 32 | ||
33 | pub fn list_files(&self, directory: FileId) -> Vec<(FileId, String)> { | 33 | pub fn list_files( |
34 | // TODO kb determine the ways to list all applicable files | 34 | &self, |
35 | // Maybe leave list directory here only and the move the rest of the logic into the database impl? | 35 | anchor: FileId, |
36 | // cache results in Salsa? | 36 | anchor_relative_path: Option<&str>, |
37 | 37 | ) -> Option<Vec<(FileId, String)>> { | |
38 | dbg!(directory); | 38 | let anchor_directory = { |
39 | /* | 39 | let path = self.paths[&anchor].clone(); |
40 | [crates/vfs/src/file_set.rs:30] directory = "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/" | 40 | match anchor_relative_path { |
41 | [crates/vfs/src/file_set.rs:31] self.files.keys() = [ | 41 | Some(anchor_relative_path) => path.join(anchor_relative_path), |
42 | "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_1/test_mod_2/test_mod_3.rs", | 42 | None => path.join("../"), |
43 | "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_1/test_mod_2.rs", | 43 | } |
44 | "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_1.rs", | 44 | }?; |
45 | "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/lib.rs", | 45 | |
46 | "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_3/test_mod_3_1.rs", | 46 | Some( |
47 | "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_3.rs", | 47 | self.paths |
48 | ] | 48 | .iter() |
49 | */ | 49 | .filter(|(_, path)| path.starts_with(&anchor_directory)) |
50 | Vec::new() | 50 | // TODO kb need to ensure that no / exists after the anchor_directory |
51 | .filter(|(_, path)| path.ends_with(".rs")) | ||
52 | .map(|(&file_id, path)| (file_id, path.to_string())) | ||
53 | .collect(), | ||
54 | ) | ||
51 | } | 55 | } |
52 | pub fn insert(&mut self, file_id: FileId, path: VfsPath) { | 56 | pub fn insert(&mut self, file_id: FileId, path: VfsPath) { |
53 | self.files.insert(path.clone(), file_id); | 57 | 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 { | |||
48 | (VfsPathRepr::VirtualPath(_), _) => false, | 48 | (VfsPathRepr::VirtualPath(_), _) => false, |
49 | } | 49 | } |
50 | } | 50 | } |
51 | pub fn ends_with(&self, suffix: &str) -> bool { | ||
52 | match &self.0 { | ||
53 | VfsPathRepr::PathBuf(p) => p.ends_with(suffix), | ||
54 | VfsPathRepr::VirtualPath(p) => p.ends_with(suffix), | ||
55 | } | ||
56 | } | ||
51 | 57 | ||
52 | pub fn file_name_and_extension(&self) -> Option<(&str, &str)> { | 58 | pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> { |
53 | match &self.0 { | 59 | match &self.0 { |
54 | VfsPathRepr::PathBuf(p) => p | 60 | VfsPathRepr::PathBuf(p) => Some(( |
55 | .file_stem() | 61 | p.file_stem()?.to_str()?, |
56 | .zip(p.extension()) | 62 | p.extension().and_then(|extension| extension.to_str()), |
57 | .and_then(|(name, extension)| Some((name.to_str()?, extension.to_str()?))), | 63 | )), |
58 | VfsPathRepr::VirtualPath(p) => p.file_name_and_extension(), | 64 | VfsPathRepr::VirtualPath(p) => p.file_name_and_extension(), |
59 | } | 65 | } |
60 | } | 66 | } |
@@ -259,6 +265,9 @@ impl VirtualPath { | |||
259 | fn starts_with(&self, other: &VirtualPath) -> bool { | 265 | fn starts_with(&self, other: &VirtualPath) -> bool { |
260 | self.0.starts_with(&other.0) | 266 | self.0.starts_with(&other.0) |
261 | } | 267 | } |
268 | fn ends_with(&self, suffix: &str) -> bool { | ||
269 | self.0.ends_with(suffix) | ||
270 | } | ||
262 | fn pop(&mut self) -> bool { | 271 | fn pop(&mut self) -> bool { |
263 | let pos = match self.0.rfind('/') { | 272 | let pos = match self.0.rfind('/') { |
264 | Some(pos) => pos, | 273 | Some(pos) => pos, |
@@ -279,8 +288,8 @@ impl VirtualPath { | |||
279 | Some(res) | 288 | Some(res) |
280 | } | 289 | } |
281 | 290 | ||
282 | pub fn file_name_and_extension(&self) -> Option<(&str, &str)> { | 291 | pub fn file_name_and_extension(&self) -> Option<(&str, Option<&str>)> { |
283 | // TODO kb check if is a file | 292 | // TODO kb check if is a file |
284 | Some(("test_mod_1", "rs")) | 293 | Some(("test_mod_1", Some("rs"))) |
285 | } | 294 | } |
286 | } | 295 | } |