aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/nameres.rs1
-rw-r--r--crates/ra_hir/src/nameres/collector.rs9
-rw-r--r--crates/ra_hir/src/nameres/mod_resolution.rs80
3 files changed, 6 insertions, 84 deletions
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs
index 39f585b44..7c4d07de0 100644
--- a/crates/ra_hir/src/nameres.rs
+++ b/crates/ra_hir/src/nameres.rs
@@ -49,7 +49,6 @@
49 49
50mod per_ns; 50mod per_ns;
51mod collector; 51mod collector;
52mod mod_resolution;
53#[cfg(test)] 52#[cfg(test)]
54mod tests; 53mod tests;
55 54
diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs
index e2e13805a..ee0a4c99f 100644
--- a/crates/ra_hir/src/nameres/collector.rs
+++ b/crates/ra_hir/src/nameres/collector.rs
@@ -1,6 +1,9 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir_def::{attr::Attr, nameres::raw}; 3use hir_def::{
4 attr::Attr,
5 nameres::{mod_resolution::ModDir, raw},
6};
4use hir_expand::name; 7use hir_expand::name;
5use ra_cfg::CfgOptions; 8use ra_cfg::CfgOptions;
6use ra_db::FileId; 9use ra_db::FileId;
@@ -12,8 +15,8 @@ use crate::{
12 db::DefDatabase, 15 db::DefDatabase,
13 ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind}, 16 ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind},
14 nameres::{ 17 nameres::{
15 diagnostics::DefDiagnostic, mod_resolution::ModDir, Crate, CrateDefMap, CrateModuleId, 18 diagnostics::DefDiagnostic, Crate, CrateDefMap, CrateModuleId, ModuleData, ModuleDef,
16 ModuleData, ModuleDef, PerNs, ReachedFixedPoint, Resolution, ResolveMode, 19 PerNs, ReachedFixedPoint, Resolution, ResolveMode,
17 }, 20 },
18 Adt, AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind, Static, 21 Adt, AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind, Static,
19 Struct, Trait, TypeAlias, Union, 22 Struct, Trait, TypeAlias, Union,
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}