aboutsummaryrefslogtreecommitdiff
path: root/src/yellow/syntax.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-07-31 11:49:03 +0100
committerAleksey Kladov <[email protected]>2018-07-31 11:49:03 +0100
commit407ebbc552fd9a8e73a9e46873ab834c54cea967 (patch)
treeb68be1e094307d54ac060181a8c752d9d7fb1db8 /src/yellow/syntax.rs
parent87b5e14c75dbc02b5bc610dfa33d5789570df5db (diff)
More fool-proof API
Diffstat (limited to 'src/yellow/syntax.rs')
-rw-r--r--src/yellow/syntax.rs17
1 files changed, 8 insertions, 9 deletions
diff --git a/src/yellow/syntax.rs b/src/yellow/syntax.rs
index 58e8ab9b6..41dcf3761 100644
--- a/src/yellow/syntax.rs
+++ b/src/yellow/syntax.rs
@@ -6,8 +6,10 @@ use {
6 TextRange, TextUnit, 6 TextRange, TextUnit,
7}; 7};
8 8
9pub trait TreeRoot: Deref<Target = SyntaxRoot> + Clone {} 9pub trait TreeRoot: Deref<Target=SyntaxRoot> + Clone {}
10
10impl TreeRoot for Arc<SyntaxRoot> {} 11impl TreeRoot for Arc<SyntaxRoot> {}
12
11impl<'a> TreeRoot for &'a SyntaxRoot {} 13impl<'a> TreeRoot for &'a SyntaxRoot {}
12 14
13#[derive(Clone, Copy)] 15#[derive(Clone, Copy)]
@@ -18,14 +20,13 @@ pub struct SyntaxNode<R: TreeRoot = Arc<SyntaxRoot>> {
18 red: ptr::NonNull<RedNode>, 20 red: ptr::NonNull<RedNode>,
19} 21}
20 22
21impl <R1: TreeRoot, R2: TreeRoot> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> { 23impl<R1: TreeRoot, R2: TreeRoot> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> {
22 fn eq(&self, other: &SyntaxNode<R1>) -> bool { 24 fn eq(&self, other: &SyntaxNode<R1>) -> bool {
23 self.red == other.red 25 self.red == other.red
24 } 26 }
25} 27}
26 28
27impl <R: TreeRoot> Eq for SyntaxNode<R> { 29impl<R: TreeRoot> Eq for SyntaxNode<R> {}
28}
29 30
30pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>; 31pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>;
31 32
@@ -88,7 +89,7 @@ impl<R: TreeRoot> SyntaxNode<R> {
88 (0..n_children).map(move |i| { 89 (0..n_children).map(move |i| {
89 SyntaxNode { 90 SyntaxNode {
90 root: self.root.clone(), 91 root: self.root.clone(),
91 red: red.nth_child(i), 92 red: red.get_child(i).unwrap(),
92 } 93 }
93 }) 94 })
94 } 95 }
@@ -109,12 +110,10 @@ impl<R: TreeRoot> SyntaxNode<R> {
109 let red = self.red(); 110 let red = self.red();
110 let parent = self.parent()?; 111 let parent = self.parent()?;
111 let next_sibling_idx = red.index_in_parent()? + 1; 112 let next_sibling_idx = red.index_in_parent()? + 1;
112 if next_sibling_idx == parent.red().n_children() { 113 let sibling_red = parent.red().get_child(next_sibling_idx)?;
113 return None;
114 }
115 Some(SyntaxNode { 114 Some(SyntaxNode {
116 root: self.root.clone(), 115 root: self.root.clone(),
117 red: parent.red().nth_child(next_sibling_idx), 116 red: sibling_red,
118 }) 117 })
119 } 118 }
120 119