aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model_impl
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-03-13 13:38:02 +0000
committerAleksey Kladov <[email protected]>2019-03-17 09:49:07 +0000
commit2195d1db6d70d64383bec82819fab02891d09744 (patch)
tree40174ca7cbb3625ea62ebc10dbd9b592c83a8081 /crates/ra_hir/src/code_model_impl
parent182c05a96c25321ac3ff262cea098e0c4d7ed6f8 (diff)
Replace module_tree with CrateDefMap
Diffstat (limited to 'crates/ra_hir/src/code_model_impl')
-rw-r--r--crates/ra_hir/src/code_model_impl/krate.rs4
-rw-r--r--crates/ra_hir/src/code_model_impl/module.rs97
2 files changed, 67 insertions, 34 deletions
diff --git a/crates/ra_hir/src/code_model_impl/krate.rs b/crates/ra_hir/src/code_model_impl/krate.rs
index 161ae6e18..cc87c6f14 100644
--- a/crates/ra_hir/src/code_model_impl/krate.rs
+++ b/crates/ra_hir/src/code_model_impl/krate.rs
@@ -18,9 +18,7 @@ impl Crate {
18 .collect() 18 .collect()
19 } 19 }
20 pub(crate) fn root_module_impl(&self, db: &impl PersistentHirDatabase) -> Option<Module> { 20 pub(crate) fn root_module_impl(&self, db: &impl PersistentHirDatabase) -> Option<Module> {
21 let module_tree = db.module_tree(*self); 21 let module_id = db.crate_def_map(*self).root();
22 let module_id = module_tree.modules().next()?;
23
24 let module = Module { krate: *self, module_id }; 22 let module = Module { krate: *self, module_id };
25 Some(module) 23 Some(module)
26 } 24 }
diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs
index 437f96942..5d8f738b5 100644
--- a/crates/ra_hir/src/code_model_impl/module.rs
+++ b/crates/ra_hir/src/code_model_impl/module.rs
@@ -1,33 +1,62 @@
1use ra_syntax::{ast, SyntaxNode, TreeArc}; 1use ra_db::FileId;
2use ra_syntax::{ast, SyntaxNode, TreeArc, AstNode};
2 3
3use crate::{ 4use crate::{
4 Module, ModuleSource, Problem, 5 Module, ModuleSource, Problem, Name,
5 Name, 6 nameres::crate_def_map::ModuleId,
6 module_tree::ModuleId,
7 nameres::lower::ImportId, 7 nameres::lower::ImportId,
8 HirDatabase, PersistentHirDatabase, 8 HirDatabase, PersistentHirDatabase,
9 HirFileId 9 HirFileId, SourceItemId,
10}; 10};
11 11
12impl ModuleSource {
13 pub(crate) fn new(
14 db: &impl PersistentHirDatabase,
15 file_id: Option<FileId>,
16 decl_id: Option<SourceItemId>,
17 ) -> ModuleSource {
18 match (file_id, decl_id) {
19 (Some(file_id), _) => {
20 let source_file = db.parse(file_id);
21 ModuleSource::SourceFile(source_file)
22 }
23 (None, Some(item_id)) => {
24 let module = db.file_item(item_id);
25 let module = ast::Module::cast(&*module).unwrap();
26 assert!(module.item_list().is_some(), "expected inline module");
27 ModuleSource::Module(module.to_owned())
28 }
29 (None, None) => panic!(),
30 }
31 }
32}
33
12impl Module { 34impl Module {
13 fn with_module_id(&self, module_id: ModuleId) -> Module { 35 fn with_module_id(&self, module_id: ModuleId) -> Module {
14 Module { module_id, krate: self.krate } 36 Module { module_id, krate: self.krate }
15 } 37 }
16 38
17 pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Option<Name> { 39 pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Option<Name> {
18 let module_tree = db.module_tree(self.krate); 40 let def_map = db.crate_def_map(self.krate);
19 let link = self.module_id.parent_link(&module_tree)?; 41 let parent = def_map[self.module_id].parent?;
20 Some(link.name(&module_tree).clone()) 42 def_map[parent].children.iter().find_map(|(name, module_id)| {
43 if *module_id == self.module_id {
44 Some(name.clone())
45 } else {
46 None
47 }
48 })
21 } 49 }
22 50
23 pub(crate) fn definition_source_impl( 51 pub(crate) fn definition_source_impl(
24 &self, 52 &self,
25 db: &impl PersistentHirDatabase, 53 db: &impl PersistentHirDatabase,
26 ) -> (HirFileId, ModuleSource) { 54 ) -> (HirFileId, ModuleSource) {
27 let module_tree = db.module_tree(self.krate); 55 let def_map = db.crate_def_map(self.krate);
28 let file_id = self.module_id.file_id(&module_tree); 56 let decl_id = def_map[self.module_id].declaration;
29 let decl_id = self.module_id.decl_id(&module_tree); 57 let file_id = def_map[self.module_id].definition;
30 let module_source = ModuleSource::new(db, file_id, decl_id); 58 let module_source = ModuleSource::new(db, file_id, decl_id);
59 let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id);
31 (file_id, module_source) 60 (file_id, module_source)
32 } 61 }
33 62
@@ -35,11 +64,11 @@ impl Module {
35 &self, 64 &self,
36 db: &impl HirDatabase, 65 db: &impl HirDatabase,
37 ) -> Option<(HirFileId, TreeArc<ast::Module>)> { 66 ) -> Option<(HirFileId, TreeArc<ast::Module>)> {
38 let module_tree = db.module_tree(self.krate); 67 let def_map = db.crate_def_map(self.krate);
39 let link = self.module_id.parent_link(&module_tree)?; 68 let decl = def_map[self.module_id].declaration?;
40 let file_id = link.owner(&module_tree).file_id(&module_tree); 69 let syntax_node = db.file_item(decl);
41 let src = link.source(&module_tree, db); 70 let ast = ast::Module::cast(&syntax_node).unwrap().to_owned();
42 Some((file_id, src)) 71 Some((decl.file_id, ast))
43 } 72 }
44 73
45 pub(crate) fn import_source_impl( 74 pub(crate) fn import_source_impl(
@@ -53,16 +82,15 @@ impl Module {
53 } 82 }
54 83
55 pub(crate) fn crate_root_impl(&self, db: &impl PersistentHirDatabase) -> Module { 84 pub(crate) fn crate_root_impl(&self, db: &impl PersistentHirDatabase) -> Module {
56 let module_tree = db.module_tree(self.krate); 85 let def_map = db.crate_def_map(self.krate);
57 let module_id = self.module_id.crate_root(&module_tree); 86 self.with_module_id(def_map.root())
58 self.with_module_id(module_id)
59 } 87 }
60 88
61 /// Finds a child module with the specified name. 89 /// Finds a child module with the specified name.
62 pub(crate) fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Option<Module> { 90 pub(crate) fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Option<Module> {
63 let module_tree = db.module_tree(self.krate); 91 let def_map = db.crate_def_map(self.krate);
64 let child_id = self.module_id.child(&module_tree, name)?; 92 let child_id = def_map[self.module_id].children.get(name)?;
65 Some(self.with_module_id(child_id)) 93 Some(self.with_module_id(*child_id))
66 } 94 }
67 95
68 /// Iterates over all child modules. 96 /// Iterates over all child modules.
@@ -70,18 +98,18 @@ impl Module {
70 &self, 98 &self,
71 db: &impl PersistentHirDatabase, 99 db: &impl PersistentHirDatabase,
72 ) -> impl Iterator<Item = Module> { 100 ) -> impl Iterator<Item = Module> {
73 let module_tree = db.module_tree(self.krate); 101 let def_map = db.crate_def_map(self.krate);
74 let children = self 102 let children = def_map[self.module_id]
75 .module_id 103 .children
76 .children(&module_tree) 104 .iter()
77 .map(|(_, module_id)| self.with_module_id(module_id)) 105 .map(|(_, module_id)| self.with_module_id(*module_id))
78 .collect::<Vec<_>>(); 106 .collect::<Vec<_>>();
79 children.into_iter() 107 children.into_iter()
80 } 108 }
81 109
82 pub(crate) fn parent_impl(&self, db: &impl PersistentHirDatabase) -> Option<Module> { 110 pub(crate) fn parent_impl(&self, db: &impl PersistentHirDatabase) -> Option<Module> {
83 let module_tree = db.module_tree(self.krate); 111 let def_map = db.crate_def_map(self.krate);
84 let parent_id = self.module_id.parent(&module_tree)?; 112 let parent_id = def_map[self.module_id].parent?;
85 Some(self.with_module_id(parent_id)) 113 Some(self.with_module_id(parent_id))
86 } 114 }
87 115
@@ -89,7 +117,14 @@ impl Module {
89 &self, 117 &self,
90 db: &impl HirDatabase, 118 db: &impl HirDatabase,
91 ) -> Vec<(TreeArc<SyntaxNode>, Problem)> { 119 ) -> Vec<(TreeArc<SyntaxNode>, Problem)> {
92 let module_tree = db.module_tree(self.krate); 120 let def_map = db.crate_def_map(self.krate);
93 self.module_id.problems(&module_tree, db) 121 let (my_file_id, _) = self.definition_source(db);
122 // FIXME: not entirely corret filterint by module
123 def_map
124 .problems()
125 .iter()
126 .filter(|(source_item_id, _problem)| my_file_id == source_item_id.file_id)
127 .map(|(source_item_id, problem)| (db.file_item(*source_item_id), problem.clone()))
128 .collect()
94 } 129 }
95} 130}