From a1c187eef3ba08076aedb5154929f7eda8d1b424 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 18:26:51 +0200 Subject: Rename ra_syntax -> syntax --- crates/ra_syntax/src/ptr.rs | 105 -------------------------------------------- 1 file changed, 105 deletions(-) delete mode 100644 crates/ra_syntax/src/ptr.rs (limited to 'crates/ra_syntax/src/ptr.rs') diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs deleted file mode 100644 index ca7957747..000000000 --- a/crates/ra_syntax/src/ptr.rs +++ /dev/null @@ -1,105 +0,0 @@ -//! FIXME: write short doc here - -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 { - pub(crate) range: TextRange, - kind: SyntaxKind, -} - -impl SyntaxNodePtr { - pub fn new(node: &SyntaxNode) -> SyntaxNodePtr { - SyntaxNodePtr { range: node.text_range(), kind: node.kind() } - } - - 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); -} -- cgit v1.2.3