]> git.lizzy.rs Git - rust.git/blob - crates/ra_hir_def/src/nameres/mod_resolution.rs
Merge #2173
[rust.git] / crates / ra_hir_def / src / nameres / mod_resolution.rs
1 //! This module resolves `mod foo;` declaration to file.
2 use hir_expand::name::Name;
3 use ra_db::{FileId, RelativePathBuf};
4 use ra_syntax::SmolStr;
5
6 use crate::{db::DefDatabase2, HirFileId};
7
8 #[derive(Clone, Debug)]
9 pub struct ModDir {
10     /// `.` for `mod.rs`, `lib.rs`
11     /// `./foo` for `foo.rs`
12     /// `./foo/bar` for `mod bar { mod x; }` nested in `foo.rs`
13     path: RelativePathBuf,
14     /// inside `./foo.rs`, mods with `#[path]` should *not* be relative to `./foo/`
15     root_non_dir_owner: bool,
16 }
17
18 impl ModDir {
19     pub fn root() -> ModDir {
20         ModDir { path: RelativePathBuf::default(), root_non_dir_owner: false }
21     }
22
23     pub fn descend_into_definition(&self, name: &Name, attr_path: Option<&SmolStr>) -> ModDir {
24         let mut path = self.path.clone();
25         match attr_to_path(attr_path) {
26             None => path.push(&name.to_string()),
27             Some(attr_path) => {
28                 if self.root_non_dir_owner {
29                     assert!(path.pop());
30                 }
31                 path.push(attr_path);
32             }
33         }
34         ModDir { path, root_non_dir_owner: false }
35     }
36
37     pub fn resolve_declaration(
38         &self,
39         db: &impl DefDatabase2,
40         file_id: HirFileId,
41         name: &Name,
42         attr_path: Option<&SmolStr>,
43     ) -> Result<(FileId, ModDir), RelativePathBuf> {
44         let file_id = file_id.original_file(db);
45
46         let mut candidate_files = Vec::new();
47         match attr_to_path(attr_path) {
48             Some(attr_path) => {
49                 let base =
50                     if self.root_non_dir_owner { self.path.parent().unwrap() } else { &self.path };
51                 candidate_files.push(base.join(attr_path))
52             }
53             None => {
54                 candidate_files.push(self.path.join(&format!("{}.rs", name)));
55                 candidate_files.push(self.path.join(&format!("{}/mod.rs", name)));
56             }
57         };
58
59         for candidate in candidate_files.iter() {
60             if let Some(file_id) = db.resolve_relative_path(file_id, candidate) {
61                 let mut root_non_dir_owner = false;
62                 let mut mod_path = RelativePathBuf::new();
63                 if !(candidate.ends_with("mod.rs") || attr_path.is_some()) {
64                     root_non_dir_owner = true;
65                     mod_path.push(&name.to_string());
66                 }
67                 return Ok((file_id, ModDir { path: mod_path, root_non_dir_owner }));
68             }
69         }
70         Err(candidate_files.remove(0))
71     }
72 }
73
74 fn attr_to_path(attr: Option<&SmolStr>) -> Option<RelativePathBuf> {
75     attr.and_then(|it| RelativePathBuf::from_path(&it.replace("\\", "/")).ok())
76 }