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.rs148
1 files changed, 92 insertions, 56 deletions
diff --git a/crates/ra_syntax/src/yellow.rs b/crates/ra_syntax/src/yellow.rs
index cacd89dc8..1bf1806b9 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,76 @@ impl Types for RaTypes {
21 type RootData = Vec<SyntaxError>; 18 type RootData = Vec<SyntaxError>;
22} 19}
23 20
24pub type OwnedRoot = ::rowan::OwnedRoot<RaTypes>; 21pub type GreenNode = rowan::GreenNode<RaTypes>;
25pub type RefRoot<'a> = ::rowan::RefRoot<'a, RaTypes>;
26 22
27pub type GreenNode = ::rowan::GreenNode<RaTypes>; 23#[derive(PartialEq, Eq, Hash)]
24pub struct TreePtr<T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>>(
25 pub(crate) rowan::TreePtr<RaTypes, T>,
26);
28 27
29#[derive(Clone, Copy)] 28impl<T> TreePtr<T>
30pub struct SyntaxNode<R: TreeRoot<RaTypes> = OwnedRoot>(pub(crate) ::rowan::SyntaxNode<RaTypes, R>); 29where
31pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>; 30 T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,
31{
32 pub(crate) fn cast<U>(this: TreePtr<T>) -> TreePtr<U>
33 where
34 U: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,
35 {
36 TreePtr(rowan::TreePtr::cast(this.0))
37 }
38}
39
40impl<T> std::ops::Deref for TreePtr<T>
41where
42 T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,
43{
44 type Target = T;
45 fn deref(&self) -> &T {
46 self.0.deref()
47 }
48}
49
50impl<T> PartialEq<T> for TreePtr<T>
51where
52 T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,
53 T: PartialEq<T>,
54{
55 fn eq(&self, other: &T) -> bool {
56 let t: &T = self;
57 t == other
58 }
59}
32 60
33impl<R1, R2> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> 61impl<T> Clone for TreePtr<T>
34where 62where
35 R1: TreeRoot<RaTypes>, 63 T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,
36 R2: TreeRoot<RaTypes>,
37{ 64{
38 fn eq(&self, other: &SyntaxNode<R1>) -> bool { 65 fn clone(&self) -> TreePtr<T> {
39 self.0 == other.0 66 TreePtr(self.0.clone())
40 } 67 }
41} 68}
42 69
43impl<R: TreeRoot<RaTypes>> Eq for SyntaxNode<R> {} 70impl<T> fmt::Debug for TreePtr<T>
44impl<R: TreeRoot<RaTypes>> Hash for SyntaxNode<R> { 71where
45 fn hash<H: Hasher>(&self, state: &mut H) { 72 T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,
46 self.0.hash(state) 73 T: fmt::Debug,
74{
75 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
76 fmt::Debug::fmt(&self.0, fmt)
47 } 77 }
48} 78}
49 79
80#[derive(PartialEq, Eq, Hash)]
81#[repr(transparent)]
82pub struct SyntaxNode(pub(crate) rowan::SyntaxNode<RaTypes>);
83unsafe impl TransparentNewType for SyntaxNode {
84 type Repr = rowan::SyntaxNode<RaTypes>;
85}
86
50impl SyntaxNode { 87impl SyntaxNode {
51 pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxNode { 88 pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreePtr<SyntaxNode> {
52 SyntaxNode(::rowan::SyntaxNode::new(green, errors)) 89 let ptr = TreePtr(rowan::SyntaxNode::new(green, errors));
90 TreePtr::cast(ptr)
53 } 91 }
54} 92}
55 93
@@ -59,45 +97,43 @@ pub enum Direction {
59 Prev, 97 Prev,
60} 98}
61 99
62impl<'a> SyntaxNodeRef<'a> { 100impl SyntaxNode {
63 pub fn leaf_text(self) -> Option<&'a SmolStr> { 101 pub fn leaf_text(&self) -> Option<&SmolStr> {
64 self.0.leaf_text() 102 self.0.leaf_text()
65 } 103 }
66 pub fn ancestors(self) -> impl Iterator<Item = SyntaxNodeRef<'a>> { 104 pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> {
67 crate::algo::generate(Some(self), |&node| node.parent()) 105 crate::algo::generate(Some(self), |&node| node.parent())
68 } 106 }
69 pub fn descendants(self) -> impl Iterator<Item = SyntaxNodeRef<'a>> { 107 pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> {
70 self.preorder().filter_map(|event| match event { 108 self.preorder().filter_map(|event| match event {
71 WalkEvent::Enter(node) => Some(node), 109 WalkEvent::Enter(node) => Some(node),
72 WalkEvent::Leave(_) => None, 110 WalkEvent::Leave(_) => None,
73 }) 111 })
74 } 112 }
75 pub fn siblings(self, direction: Direction) -> impl Iterator<Item = SyntaxNodeRef<'a>> { 113 pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> {
76 crate::algo::generate(Some(self), move |&node| match direction { 114 crate::algo::generate(Some(self), move |&node| match direction {
77 Direction::Next => node.next_sibling(), 115 Direction::Next => node.next_sibling(),
78 Direction::Prev => node.prev_sibling(), 116 Direction::Prev => node.prev_sibling(),
79 }) 117 })
80 } 118 }
81 pub fn preorder(self) -> impl Iterator<Item = WalkEvent<SyntaxNodeRef<'a>>> { 119 pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&SyntaxNode>> {
82 self.0.preorder().map(|event| match event { 120 self.0.preorder().map(|event| match event {
83 WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode(n)), 121 WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode::from_repr(n)),
84 WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode(n)), 122 WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode::from_repr(n)),
85 }) 123 })
86 } 124 }
87} 125}
88 126
89impl<R: TreeRoot<RaTypes>> SyntaxNode<R> { 127impl SyntaxNode {
90 pub(crate) fn root_data(&self) -> &Vec<SyntaxError> { 128 pub(crate) fn root_data(&self) -> &Vec<SyntaxError> {
91 self.0.root_data() 129 self.0.root_data()
92 } 130 }
93 pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { 131 pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode {
94 self.0.replace_with(replacement) 132 self.0.replace_self(replacement)
95 }
96 pub fn borrowed<'a>(&'a self) -> SyntaxNode<RefRoot<'a>> {
97 SyntaxNode(self.0.borrowed())
98 } 133 }
99 pub fn owned(&self) -> SyntaxNode<OwnedRoot> { 134 pub fn to_owned(&self) -> TreePtr<SyntaxNode> {
100 SyntaxNode(self.0.owned()) 135 let ptr = TreePtr(self.0.to_owned());
136 TreePtr::cast(ptr)
101 } 137 }
102 pub fn kind(&self) -> SyntaxKind { 138 pub fn kind(&self) -> SyntaxKind {
103 self.0.kind() 139 self.0.kind()
@@ -106,32 +142,32 @@ impl<R: TreeRoot<RaTypes>> SyntaxNode<R> {
106 self.0.range() 142 self.0.range()
107 } 143 }
108 pub fn text(&self) -> SyntaxText { 144 pub fn text(&self) -> SyntaxText {
109 SyntaxText::new(self.borrowed()) 145 SyntaxText::new(self)
110 } 146 }
111 pub fn is_leaf(&self) -> bool { 147 pub fn is_leaf(&self) -> bool {
112 self.0.is_leaf() 148 self.0.is_leaf()
113 } 149 }
114 pub fn parent(&self) -> Option<SyntaxNode<R>> { 150 pub fn parent(&self) -> Option<&SyntaxNode> {
115 self.0.parent().map(SyntaxNode) 151 self.0.parent().map(SyntaxNode::from_repr)
116 } 152 }
117 pub fn first_child(&self) -> Option<SyntaxNode<R>> { 153 pub fn first_child(&self) -> Option<&SyntaxNode> {
118 self.0.first_child().map(SyntaxNode) 154 self.0.first_child().map(SyntaxNode::from_repr)
119 } 155 }
120 pub fn last_child(&self) -> Option<SyntaxNode<R>> { 156 pub fn last_child(&self) -> Option<&SyntaxNode> {
121 self.0.last_child().map(SyntaxNode) 157 self.0.last_child().map(SyntaxNode::from_repr)
122 } 158 }
123 pub fn next_sibling(&self) -> Option<SyntaxNode<R>> { 159 pub fn next_sibling(&self) -> Option<&SyntaxNode> {
124 self.0.next_sibling().map(SyntaxNode) 160 self.0.next_sibling().map(SyntaxNode::from_repr)
125 } 161 }
126 pub fn prev_sibling(&self) -> Option<SyntaxNode<R>> { 162 pub fn prev_sibling(&self) -> Option<&SyntaxNode> {
127 self.0.prev_sibling().map(SyntaxNode) 163 self.0.prev_sibling().map(SyntaxNode::from_repr)
128 } 164 }
129 pub fn children(&self) -> SyntaxNodeChildren<R> { 165 pub fn children(&self) -> SyntaxNodeChildren {
130 SyntaxNodeChildren(self.0.children()) 166 SyntaxNodeChildren(self.0.children())
131 } 167 }
132} 168}
133 169
134impl<R: TreeRoot<RaTypes>> fmt::Debug for SyntaxNode<R> { 170impl fmt::Debug for SyntaxNode {
135 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 171 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
136 write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; 172 write!(fmt, "{:?}@{:?}", self.kind(), self.range())?;
137 if has_short_text(self.kind()) { 173 if has_short_text(self.kind()) {
@@ -142,13 +178,13 @@ impl<R: TreeRoot<RaTypes>> fmt::Debug for SyntaxNode<R> {
142} 178}
143 179
144#[derive(Debug)] 180#[derive(Debug)]
145pub struct SyntaxNodeChildren<R: TreeRoot<RaTypes>>(::rowan::SyntaxNodeChildren<RaTypes, R>); 181pub struct SyntaxNodeChildren<'a>(rowan::SyntaxNodeChildren<'a, RaTypes>);
146 182
147impl<R: TreeRoot<RaTypes>> Iterator for SyntaxNodeChildren<R> { 183impl<'a> Iterator for SyntaxNodeChildren<'a> {
148 type Item = SyntaxNode<R>; 184 type Item = &'a SyntaxNode;
149 185
150 fn next(&mut self) -> Option<SyntaxNode<R>> { 186 fn next(&mut self) -> Option<&'a SyntaxNode> {
151 self.0.next().map(SyntaxNode) 187 self.0.next().map(SyntaxNode::from_repr)
152 } 188 }
153} 189}
154 190