use std::{collections::HashMap, fmt::Debug, hash::Hash}; use biome_js_syntax::JsSyntaxNode; use biome_rowan::TextRange; pub type Node = JsSyntaxNode; #[derive(Default)] pub struct NodeTable { id_to_node: HashMap, node_to_id: HashMap, } impl Debug for NodeTable { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("NodeTable") .field( "id_to_node", &self.id_to_node.iter().map(|(k, v)| (k, v.text())), ) .field( "node_to_id", &self.node_to_id.iter().map(|(k, v)| (k.text(), v)), ) .finish() } } impl NodeTable { pub fn insert_node(&mut self, node: Node) -> NodeId { let node_id = NodeId::from_range(node.text_trimmed_range()); self.id_to_node.insert(node_id, node.clone()); self.node_to_id.insert(node, node_id); node_id } pub fn get_node(&self, node_id: NodeId) -> Option<&Node> { self.id_to_node.get(&node_id) } pub fn get_id(&self, node: Node) -> Option<&NodeId> { self.node_to_id.get(&node) } } /** * Node id */ #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] pub struct NodeId(TextRange); impl NodeId { fn from_range(range: TextRange) -> Self { Self(range) } }