aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/nameres/mod_resolution.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/nameres/mod_resolution.rs')
-rw-r--r--crates/ra_hir/src/nameres/mod_resolution.rs80
1 files changed, 0 insertions, 80 deletions
diff --git a/crates/ra_hir/src/nameres/mod_resolution.rs b/crates/ra_hir/src/nameres/mod_resolution.rs
deleted file mode 100644
index 334cdd692..000000000
--- a/crates/ra_hir/src/nameres/mod_resolution.rs
+++ /dev/null
@@ -1,80 +0,0 @@
1//! This module resolves `mod foo;` declaration to file.
2use ra_db::FileId;
3use ra_syntax::SmolStr;
4use relative_path::RelativePathBuf;
5
6use crate::{db::DefDatabase, HirFileId, Name};
7
8#[derive(Clone, Debug)]
9pub(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
18impl 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: &impl DefDatabase,
44 file_id: HirFileId,
45 name: &Name,
46 attr_path: Option<&SmolStr>,
47 ) -> Result<(FileId, ModDir), RelativePathBuf> {
48 let file_id = file_id.original_file(db);
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))
56 }
57 None => {
58 candidate_files.push(self.path.join(&format!("{}.rs", name)));
59 candidate_files.push(self.path.join(&format!("{}/mod.rs", name)));
60 }
61 };
62
63 for candidate in candidate_files.iter() {
64 if let Some(file_id) = db.resolve_relative_path(file_id, candidate) {
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
78fn attr_to_path(attr: Option<&SmolStr>) -> Option<RelativePathBuf> {
79 attr.and_then(|it| RelativePathBuf::from_path(&it.replace("\\", "/")).ok())
80}