//! In rust-analyzer, syntax trees are transient objects. //! //! That means that we create trees when we need them, and tear them down to //! save memory. In this architecture, hanging on to a particular syntax node //! for a long time is ill-advisable, as that keeps the whole tree resident. //! //! Instead, we provide a [`SyntaxNodePtr`] type, which stores information about //! *location* of a particular syntax node in a tree. Its a small type which can //! be cheaply stored, and which can be resolved to a real [`SyntaxNode`] when //! necessary. use std::{ hash::{Hash, Hasher}, iter::successors, marker::PhantomData, }; use crate::{AstNode, SyntaxKind, SyntaxNode, TextRange}; /// A pointer to a syntax node inside a file. It can be used to remember a /// specific node across reparses of the same file. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct SyntaxNodePtr { // Don't expose this field further. At some point, we might want to replace // range with node id. pub(crate) range: TextRange, kind: SyntaxKind, } impl SyntaxNodePtr { pub fn new(node: &SyntaxNode) -> SyntaxNodePtr { SyntaxNodePtr { range: node.text_range(), kind: node.kind() } } /// "Dereference" the pointer to get the node it points to. /// /// Panics if node is not found, so make sure that `root` syntax tree is /// equivalent (is build from the same text) to the tree which was /// originally used to get this [`SyntaxNodePtr`]. /// /// The complexity is linear in the depth of the tree and logarithmic in /// tree width. As most trees are shallow, thinking about this as /// `O(log(N))` in the size of the tree is not too wrong! pub fn to_node(&self, root: &SyntaxNode) -> SyntaxNode { assert!(root.parent().is_none()); successors(Some(root.clone()), |node| { node.children().find(|it| it.text_range().contains_range(self.range)) }) .find(|it| it.text_range() == self.range && it.kind() == self.kind) .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self)) } pub fn cast(self) -> Option> { if !N::can_cast(self.kind) { return None; } Some(AstPtr { raw: self, _ty: PhantomData }) } } /// Like `SyntaxNodePtr`, but remembers the type of node #[derive(Debug)] pub struct AstPtr { raw: SyntaxNodePtr, _ty: PhantomData N>, } impl Clone for AstPtr { fn clone(&self) -> AstPtr { AstPtr { raw: self.raw.clone(), _ty: PhantomData } } } impl Eq for AstPtr {} impl PartialEq for AstPtr { fn eq(&self, other: &AstPtr) -> bool { self.raw == other.raw } } impl Hash for AstPtr { fn hash(&self, state: &mut H) { self.raw.hash(state) } } impl AstPtr { pub fn new(node: &N) -> AstPtr { AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData } } pub fn to_node(&self, root: &SyntaxNode) -> N { let syntax_node = self.raw.to_node(root); N::cast(syntax_node).unwrap() } pub fn syntax_node_ptr(&self) -> SyntaxNodePtr { self.raw.clone() } pub fn cast(self) -> Option> { if !U::can_cast(self.raw.kind) { return None; } Some(AstPtr { raw: self.raw, _ty: PhantomData }) } } impl From> for SyntaxNodePtr { fn from(ptr: AstPtr) -> SyntaxNodePtr { ptr.raw } } #[test] fn test_local_syntax_ptr() { use crate::{ast, AstNode, SourceFile}; let file = SourceFile::parse("struct Foo { f: u32, }").ok().unwrap(); let field = file.syntax().descendants().find_map(ast::RecordField::cast).unwrap(); let ptr = SyntaxNodePtr::new(field.syntax()); let field_syntax = ptr.to_node(file.syntax()); assert_eq!(field.syntax(), &field_syntax); }