store syntax ptr in FileItems

we cache the tree in file_item query anyway
This commit is contained in:
Aleksey Kladov 2019-01-26 22:48:04 +03:00
parent ae97a45c35
commit 9c1a18a626
2 changed files with 15 additions and 21 deletions

View File

@ -4,7 +4,7 @@ use std::{
};
use ra_db::{LocationIntener, FileId};
use ra_syntax::{TreeArc, SyntaxNode, SourceFile, AstNode, ast};
use ra_syntax::{TreeArc, SyntaxNode, SourceFile, AstNode, SyntaxNodePtr, ast};
use ra_arena::{Arena, RawId, ArenaId, impl_arena_id};
use crate::{
@ -309,7 +309,7 @@ pub struct SourceItemId {
#[derive(Debug, PartialEq, Eq)]
pub struct SourceFileItems {
file_id: HirFileId,
arena: Arena<SourceFileItemId, TreeArc<SyntaxNode>>,
arena: Arena<SourceFileItemId, SyntaxNodePtr>,
}
impl SourceFileItems {
@ -329,15 +329,15 @@ impl SourceFileItems {
// trait does not chage ids of top-level items, which helps caching.
bfs(source_file.syntax(), |it| {
if let Some(module_item) = ast::ModuleItem::cast(it) {
self.alloc(module_item.syntax().to_owned());
self.alloc(module_item.syntax());
} else if let Some(macro_call) = ast::MacroCall::cast(it) {
self.alloc(macro_call.syntax().to_owned());
self.alloc(macro_call.syntax());
}
})
}
fn alloc(&mut self, item: TreeArc<SyntaxNode>) -> SourceFileItemId {
self.arena.alloc(item)
fn alloc(&mut self, item: &SyntaxNode) -> SourceFileItemId {
self.arena.alloc(SyntaxNodePtr::new(item))
}
pub(crate) fn id_of(&self, file_id: HirFileId, item: &SyntaxNode) -> SourceFileItemId {
assert_eq!(
@ -348,17 +348,8 @@ impl SourceFileItems {
self.id_of_unchecked(item)
}
pub(crate) fn id_of_unchecked(&self, item: &SyntaxNode) -> SourceFileItemId {
if let Some((id, _)) = self.arena.iter().find(|(_id, i)| *i == item) {
return id;
}
// This should not happen. Let's try to give a sensible diagnostics.
if let Some((id, i)) = self.arena.iter().find(|(_id, i)| i.range() == item.range()) {
// FIXME(#288): whyyy are we getting here?
log::error!(
"unequal syntax nodes with the same range:\n{:?}\n{:?}",
item,
i
);
let ptr = SyntaxNodePtr::new(item);
if let Some((id, _)) = self.arena.iter().find(|(_id, i)| **i == ptr) {
return id;
}
panic!(
@ -370,8 +361,8 @@ impl SourceFileItems {
}
impl std::ops::Index<SourceFileItemId> for SourceFileItems {
type Output = SyntaxNode;
fn index(&self, idx: SourceFileItemId) -> &SyntaxNode {
type Output = SyntaxNodePtr;
fn index(&self, idx: SourceFileItemId) -> &SyntaxNodePtr {
&self.arena[idx]
}
}

View File

@ -32,9 +32,12 @@ pub(super) fn file_item(
db: &impl HirDatabase,
source_item_id: SourceItemId,
) -> TreeArc<SyntaxNode> {
let source_file = db.hir_parse(source_item_id.file_id);
match source_item_id.item_id {
Some(id) => db.file_items(source_item_id.file_id)[id].to_owned(),
None => db.hir_parse(source_item_id.file_id).syntax().to_owned(),
Some(id) => db.file_items(source_item_id.file_id)[id]
.to_node(&source_file)
.to_owned(),
None => source_file.syntax().to_owned(),
}
}