Remove file id from item tree

It's not needed, and `source` is only used by tests anyways
This commit is contained in:
Jonas Schievink 2020-06-23 20:20:30 +02:00
parent c019002d17
commit 16fd4dabb7
4 changed files with 28 additions and 34 deletions

View file

@ -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<AttrOwner, Attrs>,
inner_items: FxHashMap<FileAstId<ast::ModuleItem>, 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<S: ItemTreeNode>(
&self,
db: &dyn DefDatabase,
of: FileItemTreeId<S>,
) -> S::Source {
pub fn source<S: ItemTreeNode>(&self, db: &dyn DefDatabase, of: ItemTreeId<S>) -> 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)
}

View file

@ -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),

View file

@ -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,
};

View file

@ -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 {