aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/item_tree.rs21
-rw-r--r--crates/ra_hir_def/src/item_tree/lower.rs2
-rw-r--r--crates/ra_hir_def/src/item_tree/tests.rs33
3 files changed, 25 insertions, 31 deletions
diff --git a/crates/ra_hir_def/src/item_tree.rs b/crates/ra_hir_def/src/item_tree.rs
index d55b3f777..40bb78b57 100644
--- a/crates/ra_hir_def/src/item_tree.rs
+++ b/crates/ra_hir_def/src/item_tree.rs
@@ -67,7 +67,6 @@ enum AttrOwner {
67/// The item tree of a source file. 67/// The item tree of a source file.
68#[derive(Debug, Eq, PartialEq)] 68#[derive(Debug, Eq, PartialEq)]
69pub struct ItemTree { 69pub struct ItemTree {
70 file_id: HirFileId,
71 top_level: SmallVec<[ModItem; 1]>, 70 top_level: SmallVec<[ModItem; 1]>,
72 attrs: FxHashMap<AttrOwner, Attrs>, 71 attrs: FxHashMap<AttrOwner, Attrs>,
73 inner_items: FxHashMap<FileAstId<ast::ModuleItem>, SmallVec<[ModItem; 1]>>, 72 inner_items: FxHashMap<FileAstId<ast::ModuleItem>, SmallVec<[ModItem; 1]>>,
@@ -81,7 +80,7 @@ impl ItemTree {
81 let syntax = if let Some(node) = db.parse_or_expand(file_id) { 80 let syntax = if let Some(node) = db.parse_or_expand(file_id) {
82 node 81 node
83 } else { 82 } else {
84 return Arc::new(Self::empty(file_id)); 83 return Arc::new(Self::empty());
85 }; 84 };
86 85
87 let hygiene = Hygiene::new(db.upcast(), file_id); 86 let hygiene = Hygiene::new(db.upcast(), file_id);
@@ -113,9 +112,8 @@ impl ItemTree {
113 Arc::new(item_tree) 112 Arc::new(item_tree)
114 } 113 }
115 114
116 fn empty(file_id: HirFileId) -> Self { 115 fn empty() -> Self {
117 Self { 116 Self {
118 file_id,
119 top_level: Default::default(), 117 top_level: Default::default(),
120 attrs: Default::default(), 118 attrs: Default::default(),
121 inner_items: Default::default(), 119 inner_items: Default::default(),
@@ -150,19 +148,14 @@ impl ItemTree {
150 self.inner_items.values().flatten().copied() 148 self.inner_items.values().flatten().copied()
151 } 149 }
152 150
153 pub fn source<S: ItemTreeNode>( 151 pub fn source<S: ItemTreeNode>(&self, db: &dyn DefDatabase, of: ItemTreeId<S>) -> S::Source {
154 &self,
155 db: &dyn DefDatabase,
156 of: FileItemTreeId<S>,
157 ) -> S::Source {
158 // This unwrap cannot fail, since it has either succeeded above, or resulted in an empty 152 // This unwrap cannot fail, since it has either succeeded above, or resulted in an empty
159 // ItemTree (in which case there is no valid `FileItemTreeId` to call this method with). 153 // ItemTree (in which case there is no valid `FileItemTreeId` to call this method with).
160 let root = db 154 let root =
161 .parse_or_expand(self.file_id) 155 db.parse_or_expand(of.file_id).expect("parse_or_expand failed on constructed ItemTree");
162 .expect("parse_or_expand failed on constructed ItemTree");
163 156
164 let id = self[of].ast_id(); 157 let id = self[of.value].ast_id();
165 let map = db.ast_id_map(self.file_id); 158 let map = db.ast_id_map(of.file_id);
166 let ptr = map.get(id); 159 let ptr = map.get(id);
167 ptr.to_node(&root) 160 ptr.to_node(&root)
168 } 161 }
diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs
index 7d28fe7c6..3af22149d 100644
--- a/crates/ra_hir_def/src/item_tree/lower.rs
+++ b/crates/ra_hir_def/src/item_tree/lower.rs
@@ -42,7 +42,7 @@ pub(super) struct Ctx {
42impl Ctx { 42impl Ctx {
43 pub(super) fn new(db: &dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self { 43 pub(super) fn new(db: &dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self {
44 Self { 44 Self {
45 tree: ItemTree::empty(file), 45 tree: ItemTree::empty(),
46 hygiene, 46 hygiene,
47 file, 47 file,
48 source_ast_id_map: db.ast_id_map(file), 48 source_ast_id_map: db.ast_id_map(file),
diff --git a/crates/ra_hir_def/src/item_tree/tests.rs b/crates/ra_hir_def/src/item_tree/tests.rs
index 2df1b3e0c..179baee78 100644
--- a/crates/ra_hir_def/src/item_tree/tests.rs
+++ b/crates/ra_hir_def/src/item_tree/tests.rs
@@ -1,6 +1,6 @@
1use super::{ItemTree, ModItem, ModKind}; 1use super::{ItemTree, ModItem, ModKind};
2use crate::{db::DefDatabase, test_db::TestDB}; 2use crate::{db::DefDatabase, test_db::TestDB};
3use hir_expand::db::AstDatabase; 3use hir_expand::{db::AstDatabase, HirFileId, InFile};
4use insta::assert_snapshot; 4use insta::assert_snapshot;
5use ra_db::fixture::WithFixture; 5use ra_db::fixture::WithFixture;
6use ra_syntax::{ast, AstNode}; 6use ra_syntax::{ast, AstNode};
@@ -10,37 +10,38 @@ use stdx::format_to;
10 10
11fn test_inner_items(ra_fixture: &str) { 11fn test_inner_items(ra_fixture: &str) {
12 let (db, file_id) = TestDB::with_single_file(ra_fixture); 12 let (db, file_id) = TestDB::with_single_file(ra_fixture);
13 let tree = db.item_tree(file_id.into()); 13 let file_id = HirFileId::from(file_id);
14 let root = db.parse_or_expand(file_id.into()).unwrap(); 14 let tree = db.item_tree(file_id);
15 let ast_id_map = db.ast_id_map(file_id.into()); 15 let root = db.parse_or_expand(file_id).unwrap();
16 let ast_id_map = db.ast_id_map(file_id);
16 17
17 // Traverse the item tree and collect all module/impl/trait-level items as AST nodes. 18 // Traverse the item tree and collect all module/impl/trait-level items as AST nodes.
18 let mut outer_items = FxHashSet::default(); 19 let mut outer_items = FxHashSet::default();
19 let mut worklist = tree.top_level_items().to_vec(); 20 let mut worklist = tree.top_level_items().to_vec();
20 while let Some(item) = worklist.pop() { 21 while let Some(item) = worklist.pop() {
21 let node: ast::ModuleItem = match item { 22 let node: ast::ModuleItem = match item {
22 ModItem::Import(it) => tree.source(&db, it).into(), 23 ModItem::Import(it) => tree.source(&db, InFile::new(file_id, it)).into(),
23 ModItem::ExternCrate(it) => tree.source(&db, it).into(), 24 ModItem::ExternCrate(it) => tree.source(&db, InFile::new(file_id, it)).into(),
24 ModItem::Function(it) => tree.source(&db, it).into(), 25 ModItem::Function(it) => tree.source(&db, InFile::new(file_id, it)).into(),
25 ModItem::Struct(it) => tree.source(&db, it).into(), 26 ModItem::Struct(it) => tree.source(&db, InFile::new(file_id, it)).into(),
26 ModItem::Union(it) => tree.source(&db, it).into(), 27 ModItem::Union(it) => tree.source(&db, InFile::new(file_id, it)).into(),
27 ModItem::Enum(it) => tree.source(&db, it).into(), 28 ModItem::Enum(it) => tree.source(&db, InFile::new(file_id, it)).into(),
28 ModItem::Const(it) => tree.source(&db, it).into(), 29 ModItem::Const(it) => tree.source(&db, InFile::new(file_id, it)).into(),
29 ModItem::Static(it) => tree.source(&db, it).into(), 30 ModItem::Static(it) => tree.source(&db, InFile::new(file_id, it)).into(),
30 ModItem::TypeAlias(it) => tree.source(&db, it).into(), 31 ModItem::TypeAlias(it) => tree.source(&db, InFile::new(file_id, it)).into(),
31 ModItem::Mod(it) => { 32 ModItem::Mod(it) => {
32 if let ModKind::Inline { items } = &tree[it].kind { 33 if let ModKind::Inline { items } = &tree[it].kind {
33 worklist.extend(items); 34 worklist.extend(items);
34 } 35 }
35 tree.source(&db, it).into() 36 tree.source(&db, InFile::new(file_id, it)).into()
36 } 37 }
37 ModItem::Trait(it) => { 38 ModItem::Trait(it) => {
38 worklist.extend(tree[it].items.iter().map(|item| ModItem::from(*item))); 39 worklist.extend(tree[it].items.iter().map(|item| ModItem::from(*item)));
39 tree.source(&db, it).into() 40 tree.source(&db, InFile::new(file_id, it)).into()
40 } 41 }
41 ModItem::Impl(it) => { 42 ModItem::Impl(it) => {
42 worklist.extend(tree[it].items.iter().map(|item| ModItem::from(*item))); 43 worklist.extend(tree[it].items.iter().map(|item| ModItem::from(*item)));
43 tree.source(&db, it).into() 44 tree.source(&db, InFile::new(file_id, it)).into()
44 } 45 }
45 ModItem::MacroCall(_) => continue, 46 ModItem::MacroCall(_) => continue,
46 }; 47 };