mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 05:08:52 +00:00
Properly use FileSet API
This commit is contained in:
parent
17870a3e2c
commit
0de71f7bc9
3 changed files with 49 additions and 38 deletions
|
@ -171,16 +171,14 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
|
|||
module_files: &FileSet,
|
||||
module_file: FileId,
|
||||
) -> Option<Vec<(FileId, String)>> {
|
||||
// TODO kb resolve path thinks that the input is a file...
|
||||
let directory_with_module_file = module_files.resolve_path(module_file, "/../")?;
|
||||
let directory_with_applicable_modules =
|
||||
match module_files.file_name_and_extension(module_file)? {
|
||||
("mod", "rs") | ("lib", "rs") => Some(directory_with_module_file),
|
||||
(directory_with_module_name, "rs") => module_files
|
||||
.resolve_path(directory_with_module_file, directory_with_module_name),
|
||||
_ => None,
|
||||
}?;
|
||||
Some(module_files.list_files(directory_with_applicable_modules))
|
||||
match module_files.file_name_and_extension(module_file)? {
|
||||
("mod", Some("rs")) | ("lib", Some("rs")) => {
|
||||
module_files.list_files(module_file, None)
|
||||
}
|
||||
(directory_with_module_name, Some("rs")) => module_files
|
||||
.list_files(module_file, Some(&format!("../{}/", directory_with_module_name))),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
possible_sudmobules_opt(&self.source_root(module_file).file_set, module_file)
|
||||
|
|
|
@ -20,34 +20,38 @@ impl FileSet {
|
|||
self.files.len()
|
||||
}
|
||||
pub fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
|
||||
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?
|
||||
pub fn list_files(
|
||||
&self,
|
||||
anchor: FileId,
|
||||
anchor_relative_path: Option<&str>,
|
||||
) -> Option<Vec<(FileId, String)>> {
|
||||
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("../"),
|
||||
}
|
||||
}?;
|
||||
|
||||
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()
|
||||
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);
|
||||
|
|
|
@ -48,13 +48,19 @@ impl VfsPath {
|
|||
(VfsPathRepr::VirtualPath(_), _) => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file_name_and_extension(&self) -> Option<(&str, &str)> {
|
||||
pub fn ends_with(&self, suffix: &str) -> bool {
|
||||
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) => p.ends_with(suffix),
|
||||
VfsPathRepr::VirtualPath(p) => p.ends_with(suffix),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file_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(),
|
||||
}
|
||||
}
|
||||
|
@ -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")))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue