From 16fd4dabb754b017237127e70ef1e2b409c4f9b6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 23 Jun 2020 20:20:30 +0200 Subject: Remove file id from item tree It's not needed, and `source` is only used by tests anyways --- crates/ra_hir_def/src/item_tree.rs | 21 +++++++------------- crates/ra_hir_def/src/item_tree/lower.rs | 2 +- crates/ra_hir_def/src/item_tree/tests.rs | 33 ++++++++++++++++---------------- crates/ra_hir_ty/src/tests.rs | 6 +++--- 4 files changed, 28 insertions(+), 34 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 { /// The item tree of a source file. #[derive(Debug, Eq, PartialEq)] pub struct ItemTree { - file_id: HirFileId, top_level: SmallVec<[ModItem; 1]>, attrs: FxHashMap, inner_items: FxHashMap, SmallVec<[ModItem; 1]>>, @@ -81,7 +80,7 @@ impl ItemTree { let syntax = if let Some(node) = db.parse_or_expand(file_id) { node } else { - return Arc::new(Self::empty(file_id)); + return Arc::new(Self::empty()); }; let hygiene = Hygiene::new(db.upcast(), file_id); @@ -113,9 +112,8 @@ impl ItemTree { Arc::new(item_tree) } - fn empty(file_id: HirFileId) -> Self { + fn empty() -> Self { Self { - file_id, top_level: Default::default(), attrs: Default::default(), inner_items: Default::default(), @@ -150,19 +148,14 @@ impl ItemTree { self.inner_items.values().flatten().copied() } - pub fn source( - &self, - db: &dyn DefDatabase, - of: FileItemTreeId, - ) -> S::Source { + pub fn source(&self, db: &dyn DefDatabase, of: ItemTreeId) -> S::Source { // This unwrap cannot fail, since it has either succeeded above, or resulted in an empty // ItemTree (in which case there is no valid `FileItemTreeId` to call this method with). - let root = db - .parse_or_expand(self.file_id) - .expect("parse_or_expand failed on constructed ItemTree"); + let root = + db.parse_or_expand(of.file_id).expect("parse_or_expand failed on constructed ItemTree"); - let id = self[of].ast_id(); - let map = db.ast_id_map(self.file_id); + let id = self[of.value].ast_id(); + let map = db.ast_id_map(of.file_id); let ptr = map.get(id); ptr.to_node(&root) } 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 { impl Ctx { pub(super) fn new(db: &dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self { Self { - tree: ItemTree::empty(file), + tree: ItemTree::empty(), hygiene, file, 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 @@ use super::{ItemTree, ModItem, ModKind}; use crate::{db::DefDatabase, test_db::TestDB}; -use hir_expand::db::AstDatabase; +use hir_expand::{db::AstDatabase, HirFileId, InFile}; use insta::assert_snapshot; use ra_db::fixture::WithFixture; use ra_syntax::{ast, AstNode}; @@ -10,37 +10,38 @@ use stdx::format_to; fn test_inner_items(ra_fixture: &str) { let (db, file_id) = TestDB::with_single_file(ra_fixture); - let tree = db.item_tree(file_id.into()); - let root = db.parse_or_expand(file_id.into()).unwrap(); - let ast_id_map = db.ast_id_map(file_id.into()); + let file_id = HirFileId::from(file_id); + let tree = db.item_tree(file_id); + let root = db.parse_or_expand(file_id).unwrap(); + let ast_id_map = db.ast_id_map(file_id); // Traverse the item tree and collect all module/impl/trait-level items as AST nodes. let mut outer_items = FxHashSet::default(); let mut worklist = tree.top_level_items().to_vec(); while let Some(item) = worklist.pop() { let node: ast::ModuleItem = match item { - ModItem::Import(it) => tree.source(&db, it).into(), - ModItem::ExternCrate(it) => tree.source(&db, it).into(), - ModItem::Function(it) => tree.source(&db, it).into(), - ModItem::Struct(it) => tree.source(&db, it).into(), - ModItem::Union(it) => tree.source(&db, it).into(), - ModItem::Enum(it) => tree.source(&db, it).into(), - ModItem::Const(it) => tree.source(&db, it).into(), - ModItem::Static(it) => tree.source(&db, it).into(), - ModItem::TypeAlias(it) => tree.source(&db, it).into(), + ModItem::Import(it) => tree.source(&db, InFile::new(file_id, it)).into(), + ModItem::ExternCrate(it) => tree.source(&db, InFile::new(file_id, it)).into(), + ModItem::Function(it) => tree.source(&db, InFile::new(file_id, it)).into(), + ModItem::Struct(it) => tree.source(&db, InFile::new(file_id, it)).into(), + ModItem::Union(it) => tree.source(&db, InFile::new(file_id, it)).into(), + ModItem::Enum(it) => tree.source(&db, InFile::new(file_id, it)).into(), + ModItem::Const(it) => tree.source(&db, InFile::new(file_id, it)).into(), + ModItem::Static(it) => tree.source(&db, InFile::new(file_id, it)).into(), + ModItem::TypeAlias(it) => tree.source(&db, InFile::new(file_id, it)).into(), ModItem::Mod(it) => { if let ModKind::Inline { items } = &tree[it].kind { worklist.extend(items); } - tree.source(&db, it).into() + tree.source(&db, InFile::new(file_id, it)).into() } ModItem::Trait(it) => { worklist.extend(tree[it].items.iter().map(|item| ModItem::from(*item))); - tree.source(&db, it).into() + tree.source(&db, InFile::new(file_id, it)).into() } ModItem::Impl(it) => { worklist.extend(tree[it].items.iter().map(|item| ModItem::from(*item))); - tree.source(&db, it).into() + tree.source(&db, InFile::new(file_id, it)).into() } ModItem::MacroCall(_) => continue, }; diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs index e82f2c11e..85ff26a36 100644 --- a/crates/ra_hir_ty/src/tests.rs +++ b/crates/ra_hir_ty/src/tests.rs @@ -166,17 +166,17 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String { DefWithBodyId::FunctionId(it) => { let loc = it.lookup(&db); let tree = db.item_tree(loc.id.file_id); - tree.source(&db, loc.id.value).syntax().text_range().start() + tree.source(&db, loc.id).syntax().text_range().start() } DefWithBodyId::ConstId(it) => { let loc = it.lookup(&db); let tree = db.item_tree(loc.id.file_id); - tree.source(&db, loc.id.value).syntax().text_range().start() + tree.source(&db, loc.id).syntax().text_range().start() } DefWithBodyId::StaticId(it) => { let loc = it.lookup(&db); let tree = db.item_tree(loc.id.file_id); - tree.source(&db, loc.id.value).syntax().text_range().start() + tree.source(&db, loc.id).syntax().text_range().start() } }); for def in defs { -- cgit v1.2.3