1 //! In rust-analyzer, syntax trees are transient objects.
3 //! That means that we create trees when we need them, and tear them down to
4 //! save memory. In this architecture, hanging on to a particular syntax node
5 //! for a long time is ill-advisable, as that keeps the whole tree resident.
7 //! Instead, we provide a [`SyntaxNodePtr`] type, which stores information about
8 //! *location* of a particular syntax node in a tree. Its a small type which can
9 //! be cheaply stored, and which can be resolved to a real [`SyntaxNode`] when
18 use crate::{AstNode, SyntaxKind, SyntaxNode, TextRange};
20 /// A pointer to a syntax node inside a file. It can be used to remember a
21 /// specific node across reparses of the same file.
22 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
23 pub struct SyntaxNodePtr {
24 // Don't expose this field further. At some point, we might want to replace
25 // range with node id.
26 pub(crate) range: TextRange,
31 pub fn new(node: &SyntaxNode) -> SyntaxNodePtr {
32 SyntaxNodePtr { range: node.text_range(), kind: node.kind() }
35 /// "Dereference" the pointer to get the node it points to.
37 /// Panics if node is not found, so make sure that `root` syntax tree is
38 /// equivalent (is build from the same text) to the tree which was
39 /// originally used to get this [`SyntaxNodePtr`].
41 /// The complexity is linear in the depth of the tree and logarithmic in
42 /// tree width. As most trees are shallow, thinking about this as
43 /// `O(log(N))` in the size of the tree is not too wrong!
44 pub fn to_node(&self, root: &SyntaxNode) -> SyntaxNode {
45 assert!(root.parent().is_none());
46 successors(Some(root.clone()), |node| {
47 node.child_or_token_at_range(self.range).and_then(|it| it.into_node())
49 .find(|it| it.text_range() == self.range && it.kind() == self.kind)
50 .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self))
53 pub fn cast<N: AstNode>(self) -> Option<AstPtr<N>> {
54 if !N::can_cast(self.kind) {
57 Some(AstPtr { raw: self, _ty: PhantomData })
61 /// Like `SyntaxNodePtr`, but remembers the type of node
63 pub struct AstPtr<N: AstNode> {
65 _ty: PhantomData<fn() -> N>,
68 impl<N: AstNode> Clone for AstPtr<N> {
69 fn clone(&self) -> AstPtr<N> {
70 AstPtr { raw: self.raw.clone(), _ty: PhantomData }
74 impl<N: AstNode> Eq for AstPtr<N> {}
76 impl<N: AstNode> PartialEq for AstPtr<N> {
77 fn eq(&self, other: &AstPtr<N>) -> bool {
82 impl<N: AstNode> Hash for AstPtr<N> {
83 fn hash<H: Hasher>(&self, state: &mut H) {
88 impl<N: AstNode> AstPtr<N> {
89 pub fn new(node: &N) -> AstPtr<N> {
90 AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData }
93 pub fn to_node(&self, root: &SyntaxNode) -> N {
94 let syntax_node = self.raw.to_node(root);
95 N::cast(syntax_node).unwrap()
98 pub fn syntax_node_ptr(&self) -> SyntaxNodePtr {
102 pub fn cast<U: AstNode>(self) -> Option<AstPtr<U>> {
103 if !U::can_cast(self.raw.kind) {
106 Some(AstPtr { raw: self.raw, _ty: PhantomData })
110 impl<N: AstNode> From<AstPtr<N>> for SyntaxNodePtr {
111 fn from(ptr: AstPtr<N>) -> SyntaxNodePtr {
117 fn test_local_syntax_ptr() {
118 use crate::{ast, AstNode, SourceFile};
120 let file = SourceFile::parse("struct Foo { f: u32, }").ok().unwrap();
121 let field = file.syntax().descendants().find_map(ast::RecordField::cast).unwrap();
122 let ptr = SyntaxNodePtr::new(field.syntax());
123 let field_syntax = ptr.to_node(file.syntax());
124 assert_eq!(field.syntax(), &field_syntax);