//! 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}, marker::PhantomData, }; use rowan::TextRange; use crate::{syntax_node::RustLanguage, AstNode, SyntaxNode}; /// A "pointer" to a [`SyntaxNode`], via location in the source code. pub type SyntaxNodePtr = rowan::ast::SyntaxNodePtr; /// 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 text_range(&self) -> TextRange { self.raw.text_range() } pub fn cast(self) -> Option> { if !U::can_cast(self.raw.kind()) { return None; } Some(AstPtr { raw: self.raw, _ty: PhantomData }) } pub fn upcast(self) -> AstPtr where N: Into, { AstPtr { raw: self.raw, _ty: PhantomData } } /// Like `SyntaxNodePtr::cast` but the trait bounds work out. pub fn try_from_raw(raw: SyntaxNodePtr) -> Option> { N::can_cast(raw.kind()).then(|| AstPtr { 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); }