From d4ed25d86fdec0ce47199c262af62213b62e4863 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 23 Jan 2019 18:26:02 +0300 Subject: introduced better typed AstPtr --- crates/ra_syntax/src/lib.rs | 2 +- crates/ra_syntax/src/ptr.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 97b196118..104f32851 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -43,7 +43,7 @@ pub use crate::{ lexer::{tokenize, Token}, syntax_kinds::SyntaxKind, yellow::{Direction, SyntaxError, SyntaxNode, WalkEvent, Location, TreeArc}, - ptr::SyntaxNodePtr, + ptr::{SyntaxNodePtr, AstPtr}, }; use ra_text_edit::AtomTextEdit; diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs index e8c40e5d3..b50cd8a52 100644 --- a/crates/ra_syntax/src/ptr.rs +++ b/crates/ra_syntax/src/ptr.rs @@ -1,3 +1,5 @@ +use std::marker::PhantomData; + use crate::{ AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, algo::generate, @@ -37,6 +39,38 @@ impl SyntaxNodePtr { } } +/// Like `SyntaxNodePtr`, but remembers the type of node +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct AstPtr { + ptr: SyntaxNodePtr, + _ty: PhantomData, +} + +impl Copy for AstPtr {} +impl Clone for AstPtr { + fn clone(&self) -> AstPtr { + *self + } +} + +impl AstPtr { + pub fn new(node: &N) -> AstPtr { + AstPtr { + ptr: SyntaxNodePtr::new(node.syntax()), + _ty: PhantomData, + } + } + + pub fn to_node(self, source_file: &SourceFile) -> &N { + let syntax_node = self.ptr.to_node(source_file); + N::cast(syntax_node).unwrap() + } + + pub fn syntax_node_ptr(self) -> SyntaxNodePtr { + self.ptr + } +} + #[test] fn test_local_syntax_ptr() { use crate::{ast, AstNode}; -- cgit v1.2.3