From 5b803055b7b690a57f1c08da8f1640139c739e76 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 29 Oct 2019 14:59:55 +0300 Subject: rename hir_def -> hir_expand --- crates/ra_hir_def/src/ast_id_map.rs | 114 ------------------------------------ 1 file changed, 114 deletions(-) delete mode 100644 crates/ra_hir_def/src/ast_id_map.rs (limited to 'crates/ra_hir_def/src/ast_id_map.rs') diff --git a/crates/ra_hir_def/src/ast_id_map.rs b/crates/ra_hir_def/src/ast_id_map.rs deleted file mode 100644 index c3b389102..000000000 --- a/crates/ra_hir_def/src/ast_id_map.rs +++ /dev/null @@ -1,114 +0,0 @@ -//! `AstIdMap` allows to create stable IDs for "large" syntax nodes like items -//! and macro calls. -//! -//! Specifically, it enumerates all items in a file and uses position of a an -//! item as an ID. That way, id's don't change unless the set of items itself -//! changes. - -use std::{ - hash::{Hash, Hasher}, - marker::PhantomData, - ops, -}; - -use ra_arena::{impl_arena_id, Arena, RawId}; -use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxNodePtr}; - -/// `AstId` points to an AST node in a specific file. -#[derive(Debug)] -pub struct FileAstId { - raw: ErasedFileAstId, - _ty: PhantomData N>, -} - -impl Clone for FileAstId { - fn clone(&self) -> FileAstId { - *self - } -} -impl Copy for FileAstId {} - -impl PartialEq for FileAstId { - fn eq(&self, other: &Self) -> bool { - self.raw == other.raw - } -} -impl Eq for FileAstId {} -impl Hash for FileAstId { - fn hash(&self, hasher: &mut H) { - self.raw.hash(hasher); - } -} - -impl From> for ErasedFileAstId { - fn from(id: FileAstId) -> Self { - id.raw - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct ErasedFileAstId(RawId); -impl_arena_id!(ErasedFileAstId); - -/// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back. -#[derive(Debug, PartialEq, Eq, Default)] -pub struct AstIdMap { - arena: Arena, -} - -impl AstIdMap { - pub fn from_source(node: &SyntaxNode) -> AstIdMap { - assert!(node.parent().is_none()); - let mut res = AstIdMap { arena: Arena::default() }; - // 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. - bfs(node, |it| { - if let Some(module_item) = ast::ModuleItem::cast(it.clone()) { - res.alloc(module_item.syntax()); - } else if let Some(macro_call) = ast::MacroCall::cast(it) { - res.alloc(macro_call.syntax()); - } - }); - res - } - - pub fn ast_id(&self, item: &N) -> FileAstId { - let ptr = SyntaxNodePtr::new(item.syntax()); - let raw = match self.arena.iter().find(|(_id, i)| **i == ptr) { - Some((it, _)) => it, - None => panic!( - "Can't find {:?} in AstIdMap:\n{:?}", - item.syntax(), - self.arena.iter().map(|(_id, i)| i).collect::>(), - ), - }; - - FileAstId { raw, _ty: PhantomData } - } - - fn alloc(&mut self, item: &SyntaxNode) -> ErasedFileAstId { - self.arena.alloc(SyntaxNodePtr::new(item)) - } -} - -impl ops::Index for AstIdMap { - type Output = SyntaxNodePtr; - fn index(&self, index: ErasedFileAstId) -> &SyntaxNodePtr { - &self.arena[index] - } -} - -/// Walks the subtree in bfs order, calling `f` for each node. -fn bfs(node: &SyntaxNode, mut f: impl FnMut(SyntaxNode)) { - let mut curr_layer = vec![node.clone()]; - 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); - } -} -- cgit v1.2.3