mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 22:31:43 +00:00
Remove file id from item tree
It's not needed, and `source` is only used by tests anyways
This commit is contained in:
parent
c019002d17
commit
16fd4dabb7
4 changed files with 28 additions and 34 deletions
|
@ -67,7 +67,6 @@ enum AttrOwner {
|
||||||
/// The item tree of a source file.
|
/// The item tree of a source file.
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct ItemTree {
|
pub struct ItemTree {
|
||||||
file_id: HirFileId,
|
|
||||||
top_level: SmallVec<[ModItem; 1]>,
|
top_level: SmallVec<[ModItem; 1]>,
|
||||||
attrs: FxHashMap<AttrOwner, Attrs>,
|
attrs: FxHashMap<AttrOwner, Attrs>,
|
||||||
inner_items: FxHashMap<FileAstId<ast::ModuleItem>, SmallVec<[ModItem; 1]>>,
|
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) {
|
let syntax = if let Some(node) = db.parse_or_expand(file_id) {
|
||||||
node
|
node
|
||||||
} else {
|
} else {
|
||||||
return Arc::new(Self::empty(file_id));
|
return Arc::new(Self::empty());
|
||||||
};
|
};
|
||||||
|
|
||||||
let hygiene = Hygiene::new(db.upcast(), file_id);
|
let hygiene = Hygiene::new(db.upcast(), file_id);
|
||||||
|
@ -113,9 +112,8 @@ impl ItemTree {
|
||||||
Arc::new(item_tree)
|
Arc::new(item_tree)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn empty(file_id: HirFileId) -> Self {
|
fn empty() -> Self {
|
||||||
Self {
|
Self {
|
||||||
file_id,
|
|
||||||
top_level: Default::default(),
|
top_level: Default::default(),
|
||||||
attrs: Default::default(),
|
attrs: Default::default(),
|
||||||
inner_items: Default::default(),
|
inner_items: Default::default(),
|
||||||
|
@ -150,19 +148,14 @@ impl ItemTree {
|
||||||
self.inner_items.values().flatten().copied()
|
self.inner_items.values().flatten().copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn source<S: ItemTreeNode>(
|
pub fn source<S: ItemTreeNode>(&self, db: &dyn DefDatabase, of: ItemTreeId<S>) -> S::Source {
|
||||||
&self,
|
|
||||||
db: &dyn DefDatabase,
|
|
||||||
of: FileItemTreeId<S>,
|
|
||||||
) -> S::Source {
|
|
||||||
// This unwrap cannot fail, since it has either succeeded above, or resulted in an empty
|
// 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).
|
// ItemTree (in which case there is no valid `FileItemTreeId` to call this method with).
|
||||||
let root = db
|
let root =
|
||||||
.parse_or_expand(self.file_id)
|
db.parse_or_expand(of.file_id).expect("parse_or_expand failed on constructed ItemTree");
|
||||||
.expect("parse_or_expand failed on constructed ItemTree");
|
|
||||||
|
|
||||||
let id = self[of].ast_id();
|
let id = self[of.value].ast_id();
|
||||||
let map = db.ast_id_map(self.file_id);
|
let map = db.ast_id_map(of.file_id);
|
||||||
let ptr = map.get(id);
|
let ptr = map.get(id);
|
||||||
ptr.to_node(&root)
|
ptr.to_node(&root)
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ pub(super) struct Ctx {
|
||||||
impl Ctx {
|
impl Ctx {
|
||||||
pub(super) fn new(db: &dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self {
|
pub(super) fn new(db: &dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self {
|
||||||
Self {
|
Self {
|
||||||
tree: ItemTree::empty(file),
|
tree: ItemTree::empty(),
|
||||||
hygiene,
|
hygiene,
|
||||||
file,
|
file,
|
||||||
source_ast_id_map: db.ast_id_map(file),
|
source_ast_id_map: db.ast_id_map(file),
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use super::{ItemTree, ModItem, ModKind};
|
use super::{ItemTree, ModItem, ModKind};
|
||||||
use crate::{db::DefDatabase, test_db::TestDB};
|
use crate::{db::DefDatabase, test_db::TestDB};
|
||||||
use hir_expand::db::AstDatabase;
|
use hir_expand::{db::AstDatabase, HirFileId, InFile};
|
||||||
use insta::assert_snapshot;
|
use insta::assert_snapshot;
|
||||||
use ra_db::fixture::WithFixture;
|
use ra_db::fixture::WithFixture;
|
||||||
use ra_syntax::{ast, AstNode};
|
use ra_syntax::{ast, AstNode};
|
||||||
|
@ -10,37 +10,38 @@ use stdx::format_to;
|
||||||
|
|
||||||
fn test_inner_items(ra_fixture: &str) {
|
fn test_inner_items(ra_fixture: &str) {
|
||||||
let (db, file_id) = TestDB::with_single_file(ra_fixture);
|
let (db, file_id) = TestDB::with_single_file(ra_fixture);
|
||||||
let tree = db.item_tree(file_id.into());
|
let file_id = HirFileId::from(file_id);
|
||||||
let root = db.parse_or_expand(file_id.into()).unwrap();
|
let tree = db.item_tree(file_id);
|
||||||
let ast_id_map = db.ast_id_map(file_id.into());
|
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.
|
// Traverse the item tree and collect all module/impl/trait-level items as AST nodes.
|
||||||
let mut outer_items = FxHashSet::default();
|
let mut outer_items = FxHashSet::default();
|
||||||
let mut worklist = tree.top_level_items().to_vec();
|
let mut worklist = tree.top_level_items().to_vec();
|
||||||
while let Some(item) = worklist.pop() {
|
while let Some(item) = worklist.pop() {
|
||||||
let node: ast::ModuleItem = match item {
|
let node: ast::ModuleItem = match item {
|
||||||
ModItem::Import(it) => tree.source(&db, it).into(),
|
ModItem::Import(it) => tree.source(&db, InFile::new(file_id, it)).into(),
|
||||||
ModItem::ExternCrate(it) => tree.source(&db, it).into(),
|
ModItem::ExternCrate(it) => tree.source(&db, InFile::new(file_id, it)).into(),
|
||||||
ModItem::Function(it) => tree.source(&db, it).into(),
|
ModItem::Function(it) => tree.source(&db, InFile::new(file_id, it)).into(),
|
||||||
ModItem::Struct(it) => tree.source(&db, it).into(),
|
ModItem::Struct(it) => tree.source(&db, InFile::new(file_id, it)).into(),
|
||||||
ModItem::Union(it) => tree.source(&db, it).into(),
|
ModItem::Union(it) => tree.source(&db, InFile::new(file_id, it)).into(),
|
||||||
ModItem::Enum(it) => tree.source(&db, it).into(),
|
ModItem::Enum(it) => tree.source(&db, InFile::new(file_id, it)).into(),
|
||||||
ModItem::Const(it) => tree.source(&db, it).into(),
|
ModItem::Const(it) => tree.source(&db, InFile::new(file_id, it)).into(),
|
||||||
ModItem::Static(it) => tree.source(&db, it).into(),
|
ModItem::Static(it) => tree.source(&db, InFile::new(file_id, it)).into(),
|
||||||
ModItem::TypeAlias(it) => tree.source(&db, it).into(),
|
ModItem::TypeAlias(it) => tree.source(&db, InFile::new(file_id, it)).into(),
|
||||||
ModItem::Mod(it) => {
|
ModItem::Mod(it) => {
|
||||||
if let ModKind::Inline { items } = &tree[it].kind {
|
if let ModKind::Inline { items } = &tree[it].kind {
|
||||||
worklist.extend(items);
|
worklist.extend(items);
|
||||||
}
|
}
|
||||||
tree.source(&db, it).into()
|
tree.source(&db, InFile::new(file_id, it)).into()
|
||||||
}
|
}
|
||||||
ModItem::Trait(it) => {
|
ModItem::Trait(it) => {
|
||||||
worklist.extend(tree[it].items.iter().map(|item| ModItem::from(*item)));
|
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) => {
|
ModItem::Impl(it) => {
|
||||||
worklist.extend(tree[it].items.iter().map(|item| ModItem::from(*item)));
|
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,
|
ModItem::MacroCall(_) => continue,
|
||||||
};
|
};
|
||||||
|
|
|
@ -166,17 +166,17 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
|
||||||
DefWithBodyId::FunctionId(it) => {
|
DefWithBodyId::FunctionId(it) => {
|
||||||
let loc = it.lookup(&db);
|
let loc = it.lookup(&db);
|
||||||
let tree = db.item_tree(loc.id.file_id);
|
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) => {
|
DefWithBodyId::ConstId(it) => {
|
||||||
let loc = it.lookup(&db);
|
let loc = it.lookup(&db);
|
||||||
let tree = db.item_tree(loc.id.file_id);
|
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) => {
|
DefWithBodyId::StaticId(it) => {
|
||||||
let loc = it.lookup(&db);
|
let loc = it.lookup(&db);
|
||||||
let tree = db.item_tree(loc.id.file_id);
|
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 {
|
for def in defs {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue