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/file_set.rs') 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 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'crates/vfs/src/file_set.rs') 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) { -- 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 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) (limited to 'crates/vfs/src/file_set.rs') 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); -- 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 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'crates/vfs/src/file_set.rs') 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(), ) } -- 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/file_set.rs') 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 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/file_set.rs') 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/file_set.rs') 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