]> git.lizzy.rs Git - rust.git/commitdiff
move mod_resolution to hir_def
authorAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 31 Oct 2019 07:31:29 +0000 (10:31 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 31 Oct 2019 07:31:29 +0000 (10:31 +0300)
crates/ra_hir/src/nameres.rs
crates/ra_hir/src/nameres/collector.rs
crates/ra_hir/src/nameres/mod_resolution.rs [deleted file]
crates/ra_hir_def/src/nameres.rs
crates/ra_hir_def/src/nameres/mod_resolution.rs [new file with mode: 0644]

index 39f585b446a9850726db321c6e575c7becdf5884..7c4d07de0da37d3b330da634b006114dc6102c3d 100644 (file)
@@ -49,7 +49,6 @@
 
 mod per_ns;
 mod collector;
-mod mod_resolution;
 #[cfg(test)]
 mod tests;
 
index e2e13805acc07a17a32aae199b9807ca284aa6ad..ee0a4c99fe00295a392110c4bfbbe5cdc3657a57 100644 (file)
@@ -1,6 +1,9 @@
 //! FIXME: write short doc here
 
-use hir_def::{attr::Attr, nameres::raw};
+use hir_def::{
+    attr::Attr,
+    nameres::{mod_resolution::ModDir, raw},
+};
 use hir_expand::name;
 use ra_cfg::CfgOptions;
 use ra_db::FileId;
@@ -12,8 +15,8 @@
     db::DefDatabase,
     ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind},
     nameres::{
-        diagnostics::DefDiagnostic, mod_resolution::ModDir, Crate, CrateDefMap, CrateModuleId,
-        ModuleData, ModuleDef, PerNs, ReachedFixedPoint, Resolution, ResolveMode,
+        diagnostics::DefDiagnostic, Crate, CrateDefMap, CrateModuleId, ModuleData, ModuleDef,
+        PerNs, ReachedFixedPoint, Resolution, ResolveMode,
     },
     Adt, AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind, Static,
     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 (file)
index 334cdd6..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-//! This module resolves `mod foo;` declaration to file.
-use ra_db::FileId;
-use ra_syntax::SmolStr;
-use relative_path::RelativePathBuf;
-
-use crate::{db::DefDatabase, HirFileId, Name};
-
-#[derive(Clone, Debug)]
-pub(super) struct ModDir {
-    /// `.` 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,
-}
-
-impl ModDir {
-    pub(super) fn root() -> ModDir {
-        ModDir { path: RelativePathBuf::default(), root_non_dir_owner: false }
-    }
-
-    pub(super) fn descend_into_definition(
-        &self,
-        name: &Name,
-        attr_path: Option<&SmolStr>,
-    ) -> ModDir {
-        let mut path = self.path.clone();
-        match attr_to_path(attr_path) {
-            None => path.push(&name.to_string()),
-            Some(attr_path) => {
-                if self.root_non_dir_owner {
-                    assert!(path.pop());
-                }
-                path.push(attr_path);
-            }
-        }
-        ModDir { path, root_non_dir_owner: false }
-    }
-
-    pub(super) fn resolve_declaration(
-        &self,
-        db: &impl DefDatabase,
-        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();
-        match attr_to_path(attr_path) {
-            Some(attr_path) => {
-                let base =
-                    if self.root_non_dir_owner { self.path.parent().unwrap() } else { &self.path };
-                candidate_files.push(base.join(attr_path))
-            }
-            None => {
-                candidate_files.push(self.path.join(&format!("{}.rs", name)));
-                candidate_files.push(self.path.join(&format!("{}/mod.rs", name)));
-            }
-        };
-
-        for candidate in candidate_files.iter() {
-            if let Some(file_id) = db.resolve_relative_path(file_id, candidate) {
-                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 }));
-            }
-        }
-        Err(candidate_files.remove(0))
-    }
-}
-
-fn attr_to_path(attr: Option<&SmolStr>) -> Option<RelativePathBuf> {
-    attr.and_then(|it| RelativePathBuf::from_path(&it.replace("\\", "/")).ok())
-}
index 5893708e8ba0ef98ce996d1241bc417d0212af53..11ba8a7770d9f2af71ab311a9747fc5ee52494b7 100644 (file)
@@ -1,3 +1,5 @@
 //! FIXME: write short doc here
 
+// FIXME: review privacy of submodules
 pub mod raw;
+pub mod mod_resolution;
diff --git a/crates/ra_hir_def/src/nameres/mod_resolution.rs b/crates/ra_hir_def/src/nameres/mod_resolution.rs
new file mode 100644 (file)
index 0000000..7d7e277
--- /dev/null
@@ -0,0 +1,77 @@
+//! This module resolves `mod foo;` declaration to file.
+use hir_expand::name::Name;
+use ra_db::FileId;
+use ra_syntax::SmolStr;
+use relative_path::RelativePathBuf;
+
+use crate::{db::DefDatabase2, HirFileId};
+
+#[derive(Clone, Debug)]
+pub struct ModDir {
+    /// `.` 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,
+}
+
+impl ModDir {
+    pub fn root() -> ModDir {
+        ModDir { path: RelativePathBuf::default(), root_non_dir_owner: false }
+    }
+
+    pub fn descend_into_definition(&self, name: &Name, attr_path: Option<&SmolStr>) -> ModDir {
+        let mut path = self.path.clone();
+        match attr_to_path(attr_path) {
+            None => path.push(&name.to_string()),
+            Some(attr_path) => {
+                if self.root_non_dir_owner {
+                    assert!(path.pop());
+                }
+                path.push(attr_path);
+            }
+        }
+        ModDir { path, root_non_dir_owner: false }
+    }
+
+    pub fn resolve_declaration(
+        &self,
+        db: &impl DefDatabase2,
+        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();
+        match attr_to_path(attr_path) {
+            Some(attr_path) => {
+                let base =
+                    if self.root_non_dir_owner { self.path.parent().unwrap() } else { &self.path };
+                candidate_files.push(base.join(attr_path))
+            }
+            None => {
+                candidate_files.push(self.path.join(&format!("{}.rs", name)));
+                candidate_files.push(self.path.join(&format!("{}/mod.rs", name)));
+            }
+        };
+
+        for candidate in candidate_files.iter() {
+            if let Some(file_id) = db.resolve_relative_path(file_id, candidate) {
+                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 }));
+            }
+        }
+        Err(candidate_files.remove(0))
+    }
+}
+
+fn attr_to_path(attr: Option<&SmolStr>) -> Option<RelativePathBuf> {
+    attr.and_then(|it| RelativePathBuf::from_path(&it.replace("\\", "/")).ok())
+}