aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/module_tree.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/module_tree.rs')
-rw-r--r--crates/ra_hir/src/module_tree.rs40
1 files changed, 17 insertions, 23 deletions
diff --git a/crates/ra_hir/src/module_tree.rs b/crates/ra_hir/src/module_tree.rs
index c7a442319..91aab5c74 100644
--- a/crates/ra_hir/src/module_tree.rs
+++ b/crates/ra_hir/src/module_tree.rs
@@ -5,9 +5,9 @@ use arrayvec::ArrayVec;
5use relative_path::RelativePathBuf; 5use relative_path::RelativePathBuf;
6use ra_db::{FileId, SourceRootId, Cancelable, SourceRoot}; 6use ra_db::{FileId, SourceRootId, Cancelable, SourceRoot};
7use ra_syntax::{ 7use ra_syntax::{
8 SyntaxNode, TreePtr,
8 algo::generate, 9 algo::generate,
9 ast::{self, AstNode, NameOwner}, 10 ast::{self, AstNode, NameOwner},
10 SyntaxNode,
11}; 11};
12use ra_arena::{Arena, RawId, impl_arena_id}; 12use ra_arena::{Arena, RawId, impl_arena_id};
13 13
@@ -19,12 +19,11 @@ impl ModuleSource {
19 source_item_id: SourceItemId, 19 source_item_id: SourceItemId,
20 ) -> ModuleSource { 20 ) -> ModuleSource {
21 let module_syntax = db.file_item(source_item_id); 21 let module_syntax = db.file_item(source_item_id);
22 let module_syntax = module_syntax.borrowed(); 22 if let Some(source_file) = ast::SourceFile::cast(&module_syntax) {
23 if let Some(source_file) = ast::SourceFile::cast(module_syntax) { 23 ModuleSource::SourceFile(source_file.to_owned())
24 ModuleSource::SourceFile(source_file.owned()) 24 } else if let Some(module) = ast::Module::cast(&module_syntax) {
25 } else if let Some(module) = ast::Module::cast(module_syntax) {
26 assert!(module.item_list().is_some(), "expected inline module"); 25 assert!(module.item_list().is_some(), "expected inline module");
27 ModuleSource::Module(module.owned()) 26 ModuleSource::Module(module.to_owned())
28 } else { 27 } else {
29 panic!("expected file or inline module") 28 panic!("expected file or inline module")
30 } 29 }
@@ -49,19 +48,18 @@ impl Submodule {
49 let module_source = ModuleSource::from_source_item_id(db, source); 48 let module_source = ModuleSource::from_source_item_id(db, source);
50 let submodules = match module_source { 49 let submodules = match module_source {
51 ModuleSource::SourceFile(source_file) => { 50 ModuleSource::SourceFile(source_file) => {
52 collect_submodules(file_id, &file_items, source_file.borrowed()) 51 collect_submodules(file_id, &file_items, &*source_file)
53 } 52 }
54 ModuleSource::Module(module) => { 53 ModuleSource::Module(module) => {
55 let module = module.borrowed();
56 collect_submodules(file_id, &file_items, module.item_list().unwrap()) 54 collect_submodules(file_id, &file_items, module.item_list().unwrap())
57 } 55 }
58 }; 56 };
59 return Ok(Arc::new(submodules)); 57 return Ok(Arc::new(submodules));
60 58
61 fn collect_submodules<'a>( 59 fn collect_submodules(
62 file_id: HirFileId, 60 file_id: HirFileId,
63 file_items: &SourceFileItems, 61 file_items: &SourceFileItems,
64 root: impl ast::ModuleItemOwner<'a>, 62 root: &impl ast::ModuleItemOwner,
65 ) -> Vec<Submodule> { 63 ) -> Vec<Submodule> {
66 modules(root) 64 modules(root)
67 .map(|(name, m)| Submodule { 65 .map(|(name, m)| Submodule {
@@ -120,8 +118,8 @@ impl ModuleTree {
120 source_root: SourceRootId, 118 source_root: SourceRootId,
121 ) -> Cancelable<Arc<ModuleTree>> { 119 ) -> Cancelable<Arc<ModuleTree>> {
122 db.check_canceled()?; 120 db.check_canceled()?;
123 let res = create_module_tree(db, source_root)?; 121 let res = create_module_tree(db, source_root);
124 Ok(Arc::new(res)) 122 Ok(Arc::new(res?))
125 } 123 }
126 124
127 pub(crate) fn modules<'a>(&'a self) -> impl Iterator<Item = ModuleId> + 'a { 125 pub(crate) fn modules<'a>(&'a self) -> impl Iterator<Item = ModuleId> + 'a {
@@ -172,14 +170,14 @@ impl ModuleId {
172 self, 170 self,
173 tree: &ModuleTree, 171 tree: &ModuleTree,
174 db: &impl HirDatabase, 172 db: &impl HirDatabase,
175 ) -> Vec<(SyntaxNode, Problem)> { 173 ) -> Vec<(TreePtr<SyntaxNode>, Problem)> {
176 tree.mods[self] 174 tree.mods[self]
177 .children 175 .children
178 .iter() 176 .iter()
179 .filter_map(|&link| { 177 .filter_map(|&link| {
180 let p = tree.links[link].problem.clone()?; 178 let p = tree.links[link].problem.clone()?;
181 let s = link.source(tree, db); 179 let s = link.source(tree, db);
182 let s = s.borrowed().name().unwrap().syntax().owned(); 180 let s = s.name().unwrap().syntax().to_owned();
183 Some((s, p)) 181 Some((s, p))
184 }) 182 })
185 .collect() 183 .collect()
@@ -193,11 +191,9 @@ impl LinkId {
193 pub(crate) fn name(self, tree: &ModuleTree) -> &Name { 191 pub(crate) fn name(self, tree: &ModuleTree) -> &Name {
194 &tree.links[self].name 192 &tree.links[self].name
195 } 193 }
196 pub(crate) fn source(self, tree: &ModuleTree, db: &impl HirDatabase) -> ast::ModuleNode { 194 pub(crate) fn source(self, tree: &ModuleTree, db: &impl HirDatabase) -> TreePtr<ast::Module> {
197 let syntax_node = db.file_item(tree.links[self].source); 195 let syntax_node = db.file_item(tree.links[self].source);
198 ast::ModuleNode::cast(syntax_node.borrowed()) 196 ast::Module::cast(&syntax_node).unwrap().to_owned()
199 .unwrap()
200 .owned()
201 } 197 }
202} 198}
203 199
@@ -213,12 +209,10 @@ impl ModuleTree {
213 } 209 }
214} 210}
215 211
216fn modules<'a>( 212fn modules(root: &impl ast::ModuleItemOwner) -> impl Iterator<Item = (Name, &ast::Module)> {
217 root: impl ast::ModuleItemOwner<'a>,
218) -> impl Iterator<Item = (Name, ast::Module<'a>)> {
219 root.items() 213 root.items()
220 .filter_map(|item| match item { 214 .filter_map(|item| match item.kind() {
221 ast::ModuleItem::Module(m) => Some(m), 215 ast::ModuleItemKind::Module(m) => Some(m),
222 _ => None, 216 _ => None,
223 }) 217 })
224 .filter_map(|module| { 218 .filter_map(|module| {