2019-09-06 06:30:24 -05:00
|
|
|
//! This module resolves `mod foo;` declaration to file.
|
2019-10-31 02:31:29 -05:00
|
|
|
use hir_expand::name::Name;
|
2019-11-03 16:14:17 -06:00
|
|
|
use ra_db::{FileId, RelativePathBuf};
|
2019-09-05 12:27:10 -05:00
|
|
|
use ra_syntax::SmolStr;
|
|
|
|
|
2019-10-31 02:31:29 -05:00
|
|
|
use crate::{db::DefDatabase2, HirFileId};
|
2019-09-05 12:27:10 -05:00
|
|
|
|
2019-10-10 06:45:05 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2019-10-31 02:31:29 -05:00
|
|
|
pub struct ModDir {
|
2019-10-10 06:45:05 -05:00
|
|
|
/// `.` for `mod.rs`, `lib.rs`
|
|
|
|
/// `./foo` for `foo.rs`
|
|
|
|
/// `./foo/bar` for `mod bar { mod x; }` nested in `foo.rs`
|
|
|
|
path: RelativePathBuf,
|
|
|
|
/// inside `./foo.rs`, mods with `#[path]` should *not* be relative to `./foo/`
|
|
|
|
root_non_dir_owner: bool,
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
|
|
|
|
2019-10-10 06:45:05 -05:00
|
|
|
impl ModDir {
|
2019-10-31 02:31:29 -05:00
|
|
|
pub fn root() -> ModDir {
|
2019-10-10 06:45:05 -05:00
|
|
|
ModDir { path: RelativePathBuf::default(), root_non_dir_owner: false }
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
|
|
|
|
2019-10-31 02:31:29 -05:00
|
|
|
pub fn descend_into_definition(&self, name: &Name, attr_path: Option<&SmolStr>) -> ModDir {
|
2019-10-10 06:45:05 -05:00
|
|
|
let mut path = self.path.clone();
|
2019-10-09 06:27:37 -05:00
|
|
|
match attr_to_path(attr_path) {
|
2019-10-10 06:45:05 -05:00
|
|
|
None => path.push(&name.to_string()),
|
|
|
|
Some(attr_path) => {
|
|
|
|
if self.root_non_dir_owner {
|
2019-10-27 04:36:40 -05:00
|
|
|
assert!(path.pop());
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
2019-10-09 06:27:37 -05:00
|
|
|
path.push(attr_path);
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
|
|
|
}
|
2019-10-10 06:45:05 -05:00
|
|
|
ModDir { path, root_non_dir_owner: false }
|
|
|
|
}
|
|
|
|
|
2019-10-31 02:31:29 -05:00
|
|
|
pub fn resolve_declaration(
|
2019-10-10 06:45:05 -05:00
|
|
|
&self,
|
2019-10-31 02:31:29 -05:00
|
|
|
db: &impl DefDatabase2,
|
2019-10-10 06:45:05 -05:00
|
|
|
file_id: HirFileId,
|
|
|
|
name: &Name,
|
|
|
|
attr_path: Option<&SmolStr>,
|
|
|
|
) -> Result<(FileId, ModDir), RelativePathBuf> {
|
|
|
|
let file_id = file_id.original_file(db);
|
|
|
|
|
|
|
|
let mut candidate_files = Vec::new();
|
2019-10-09 06:27:37 -05:00
|
|
|
match attr_to_path(attr_path) {
|
|
|
|
Some(attr_path) => {
|
2019-10-27 04:36:40 -05:00
|
|
|
let base =
|
|
|
|
if self.root_non_dir_owner { self.path.parent().unwrap() } else { &self.path };
|
2019-10-09 06:27:37 -05:00
|
|
|
candidate_files.push(base.join(attr_path))
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
|
|
|
None => {
|
2019-10-09 06:27:37 -05:00
|
|
|
candidate_files.push(self.path.join(&format!("{}.rs", name)));
|
|
|
|
candidate_files.push(self.path.join(&format!("{}/mod.rs", name)));
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
2019-10-10 06:45:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
for candidate in candidate_files.iter() {
|
2019-10-09 06:27:37 -05:00
|
|
|
if let Some(file_id) = db.resolve_relative_path(file_id, candidate) {
|
2019-10-10 06:45:05 -05:00
|
|
|
let mut root_non_dir_owner = false;
|
|
|
|
let mut mod_path = RelativePathBuf::new();
|
|
|
|
if !(candidate.ends_with("mod.rs") || attr_path.is_some()) {
|
|
|
|
root_non_dir_owner = true;
|
|
|
|
mod_path.push(&name.to_string());
|
|
|
|
}
|
|
|
|
return Ok((file_id, ModDir { path: mod_path, root_non_dir_owner }));
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
|
|
|
}
|
2019-10-09 06:27:37 -05:00
|
|
|
Err(candidate_files.remove(0))
|
2019-10-10 06:45:05 -05:00
|
|
|
}
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|
|
|
|
|
2019-10-09 06:27:37 -05:00
|
|
|
fn attr_to_path(attr: Option<&SmolStr>) -> Option<RelativePathBuf> {
|
|
|
|
attr.and_then(|it| RelativePathBuf::from_path(&it.replace("\\", "/")).ok())
|
2019-09-05 12:27:10 -05:00
|
|
|
}
|