mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 13:18:47 +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_files: &FileSet,
|
||||||
module_file: FileId,
|
module_file: FileId,
|
||||||
) -> Option<Vec<(FileId, String)>> {
|
) -> 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)? {
|
match module_files.file_name_and_extension(module_file)? {
|
||||||
("mod", "rs") | ("lib", "rs") => Some(directory_with_module_file),
|
("mod", Some("rs")) | ("lib", Some("rs")) => {
|
||||||
(directory_with_module_name, "rs") => module_files
|
module_files.list_files(module_file, None)
|
||||||
.resolve_path(directory_with_module_file, directory_with_module_name),
|
}
|
||||||
|
(directory_with_module_name, Some("rs")) => module_files
|
||||||
|
.list_files(module_file, Some(&format!("../{}/", directory_with_module_name))),
|
||||||
_ => None,
|
_ => None,
|
||||||
}?;
|
}
|
||||||
Some(module_files.list_files(directory_with_applicable_modules))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
possible_sudmobules_opt(&self.source_root(module_file).file_set, module_file)
|
possible_sudmobules_opt(&self.source_root(module_file).file_set, module_file)
|
||||||
|
|
|
@ -20,34 +20,38 @@ impl FileSet {
|
||||||
self.files.len()
|
self.files.len()
|
||||||
}
|
}
|
||||||
pub fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
|
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();
|
base.pop();
|
||||||
let path = dbg!(base).join(dbg!(path))?;
|
let path = base.join(path)?;
|
||||||
self.files.get(&path).copied()
|
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()
|
self.paths[&file].file_name_and_extension()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list_files(&self, directory: FileId) -> Vec<(FileId, String)> {
|
pub fn list_files(
|
||||||
// TODO kb determine the ways to list all applicable files
|
&self,
|
||||||
// Maybe leave list directory here only and the move the rest of the logic into the database impl?
|
anchor: FileId,
|
||||||
// cache results in Salsa?
|
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);
|
Some(
|
||||||
/*
|
self.paths
|
||||||
[crates/vfs/src/file_set.rs:30] directory = "/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/"
|
.iter()
|
||||||
[crates/vfs/src/file_set.rs:31] self.files.keys() = [
|
.filter(|(_, path)| path.starts_with(&anchor_directory))
|
||||||
"/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_1/test_mod_2/test_mod_3.rs",
|
// TODO kb need to ensure that no / exists after the anchor_directory
|
||||||
"/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_1/test_mod_2.rs",
|
.filter(|(_, path)| path.ends_with(".rs"))
|
||||||
"/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/test_mod_1.rs",
|
.map(|(&file_id, path)| (file_id, path.to_string()))
|
||||||
"/Users/someonetoignore/Downloads/tmp_dir/zzzz/src/lib.rs",
|
.collect(),
|
||||||
"/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 insert(&mut self, file_id: FileId, path: VfsPath) {
|
pub fn insert(&mut self, file_id: FileId, path: VfsPath) {
|
||||||
self.files.insert(path.clone(), file_id);
|
self.files.insert(path.clone(), file_id);
|
||||||
|
|
|
@ -48,13 +48,19 @@ impl VfsPath {
|
||||||
(VfsPathRepr::VirtualPath(_), _) => false,
|
(VfsPathRepr::VirtualPath(_), _) => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn ends_with(&self, suffix: &str) -> bool {
|
||||||
pub fn file_name_and_extension(&self) -> Option<(&str, &str)> {
|
|
||||||
match &self.0 {
|
match &self.0 {
|
||||||
VfsPathRepr::PathBuf(p) => p
|
VfsPathRepr::PathBuf(p) => p.ends_with(suffix),
|
||||||
.file_stem()
|
VfsPathRepr::VirtualPath(p) => p.ends_with(suffix),
|
||||||
.zip(p.extension())
|
}
|
||||||
.and_then(|(name, extension)| Some((name.to_str()?, extension.to_str()?))),
|
}
|
||||||
|
|
||||||
|
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(),
|
VfsPathRepr::VirtualPath(p) => p.file_name_and_extension(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -259,6 +265,9 @@ impl VirtualPath {
|
||||||
fn starts_with(&self, other: &VirtualPath) -> bool {
|
fn starts_with(&self, other: &VirtualPath) -> bool {
|
||||||
self.0.starts_with(&other.0)
|
self.0.starts_with(&other.0)
|
||||||
}
|
}
|
||||||
|
fn ends_with(&self, suffix: &str) -> bool {
|
||||||
|
self.0.ends_with(suffix)
|
||||||
|
}
|
||||||
fn pop(&mut self) -> bool {
|
fn pop(&mut self) -> bool {
|
||||||
let pos = match self.0.rfind('/') {
|
let pos = match self.0.rfind('/') {
|
||||||
Some(pos) => pos,
|
Some(pos) => pos,
|
||||||
|
@ -279,8 +288,8 @@ impl VirtualPath {
|
||||||
Some(res)
|
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
|
// TODO kb check if is a file
|
||||||
Some(("test_mod_1", "rs"))
|
Some(("test_mod_1", Some("rs")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue