diff options
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 23 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model_impl.rs | 59 | ||||
-rw-r--r-- | crates/ra_hir/src/module.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/module/nameres.rs | 2 |
4 files changed, 83 insertions, 7 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index e8b3a1fb6..63e2e34e8 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use ra_db::{CrateId, Cancelable}; | 1 | use ra_db::{CrateId, Cancelable}; |
2 | 2 | ||
3 | use crate::{Module, Name, db::HirDatabase}; | 3 | use crate::{Name, db::HirDatabase, DefId}; |
4 | 4 | ||
5 | /// hir::Crate describes a single crate. It's the main inteface with which | 5 | /// hir::Crate describes a single crate. It's the main inteface with which |
6 | /// crate's dependencies interact. Mostly, it should be just a proxy for the | 6 | /// crate's dependencies interact. Mostly, it should be just a proxy for the |
@@ -24,3 +24,24 @@ impl Crate { | |||
24 | self.root_module_impl(db) | 24 | self.root_module_impl(db) |
25 | } | 25 | } |
26 | } | 26 | } |
27 | |||
28 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
29 | pub struct Module { | ||
30 | pub(crate) def_id: DefId, | ||
31 | } | ||
32 | |||
33 | impl Module { | ||
34 | /// Returns the crate this module is part of. | ||
35 | pub fn krate(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { | ||
36 | self.krate_impl(db) | ||
37 | } | ||
38 | |||
39 | pub fn crate_root(&self, db: &impl HirDatabase) -> Cancelable<Module> { | ||
40 | self.crate_root_impl(db) | ||
41 | } | ||
42 | |||
43 | /// Finds a child module with the specified name. | ||
44 | pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { | ||
45 | self.child_impl(db, name) | ||
46 | } | ||
47 | } | ||
diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs index 75d4e04c1..22079ba33 100644 --- a/crates/ra_hir/src/code_model_impl.rs +++ b/crates/ra_hir/src/code_model_impl.rs | |||
@@ -1,6 +1,9 @@ | |||
1 | use ra_db::{CrateId, Cancelable}; | 1 | use ra_db::{CrateId, Cancelable}; |
2 | 2 | ||
3 | use crate::{Module, HirFileId, db::HirDatabase, Crate, CrateDependency, AsName}; | 3 | use crate::{HirFileId, db::HirDatabase, Crate, CrateDependency, AsName, DefId, DefLoc, DefKind, Name}; |
4 | |||
5 | use crate::code_model_api::Module; | ||
6 | |||
4 | 7 | ||
5 | impl Crate { | 8 | impl Crate { |
6 | pub(crate) fn new(crate_id: CrateId) -> Crate { | 9 | pub(crate) fn new(crate_id: CrateId) -> Crate { |
@@ -28,7 +31,59 @@ impl Crate { | |||
28 | .modules_with_sources() | 31 | .modules_with_sources() |
29 | .find(|(_, src)| src.file_id() == file_id)); | 32 | .find(|(_, src)| src.file_id() == file_id)); |
30 | 33 | ||
31 | let module = Module::new(db, source_root_id, module_id)?; | 34 | let def_loc = DefLoc { |
35 | kind: DefKind::Module, | ||
36 | source_root_id, | ||
37 | module_id, | ||
38 | source_item_id: module_id.source(&module_tree).0, | ||
39 | }; | ||
40 | let def_id = def_loc.id(db); | ||
41 | |||
42 | let module = Module::new(def_id); | ||
43 | Ok(Some(module)) | ||
44 | } | ||
45 | } | ||
46 | |||
47 | impl Module { | ||
48 | fn new(def_id: DefId) -> Self { | ||
49 | crate::code_model_api::Module { def_id } | ||
50 | } | ||
51 | |||
52 | pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { | ||
53 | let root = self.crate_root(db)?; | ||
54 | let loc = root.def_id.loc(db); | ||
55 | let file_id = loc.source_item_id.file_id.as_original_file(); | ||
56 | |||
57 | let crate_graph = db.crate_graph(); | ||
58 | let crate_id = ctry!(crate_graph.crate_id_for_crate_root(file_id)); | ||
59 | Ok(Some(Crate::new(crate_id))) | ||
60 | } | ||
61 | |||
62 | pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Cancelable<Module> { | ||
63 | let loc = self.def_id.loc(db); | ||
64 | let module_tree = db.module_tree(loc.source_root_id)?; | ||
65 | let module_id = loc.module_id.crate_root(&module_tree); | ||
66 | let def_loc = DefLoc { | ||
67 | module_id, | ||
68 | source_item_id: module_id.source(&module_tree).0, | ||
69 | ..loc | ||
70 | }; | ||
71 | let def_id = def_loc.id(db); | ||
72 | let module = Module::new(def_id); | ||
73 | Ok(module) | ||
74 | } | ||
75 | /// Finds a child module with the specified name. | ||
76 | pub fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { | ||
77 | let loc = self.def_id.loc(db); | ||
78 | let module_tree = db.module_tree(loc.source_root_id)?; | ||
79 | let child_id = ctry!(loc.module_id.child(&module_tree, name)); | ||
80 | let def_loc = DefLoc { | ||
81 | module_id: child_id, | ||
82 | source_item_id: child_id.source(&module_tree).0, | ||
83 | ..loc | ||
84 | }; | ||
85 | let def_id = def_loc.id(db); | ||
86 | let module = Module::new(def_id); | ||
32 | Ok(Some(module)) | 87 | Ok(Some(module)) |
33 | } | 88 | } |
34 | } | 89 | } |
diff --git a/crates/ra_hir/src/module.rs b/crates/ra_hir/src/module.rs index b9821115c..f0b673908 100644 --- a/crates/ra_hir/src/module.rs +++ b/crates/ra_hir/src/module.rs | |||
@@ -224,7 +224,7 @@ impl ModuleTree { | |||
224 | /// `ModuleSource` is the syntax tree element that produced this module: | 224 | /// `ModuleSource` is the syntax tree element that produced this module: |
225 | /// either a file, or an inlinde module. | 225 | /// either a file, or an inlinde module. |
226 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 226 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
227 | pub struct ModuleSource(SourceItemId); | 227 | pub struct ModuleSource(pub(crate) SourceItemId); |
228 | 228 | ||
229 | /// An owned syntax node for a module. Unlike `ModuleSource`, | 229 | /// An owned syntax node for a module. Unlike `ModuleSource`, |
230 | /// this holds onto the AST for the whole file. | 230 | /// this holds onto the AST for the whole file. |
@@ -255,12 +255,12 @@ impl ModuleId { | |||
255 | let link = self.parent_link(tree)?; | 255 | let link = self.parent_link(tree)?; |
256 | Some(tree.links[link].owner) | 256 | Some(tree.links[link].owner) |
257 | } | 257 | } |
258 | fn crate_root(self, tree: &ModuleTree) -> ModuleId { | 258 | pub(crate) fn crate_root(self, tree: &ModuleTree) -> ModuleId { |
259 | generate(Some(self), move |it| it.parent(tree)) | 259 | generate(Some(self), move |it| it.parent(tree)) |
260 | .last() | 260 | .last() |
261 | .unwrap() | 261 | .unwrap() |
262 | } | 262 | } |
263 | fn child(self, tree: &ModuleTree, name: &Name) -> Option<ModuleId> { | 263 | pub(crate) fn child(self, tree: &ModuleTree, name: &Name) -> Option<ModuleId> { |
264 | let link = tree.mods[self] | 264 | let link = tree.mods[self] |
265 | .children | 265 | .children |
266 | .iter() | 266 | .iter() |
diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index 3c6851a0a..cd634e42f 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs | |||
@@ -346,7 +346,7 @@ where | |||
346 | let krate = Crate::new(crate_id); | 346 | let krate = Crate::new(crate_id); |
347 | for dep in krate.dependencies(self.db) { | 347 | for dep in krate.dependencies(self.db) { |
348 | if let Some(module) = dep.krate.root_module(self.db)? { | 348 | if let Some(module) = dep.krate.root_module(self.db)? { |
349 | let def_id = module.def_id(self.db); | 349 | let def_id = module.def_id; |
350 | self.add_module_item( | 350 | self.add_module_item( |
351 | &mut module_items, | 351 | &mut module_items, |
352 | dep.name.clone(), | 352 | dep.name.clone(), |