aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/yellow.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/yellow.rs')
-rw-r--r--crates/ra_syntax/src/yellow.rs104
1 files changed, 41 insertions, 63 deletions
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;
4 4
5use self::syntax_text::SyntaxText; 5use self::syntax_text::SyntaxText;
6use crate::{SmolStr, SyntaxKind, TextRange}; 6use crate::{SmolStr, SyntaxKind, TextRange};
7use rowan::Types; 7use rowan::{Types, TransparentNewType};
8use std::{ 8use std::fmt;
9 fmt,
10 hash::{Hash, Hasher},
11};
12 9
13pub(crate) use self::builder::GreenBuilder; 10pub(crate) use self::builder::GreenBuilder;
14pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location}; 11pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location};
15pub use rowan::{TreeRoot, WalkEvent}; 12pub use rowan::WalkEvent;
16 13
17#[derive(Debug, Clone, Copy)] 14#[derive(Debug, Clone, Copy)]
18pub enum RaTypes {} 15pub enum RaTypes {}
@@ -21,35 +18,19 @@ impl Types for RaTypes {
21 type RootData = Vec<SyntaxError>; 18 type RootData = Vec<SyntaxError>;
22} 19}
23 20
24pub type OwnedRoot = ::rowan::OwnedRoot<RaTypes>;
25pub type RefRoot<'a> = ::rowan::RefRoot<'a, RaTypes>;
26
27pub type GreenNode = ::rowan::GreenNode<RaTypes>; 21pub type GreenNode = ::rowan::GreenNode<RaTypes>;
22pub type TreePtr<T> = ::rowan::TreePtr<RaTypes, T>;
28 23
29#[derive(Clone, Copy)] 24#[derive(PartialEq, Eq, Hash)]
30pub struct SyntaxNode<R: TreeRoot<RaTypes> = OwnedRoot>(pub(crate) ::rowan::SyntaxNode<RaTypes, R>); 25#[repr(transparent)]
31pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>; 26pub struct SyntaxNode(pub(crate) ::rowan::SyntaxNode<RaTypes>);
32 27unsafe impl TransparentNewType for SyntaxNode {
33impl<R1, R2> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> 28 type Repr = ::rowan::SyntaxNode<RaTypes>;
34where
35 R1: TreeRoot<RaTypes>,
36 R2: TreeRoot<RaTypes>,
37{
38 fn eq(&self, other: &SyntaxNode<R1>) -> bool {
39 self.0 == other.0
40 }
41}
42
43impl<R: TreeRoot<RaTypes>> Eq for SyntaxNode<R> {}
44impl<R: TreeRoot<RaTypes>> Hash for SyntaxNode<R> {
45 fn hash<H: Hasher>(&self, state: &mut H) {
46 self.0.hash(state)
47 }
48} 29}
49 30
50impl SyntaxNode { 31impl SyntaxNode {
51 pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxNode { 32 pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreePtr<SyntaxNode> {
52 SyntaxNode(::rowan::SyntaxNode::new(green, errors)) 33 TreePtr::cast(::rowan::SyntaxNode::new(green, errors))
53 } 34 }
54} 35}
55 36
@@ -59,45 +40,42 @@ pub enum Direction {
59 Prev, 40 Prev,
60} 41}
61 42
62impl<'a> SyntaxNodeRef<'a> { 43impl SyntaxNode {
63 pub fn leaf_text(self) -> Option<&'a SmolStr> { 44 pub fn leaf_text(&self) -> Option<&SmolStr> {
64 self.0.leaf_text() 45 self.0.leaf_text()
65 } 46 }
66 pub fn ancestors(self) -> impl Iterator<Item = SyntaxNodeRef<'a>> { 47 pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> {
67 crate::algo::generate(Some(self), |&node| node.parent()) 48 crate::algo::generate(Some(self), |&node| node.parent())
68 } 49 }
69 pub fn descendants(self) -> impl Iterator<Item = SyntaxNodeRef<'a>> { 50 pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> {
70 self.preorder().filter_map(|event| match event { 51 self.preorder().filter_map(|event| match event {
71 WalkEvent::Enter(node) => Some(node), 52 WalkEvent::Enter(node) => Some(node),
72 WalkEvent::Leave(_) => None, 53 WalkEvent::Leave(_) => None,
73 }) 54 })
74 } 55 }
75 pub fn siblings(self, direction: Direction) -> impl Iterator<Item = SyntaxNodeRef<'a>> { 56 pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> {
76 crate::algo::generate(Some(self), move |&node| match direction { 57 crate::algo::generate(Some(self), move |&node| match direction {
77 Direction::Next => node.next_sibling(), 58 Direction::Next => node.next_sibling(),
78 Direction::Prev => node.prev_sibling(), 59 Direction::Prev => node.prev_sibling(),
79 }) 60 })
80 } 61 }
81 pub fn preorder(self) -> impl Iterator<Item = WalkEvent<SyntaxNodeRef<'a>>> { 62 pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&SyntaxNode>> {
82 self.0.preorder().map(|event| match event { 63 self.0.preorder().map(|event| match event {
83 WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode(n)), 64 WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode::from_repr(n)),
84 WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode(n)), 65 WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode::from_repr(n)),
85 }) 66 })
86 } 67 }
87} 68}
88 69
89impl<R: TreeRoot<RaTypes>> SyntaxNode<R> { 70impl SyntaxNode {
90 pub(crate) fn root_data(&self) -> &Vec<SyntaxError> { 71 pub(crate) fn root_data(&self) -> &Vec<SyntaxError> {
91 self.0.root_data() 72 self.0.root_data()
92 } 73 }
93 pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { 74 pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode {
94 self.0.replace_with(replacement) 75 self.0.replace_self(replacement)
95 }
96 pub fn borrowed<'a>(&'a self) -> SyntaxNode<RefRoot<'a>> {
97 SyntaxNode(self.0.borrowed())
98 } 76 }
99 pub fn owned(&self) -> SyntaxNode<OwnedRoot> { 77 pub fn to_owned(&self) -> TreePtr<SyntaxNode> {
100 SyntaxNode(self.0.owned()) 78 TreePtr::cast(self.0.to_owned())
101 } 79 }
102 pub fn kind(&self) -> SyntaxKind { 80 pub fn kind(&self) -> SyntaxKind {
103 self.0.kind() 81 self.0.kind()
@@ -106,32 +84,32 @@ impl<R: TreeRoot<RaTypes>> SyntaxNode<R> {
106 self.0.range() 84 self.0.range()
107 } 85 }
108 pub fn text(&self) -> SyntaxText { 86 pub fn text(&self) -> SyntaxText {
109 SyntaxText::new(self.borrowed()) 87 SyntaxText::new(self)
110 } 88 }
111 pub fn is_leaf(&self) -> bool { 89 pub fn is_leaf(&self) -> bool {
112 self.0.is_leaf() 90 self.0.is_leaf()
113 } 91 }
114 pub fn parent(&self) -> Option<SyntaxNode<R>> { 92 pub fn parent(&self) -> Option<&SyntaxNode> {
115 self.0.parent().map(SyntaxNode) 93 self.0.parent().map(SyntaxNode::from_repr)
116 } 94 }
117 pub fn first_child(&self) -> Option<SyntaxNode<R>> { 95 pub fn first_child(&self) -> Option<&SyntaxNode> {
118 self.0.first_child().map(SyntaxNode) 96 self.0.first_child().map(SyntaxNode::from_repr)
119 } 97 }
120 pub fn last_child(&self) -> Option<SyntaxNode<R>> { 98 pub fn last_child(&self) -> Option<&SyntaxNode> {
121 self.0.last_child().map(SyntaxNode) 99 self.0.last_child().map(SyntaxNode::from_repr)
122 } 100 }
123 pub fn next_sibling(&self) -> Option<SyntaxNode<R>> { 101 pub fn next_sibling(&self) -> Option<&SyntaxNode> {
124 self.0.next_sibling().map(SyntaxNode) 102 self.0.next_sibling().map(SyntaxNode::from_repr)
125 } 103 }
126 pub fn prev_sibling(&self) -> Option<SyntaxNode<R>> { 104 pub fn prev_sibling(&self) -> Option<&SyntaxNode> {
127 self.0.prev_sibling().map(SyntaxNode) 105 self.0.prev_sibling().map(SyntaxNode::from_repr)
128 } 106 }
129 pub fn children(&self) -> SyntaxNodeChildren<R> { 107 pub fn children(&self) -> SyntaxNodeChildren {
130 SyntaxNodeChildren(self.0.children()) 108 SyntaxNodeChildren(self.0.children())
131 } 109 }
132} 110}
133 111
134impl<R: TreeRoot<RaTypes>> fmt::Debug for SyntaxNode<R> { 112impl fmt::Debug for SyntaxNode {
135 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 113 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
136 write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; 114 write!(fmt, "{:?}@{:?}", self.kind(), self.range())?;
137 if has_short_text(self.kind()) { 115 if has_short_text(self.kind()) {
@@ -142,13 +120,13 @@ impl<R: TreeRoot<RaTypes>> fmt::Debug for SyntaxNode<R> {
142} 120}
143 121
144#[derive(Debug)] 122#[derive(Debug)]
145pub struct SyntaxNodeChildren<R: TreeRoot<RaTypes>>(::rowan::SyntaxNodeChildren<RaTypes, R>); 123pub struct SyntaxNodeChildren<'a>(::rowan::SyntaxNodeChildren<'a, RaTypes>);
146 124
147impl<R: TreeRoot<RaTypes>> Iterator for SyntaxNodeChildren<R> { 125impl<'a> Iterator for SyntaxNodeChildren<'a> {
148 type Item = SyntaxNode<R>; 126 type Item = &'a SyntaxNode;
149 127
150 fn next(&mut self) -> Option<SyntaxNode<R>> { 128 fn next(&mut self) -> Option<&'a SyntaxNode> {
151 self.0.next().map(SyntaxNode) 129 self.0.next().map(SyntaxNode::from_repr)
152 } 130 }
153} 131}
154 132