diff options
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 10 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model_impl.rs | 19 | ||||
-rw-r--r-- | crates/ra_hir/src/module/nameres/tests.rs | 4 |
3 files changed, 29 insertions, 4 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 63e2e34e8..2f968d97c 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -1,4 +1,5 @@ | |||
1 | use ra_db::{CrateId, Cancelable}; | 1 | use ra_db::{CrateId, Cancelable, FileId}; |
2 | use ra_syntax::ast; | ||
2 | 3 | ||
3 | use crate::{Name, db::HirDatabase, DefId}; | 4 | use crate::{Name, db::HirDatabase, DefId}; |
4 | 5 | ||
@@ -17,6 +18,9 @@ pub struct CrateDependency { | |||
17 | } | 18 | } |
18 | 19 | ||
19 | impl Crate { | 20 | impl Crate { |
21 | pub fn crate_id(&self) -> CrateId { | ||
22 | self.crate_id | ||
23 | } | ||
20 | pub fn dependencies(&self, db: &impl HirDatabase) -> Vec<CrateDependency> { | 24 | pub fn dependencies(&self, db: &impl HirDatabase) -> Vec<CrateDependency> { |
21 | self.dependencies_impl(db) | 25 | self.dependencies_impl(db) |
22 | } | 26 | } |
@@ -31,6 +35,10 @@ pub struct Module { | |||
31 | } | 35 | } |
32 | 36 | ||
33 | impl Module { | 37 | impl Module { |
38 | pub fn source(&self, db: &impl HirDatabase) -> (FileId, Option<ast::ModuleNode>) { | ||
39 | self.source_impl(db) | ||
40 | } | ||
41 | |||
34 | /// Returns the crate this module is part of. | 42 | /// Returns the crate this module is part of. |
35 | pub fn krate(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { | 43 | pub fn krate(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { |
36 | self.krate_impl(db) | 44 | self.krate_impl(db) |
diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs index 659af548c..83ee8186e 100644 --- a/crates/ra_hir/src/code_model_impl.rs +++ b/crates/ra_hir/src/code_model_impl.rs | |||
@@ -1,4 +1,5 @@ | |||
1 | use ra_db::{CrateId, Cancelable}; | 1 | use ra_db::{CrateId, Cancelable, FileId}; |
2 | use ra_syntax::{AstNode, ast}; | ||
2 | 3 | ||
3 | use crate::{HirFileId, db::HirDatabase, Crate, CrateDependency, AsName, DefId, DefLoc, DefKind, Name}; | 4 | use crate::{HirFileId, db::HirDatabase, Crate, CrateDependency, AsName, DefId, DefLoc, DefKind, Name}; |
4 | 5 | ||
@@ -48,6 +49,22 @@ impl Module { | |||
48 | crate::code_model_api::Module { def_id } | 49 | crate::code_model_api::Module { def_id } |
49 | } | 50 | } |
50 | 51 | ||
52 | pub(crate) fn source_impl(&self, db: &impl HirDatabase) -> (FileId, Option<ast::ModuleNode>) { | ||
53 | let loc = self.def_id.loc(db); | ||
54 | let source_item_id = loc.source_item_id; | ||
55 | let module = match source_item_id.item_id { | ||
56 | None => None, | ||
57 | Some(_) => { | ||
58 | let syntax_node = db.file_item(source_item_id); | ||
59 | let module = ast::Module::cast(syntax_node.borrowed()).unwrap().owned(); | ||
60 | Some(module) | ||
61 | } | ||
62 | }; | ||
63 | // FIXME: remove `as_original_file` here | ||
64 | let file_id = source_item_id.file_id.as_original_file(); | ||
65 | (file_id, module) | ||
66 | } | ||
67 | |||
51 | pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { | 68 | pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { |
52 | let root = self.crate_root(db)?; | 69 | let root = self.crate_root(db)?; |
53 | let loc = root.def_id.loc(db); | 70 | let loc = root.def_id.loc(db); |
diff --git a/crates/ra_hir/src/module/nameres/tests.rs b/crates/ra_hir/src/module/nameres/tests.rs index a6a0bea31..dcbe65aec 100644 --- a/crates/ra_hir/src/module/nameres/tests.rs +++ b/crates/ra_hir/src/module/nameres/tests.rs | |||
@@ -17,7 +17,7 @@ fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) { | |||
17 | let module = hir::source_binder::module_from_position(&db, pos) | 17 | let module = hir::source_binder::module_from_position(&db, pos) |
18 | .unwrap() | 18 | .unwrap() |
19 | .unwrap(); | 19 | .unwrap(); |
20 | let module_id = module.module_id; | 20 | let module_id = module.def_id.loc(&db).module_id; |
21 | (db.item_map(source_root).unwrap(), module_id) | 21 | (db.item_map(source_root).unwrap(), module_id) |
22 | } | 22 | } |
23 | 23 | ||
@@ -155,7 +155,7 @@ fn item_map_across_crates() { | |||
155 | let module = hir::source_binder::module_from_file_id(&db, main_id) | 155 | let module = hir::source_binder::module_from_file_id(&db, main_id) |
156 | .unwrap() | 156 | .unwrap() |
157 | .unwrap(); | 157 | .unwrap(); |
158 | let module_id = module.module_id; | 158 | let module_id = module.def_id.loc(&db).module_id; |
159 | let item_map = db.item_map(source_root).unwrap(); | 159 | let item_map = db.item_map(source_root).unwrap(); |
160 | 160 | ||
161 | check_module_item_map( | 161 | check_module_item_map( |