From d91a98ec843ec8562c58ccb01a1e29d00cc744dc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 7 Jan 2019 16:15:47 +0300 Subject: switch ra_syntax to new rowan API --- crates/ra_syntax/src/yellow.rs | 104 ++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 63 deletions(-) (limited to 'crates/ra_syntax/src/yellow.rs') diff --git a/crates/ra_syntax/src/yellow.rs b/crates/ra_syntax/src/yellow.rs index cacd89dc8..6dc846f33 100644 --- a/crates/ra_syntax/src/yellow.rs +++ b/crates/ra_syntax/src/yellow.rs @@ -4,15 +4,12 @@ mod syntax_text; use self::syntax_text::SyntaxText; use crate::{SmolStr, SyntaxKind, TextRange}; -use rowan::Types; -use std::{ - fmt, - hash::{Hash, Hasher}, -}; +use rowan::{Types, TransparentNewType}; +use std::fmt; pub(crate) use self::builder::GreenBuilder; pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location}; -pub use rowan::{TreeRoot, WalkEvent}; +pub use rowan::WalkEvent; #[derive(Debug, Clone, Copy)] pub enum RaTypes {} @@ -21,35 +18,19 @@ impl Types for RaTypes { type RootData = Vec; } -pub type OwnedRoot = ::rowan::OwnedRoot; -pub type RefRoot<'a> = ::rowan::RefRoot<'a, RaTypes>; - pub type GreenNode = ::rowan::GreenNode; +pub type TreePtr = ::rowan::TreePtr; -#[derive(Clone, Copy)] -pub struct SyntaxNode = OwnedRoot>(pub(crate) ::rowan::SyntaxNode); -pub type SyntaxNodeRef<'a> = SyntaxNode>; - -impl PartialEq> for SyntaxNode -where - R1: TreeRoot, - R2: TreeRoot, -{ - fn eq(&self, other: &SyntaxNode) -> bool { - self.0 == other.0 - } -} - -impl> Eq for SyntaxNode {} -impl> Hash for SyntaxNode { - fn hash(&self, state: &mut H) { - self.0.hash(state) - } +#[derive(PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct SyntaxNode(pub(crate) ::rowan::SyntaxNode); +unsafe impl TransparentNewType for SyntaxNode { + type Repr = ::rowan::SyntaxNode; } impl SyntaxNode { - pub(crate) fn new(green: GreenNode, errors: Vec) -> SyntaxNode { - SyntaxNode(::rowan::SyntaxNode::new(green, errors)) + pub(crate) fn new(green: GreenNode, errors: Vec) -> TreePtr { + TreePtr::cast(::rowan::SyntaxNode::new(green, errors)) } } @@ -59,45 +40,42 @@ pub enum Direction { Prev, } -impl<'a> SyntaxNodeRef<'a> { - pub fn leaf_text(self) -> Option<&'a SmolStr> { +impl SyntaxNode { + pub fn leaf_text(&self) -> Option<&SmolStr> { self.0.leaf_text() } - pub fn ancestors(self) -> impl Iterator> { + pub fn ancestors(&self) -> impl Iterator { crate::algo::generate(Some(self), |&node| node.parent()) } - pub fn descendants(self) -> impl Iterator> { + pub fn descendants(&self) -> impl Iterator { self.preorder().filter_map(|event| match event { WalkEvent::Enter(node) => Some(node), WalkEvent::Leave(_) => None, }) } - pub fn siblings(self, direction: Direction) -> impl Iterator> { + pub fn siblings(&self, direction: Direction) -> impl Iterator { crate::algo::generate(Some(self), move |&node| match direction { Direction::Next => node.next_sibling(), Direction::Prev => node.prev_sibling(), }) } - pub fn preorder(self) -> impl Iterator>> { + pub fn preorder(&self) -> impl Iterator> { self.0.preorder().map(|event| match event { - WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode(n)), - WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode(n)), + WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode::from_repr(n)), + WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode::from_repr(n)), }) } } -impl> SyntaxNode { +impl SyntaxNode { pub(crate) fn root_data(&self) -> &Vec { self.0.root_data() } pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { - self.0.replace_with(replacement) - } - pub fn borrowed<'a>(&'a self) -> SyntaxNode> { - SyntaxNode(self.0.borrowed()) + self.0.replace_self(replacement) } - pub fn owned(&self) -> SyntaxNode { - SyntaxNode(self.0.owned()) + pub fn to_owned(&self) -> TreePtr { + TreePtr::cast(self.0.to_owned()) } pub fn kind(&self) -> SyntaxKind { self.0.kind() @@ -106,32 +84,32 @@ impl> SyntaxNode { self.0.range() } pub fn text(&self) -> SyntaxText { - SyntaxText::new(self.borrowed()) + SyntaxText::new(self) } pub fn is_leaf(&self) -> bool { self.0.is_leaf() } - pub fn parent(&self) -> Option> { - self.0.parent().map(SyntaxNode) + pub fn parent(&self) -> Option<&SyntaxNode> { + self.0.parent().map(SyntaxNode::from_repr) } - pub fn first_child(&self) -> Option> { - self.0.first_child().map(SyntaxNode) + pub fn first_child(&self) -> Option<&SyntaxNode> { + self.0.first_child().map(SyntaxNode::from_repr) } - pub fn last_child(&self) -> Option> { - self.0.last_child().map(SyntaxNode) + pub fn last_child(&self) -> Option<&SyntaxNode> { + self.0.last_child().map(SyntaxNode::from_repr) } - pub fn next_sibling(&self) -> Option> { - self.0.next_sibling().map(SyntaxNode) + pub fn next_sibling(&self) -> Option<&SyntaxNode> { + self.0.next_sibling().map(SyntaxNode::from_repr) } - pub fn prev_sibling(&self) -> Option> { - self.0.prev_sibling().map(SyntaxNode) + pub fn prev_sibling(&self) -> Option<&SyntaxNode> { + self.0.prev_sibling().map(SyntaxNode::from_repr) } - pub fn children(&self) -> SyntaxNodeChildren { + pub fn children(&self) -> SyntaxNodeChildren { SyntaxNodeChildren(self.0.children()) } } -impl> fmt::Debug for SyntaxNode { +impl fmt::Debug for SyntaxNode { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; if has_short_text(self.kind()) { @@ -142,13 +120,13 @@ impl> fmt::Debug for SyntaxNode { } #[derive(Debug)] -pub struct SyntaxNodeChildren>(::rowan::SyntaxNodeChildren); +pub struct SyntaxNodeChildren<'a>(::rowan::SyntaxNodeChildren<'a, RaTypes>); -impl> Iterator for SyntaxNodeChildren { - type Item = SyntaxNode; +impl<'a> Iterator for SyntaxNodeChildren<'a> { + type Item = &'a SyntaxNode; - fn next(&mut self) -> Option> { - self.0.next().map(SyntaxNode) + fn next(&mut self) -> Option<&'a SyntaxNode> { + self.0.next().map(SyntaxNode::from_repr) } } -- cgit v1.2.3