diff options
-rw-r--r-- | crates/ra_analysis/src/imp.rs | 29 | ||||
-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 |
4 files changed, 42 insertions, 20 deletions
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 5988fb779..44e7aca44 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs | |||
@@ -105,39 +105,36 @@ impl db::RootDatabase { | |||
105 | &self, | 105 | &self, |
106 | position: FilePosition, | 106 | position: FilePosition, |
107 | ) -> Cancelable<Vec<NavigationTarget>> { | 107 | ) -> Cancelable<Vec<NavigationTarget>> { |
108 | let descr = match source_binder::module_from_position(self, position)? { | 108 | let module = match source_binder::module_from_position(self, position)? { |
109 | None => return Ok(Vec::new()), | 109 | None => return Ok(Vec::new()), |
110 | Some(it) => it, | 110 | Some(it) => it, |
111 | }; | 111 | }; |
112 | let (file_id, decl) = match descr.parent_link_source(self) { | 112 | let (file_id, ast_module) = module.source(self); |
113 | let ast_module = match ast_module { | ||
113 | None => return Ok(Vec::new()), | 114 | None => return Ok(Vec::new()), |
114 | Some(it) => it, | 115 | Some(it) => it, |
115 | }; | 116 | }; |
116 | let decl = decl.borrowed(); | 117 | let ast_module = ast_module.borrowed(); |
117 | let decl_name = decl.name().unwrap(); | 118 | let name = ast_module.name().unwrap(); |
118 | Ok(vec![NavigationTarget { | 119 | Ok(vec![NavigationTarget { |
119 | file_id, | 120 | file_id, |
120 | name: decl_name.text(), | 121 | name: name.text(), |
121 | range: decl_name.syntax().range(), | 122 | range: name.syntax().range(), |
122 | kind: MODULE, | 123 | kind: MODULE, |
123 | ptr: None, | 124 | ptr: None, |
124 | }]) | 125 | }]) |
125 | } | 126 | } |
126 | /// Returns `Vec` for the same reason as `parent_module` | 127 | /// Returns `Vec` for the same reason as `parent_module` |
127 | pub(crate) fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> { | 128 | pub(crate) fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> { |
128 | let descr = match source_binder::module_from_file_id(self, file_id)? { | 129 | let module = match source_binder::module_from_file_id(self, file_id)? { |
130 | Some(it) => it, | ||
129 | None => return Ok(Vec::new()), | 131 | None => return Ok(Vec::new()), |
132 | }; | ||
133 | let krate = match module.krate(self)? { | ||
130 | Some(it) => it, | 134 | Some(it) => it, |
135 | None => return Ok(Vec::new()), | ||
131 | }; | 136 | }; |
132 | let root = descr.crate_root(); | 137 | Ok(vec![krate.crate_id()]) |
133 | let file_id = root.file_id(); | ||
134 | |||
135 | let crate_graph = self.crate_graph(); | ||
136 | let crate_id = crate_graph.crate_id_for_crate_root(file_id); | ||
137 | Ok(crate_id.into_iter().collect()) | ||
138 | } | ||
139 | pub(crate) fn crate_root(&self, crate_id: CrateId) -> FileId { | ||
140 | self.crate_graph().crate_root(crate_id) | ||
141 | } | 138 | } |
142 | pub(crate) fn find_all_refs( | 139 | pub(crate) fn find_all_refs( |
143 | &self, | 140 | &self, |
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( |