aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-09-03 23:32:06 +0100
committerKirill Bulatov <[email protected]>2020-09-09 23:42:20 +0100
commit0de71f7bc9482c9d1ef7e9d36ec5d6c5fd378781 (patch)
tree476d805b91fde4be5801c50dbb696b49eb92abbc /crates
parent17870a3e2c39770a99f9ab5ce090abbe1dc334d2 (diff)
Properly use FileSet API
Diffstat (limited to 'crates')
-rw-r--r--crates/base_db/src/lib.rs18
-rw-r--r--crates/vfs/src/file_set.rs46
-rw-r--r--crates/vfs/src/vfs_path.rs23
3 files changed, 49 insertions, 38 deletions
diff --git a/crates/base_db/src/lib.rs b/crates/base_db/src/lib.rs
index 37a8432bd..1bc4690c9 100644
--- a/crates/base_db/src/lib.rs
+++ b/crates/base_db/src/lib.rs
@@ -171,16 +171,14 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
171 module_files: &FileSet, 171 module_files: &FileSet,
172 module_file: FileId, 172 module_file: FileId,
173 ) -> Option<Vec<(FileId, String)>> { 173 ) -> Option<Vec<(FileId, String)>> {
174 // TODO kb resolve path thinks that the input is a file... 174 match module_files.file_name_and_extension(module_file)? {
175 let directory_with_module_file = module_files.resolve_path(module_file, "/../")?; 175 ("mod", Some("rs")) | ("lib", Some("rs")) => {
176 let directory_with_applicable_modules = 176 module_files.list_files(module_file, None)
177 match module_files.file_name_and_extension(module_file)? { 177 }
178 ("mod", "rs") | ("lib", "rs") => Some(directory_with_module_file), 178 (directory_with_module_name, Some("rs")) => module_files
179 (directory_with_module_name, "rs") => module_files 179 .list_files(module_file, Some(&format!("../{}/", directory_with_module_name))),
180 .resolve_path(directory_with_module_file, directory_with_module_name), 180 _ => None,
181 _ => None, 181 }
182 }?;
183 Some(module_files.list_files(directory_with_applicable_modules))
184 } 182 }
185 183
186 possible_sudmobules_opt(&self.source_root(module_file).file_set, module_file) 184 possible_sudmobules_opt(&self.source_root(module_file).file_set, module_file)
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}