rust-analyzer/crates/ra_hir/src/source_id.rs

160 lines
4.6 KiB
Rust
Raw Normal View History

use std::{
hash::{Hash, Hasher},
marker::PhantomData,
sync::Arc,
};
2019-03-26 11:40:34 +00:00
use ra_arena::{impl_arena_id, Arena, RawId};
2019-07-19 07:43:01 +00:00
use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxNodePtr};
2019-03-26 11:40:34 +00:00
2019-09-08 06:53:49 +00:00
use crate::{db::AstDatabase, HirFileId};
2019-03-26 11:40:34 +00:00
2019-03-26 16:00:11 +00:00
/// `AstId` points to an AST node in any file.
///
/// It is stable across reparses, and can be used as salsa key/value.
2019-03-26 15:27:22 +00:00
#[derive(Debug)]
2019-03-26 14:25:14 +00:00
pub(crate) struct AstId<N: AstNode> {
file_id: HirFileId,
file_ast_id: FileAstId<N>,
}
impl<N: AstNode> Clone for AstId<N> {
fn clone(&self) -> AstId<N> {
*self
}
}
impl<N: AstNode> Copy for AstId<N> {}
2019-03-26 15:27:22 +00:00
impl<N: AstNode> PartialEq for AstId<N> {
fn eq(&self, other: &Self) -> bool {
(self.file_id, self.file_ast_id) == (other.file_id, other.file_ast_id)
}
}
impl<N: AstNode> Eq for AstId<N> {}
impl<N: AstNode> Hash for AstId<N> {
fn hash<H: Hasher>(&self, hasher: &mut H) {
(self.file_id, self.file_ast_id).hash(hasher);
}
}
2019-03-26 14:25:14 +00:00
impl<N: AstNode> AstId<N> {
pub(crate) fn file_id(&self) -> HirFileId {
self.file_id
}
2019-07-19 07:43:01 +00:00
pub(crate) fn to_node(&self, db: &impl AstDatabase) -> N {
2019-03-26 16:00:11 +00:00
let syntax_node = db.ast_id_to_node(self.file_id, self.file_ast_id.raw);
2019-07-19 07:43:01 +00:00
N::cast(syntax_node).unwrap()
2019-03-26 14:25:14 +00:00
}
}
2019-03-26 15:57:57 +00:00
/// `AstId` points to an AST node in a specific file.
2019-03-26 15:27:22 +00:00
#[derive(Debug)]
2019-03-26 14:25:14 +00:00
pub(crate) struct FileAstId<N: AstNode> {
2019-03-26 16:00:11 +00:00
raw: ErasedFileAstId,
_ty: PhantomData<fn() -> N>,
2019-03-26 14:25:14 +00:00
}
impl<N: AstNode> Clone for FileAstId<N> {
fn clone(&self) -> FileAstId<N> {
*self
}
}
impl<N: AstNode> Copy for FileAstId<N> {}
2019-03-26 15:27:22 +00:00
impl<N: AstNode> PartialEq for FileAstId<N> {
fn eq(&self, other: &Self) -> bool {
self.raw == other.raw
}
}
impl<N: AstNode> Eq for FileAstId<N> {}
impl<N: AstNode> Hash for FileAstId<N> {
fn hash<H: Hasher>(&self, hasher: &mut H) {
self.raw.hash(hasher);
}
}
2019-03-26 14:25:14 +00:00
impl<N: AstNode> FileAstId<N> {
pub(crate) fn with_file_id(self, file_id: HirFileId) -> AstId<N> {
AstId { file_id, file_ast_id: self }
}
}
2019-03-26 11:40:34 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
2019-03-26 16:00:11 +00:00
pub struct ErasedFileAstId(RawId);
impl_arena_id!(ErasedFileAstId);
2019-03-26 11:40:34 +00:00
2019-03-26 16:00:11 +00:00
/// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back.
2019-05-13 22:42:59 +00:00
#[derive(Debug, PartialEq, Eq, Default)]
2019-03-26 16:00:11 +00:00
pub struct AstIdMap {
arena: Arena<ErasedFileAstId, SyntaxNodePtr>,
2019-03-26 11:40:34 +00:00
}
2019-03-26 16:00:11 +00:00
impl AstIdMap {
2019-06-01 18:17:57 +00:00
pub(crate) fn ast_id_map_query(db: &impl AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
2019-05-13 22:42:59 +00:00
let map = if let Some(node) = db.parse_or_expand(file_id) {
2019-07-19 07:43:01 +00:00
AstIdMap::from_source(&node)
2019-05-13 22:42:59 +00:00
} else {
AstIdMap::default()
};
Arc::new(map)
2019-03-26 11:40:34 +00:00
}
pub(crate) fn file_item_query(
2019-06-01 18:17:57 +00:00
db: &impl AstDatabase,
2019-03-26 16:00:11 +00:00
file_id: HirFileId,
ast_id: ErasedFileAstId,
2019-07-19 07:43:01 +00:00
) -> SyntaxNode {
2019-05-13 22:42:59 +00:00
let node = db.parse_or_expand(file_id).unwrap();
2019-07-19 07:43:01 +00:00
db.ast_id_map(file_id).arena[ast_id].to_node(&node)
2019-03-26 11:40:34 +00:00
}
2019-03-26 15:32:46 +00:00
pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {
2019-03-26 15:57:57 +00:00
let ptr = SyntaxNodePtr::new(item.syntax());
let raw = match self.arena.iter().find(|(_id, i)| **i == ptr) {
Some((it, _)) => it,
None => panic!(
2019-03-26 16:00:11 +00:00
"Can't find {:?} in AstIdMap:\n{:?}",
2019-03-26 15:57:57 +00:00
item.syntax(),
self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
),
};
FileAstId { raw, _ty: PhantomData }
2019-03-26 15:32:46 +00:00
}
2019-05-13 16:39:06 +00:00
fn from_source(node: &SyntaxNode) -> AstIdMap {
assert!(node.parent().is_none());
2019-03-26 16:00:11 +00:00
let mut res = AstIdMap { arena: Arena::default() };
2019-03-26 11:40:34 +00:00
// By walking the tree in bread-first order we make sure that parents
// get lower ids then children. That is, adding a new child does not
// change parent's id. This means that, say, adding a new function to a
// trait does not change ids of top-level items, which helps caching.
2019-05-13 16:39:06 +00:00
bfs(node, |it| {
2019-07-19 07:43:01 +00:00
if let Some(module_item) = ast::ModuleItem::cast(it.clone()) {
2019-03-26 11:40:34 +00:00
res.alloc(module_item.syntax());
} else if let Some(macro_call) = ast::MacroCall::cast(it) {
res.alloc(macro_call.syntax());
}
});
res
}
2019-03-26 16:00:11 +00:00
fn alloc(&mut self, item: &SyntaxNode) -> ErasedFileAstId {
2019-03-26 11:40:34 +00:00
self.arena.alloc(SyntaxNodePtr::new(item))
}
}
/// Walks the subtree in bfs order, calling `f` for each node.
2019-07-19 07:43:01 +00:00
fn bfs(node: &SyntaxNode, mut f: impl FnMut(SyntaxNode)) {
let mut curr_layer = vec![node.clone()];
2019-03-26 11:40:34 +00:00
let mut next_layer = vec![];
while !curr_layer.is_empty() {
curr_layer.drain(..).for_each(|node| {
next_layer.extend(node.children());
f(node);
});
std::mem::swap(&mut curr_layer, &mut next_layer);
}
}