]> git.lizzy.rs Git - rust.git/blob - crates/ra_hir_def/src/nameres/mod_resolution.rs
Merge #4899
[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::DefDatabase, HirFileId};
7
8 #[derive(Clone, Debug)]
9 pub(super) 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(super) fn root() -> ModDir {
20         ModDir { path: RelativePathBuf::default(), root_non_dir_owner: false }
21     }
22
23     pub(super) fn descend_into_definition(
24         &self,
25         name: &Name,
26         attr_path: Option<&SmolStr>,
27     ) -> ModDir {
28         let mut path = self.path.clone();
29         match attr_to_path(attr_path) {
30             None => path.push(&name.to_string()),
31             Some(attr_path) => {
32                 if self.root_non_dir_owner {
33                     assert!(path.pop());
34                 }
35                 path.push(attr_path);
36             }
37         }
38         ModDir { path, root_non_dir_owner: false }
39     }
40
41     pub(super) fn resolve_declaration(
42         &self,
43         db: &dyn DefDatabase,
44         file_id: HirFileId,
45         name: &Name,
46         attr_path: Option<&SmolStr>,
47     ) -> Result<(FileId, ModDir), String> {
48         let file_id = file_id.original_file(db.upcast());
49
50         let mut candidate_files = Vec::new();
51         match attr_to_path(attr_path) {
52             Some(attr_path) => {
53                 let base =
54                     if self.root_non_dir_owner { self.path.parent().unwrap() } else { &self.path };
55                 candidate_files.push(base.join(attr_path).to_string())
56             }
57             None => {
58                 candidate_files.push(self.path.join(&format!("{}.rs", name)).to_string());
59                 candidate_files.push(self.path.join(&format!("{}/mod.rs", name)).to_string());
60             }
61         };
62
63         for candidate in candidate_files.iter() {
64             if let Some(file_id) = db.resolve_path(file_id, candidate.as_str()) {
65                 let mut root_non_dir_owner = false;
66                 let mut mod_path = RelativePathBuf::new();
67                 if !(candidate.ends_with("mod.rs") || attr_path.is_some()) {
68                     root_non_dir_owner = true;
69                     mod_path.push(&name.to_string());
70                 }
71                 return Ok((file_id, ModDir { path: mod_path, root_non_dir_owner }));
72             }
73         }
74         Err(candidate_files.remove(0))
75     }
76 }
77
78 fn attr_to_path(attr: Option<&SmolStr>) -> Option<RelativePathBuf> {
79     attr.and_then(|it| RelativePathBuf::from_path(&it.replace("\\", "/")).ok())
80 }