aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/src/yellow
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-09-07 23:16:07 +0100
committerAleksey Kladov <[email protected]>2018-09-07 23:16:07 +0100
commitff1c82216cc05f2621a301e30ab7a1102dea9d2b (patch)
tree393fb79e9fb06afe26eddfa66a59113a10dba26c /crates/libsyntax2/src/yellow
parentfcfda94664b454f60be2dbc1b564ed63aa4c3ec5 (diff)
Remove dyn dispatch
Diffstat (limited to 'crates/libsyntax2/src/yellow')
-rw-r--r--crates/libsyntax2/src/yellow/mod.rs2
-rw-r--r--crates/libsyntax2/src/yellow/syntax.rs37
2 files changed, 27 insertions, 12 deletions
diff --git a/crates/libsyntax2/src/yellow/mod.rs b/crates/libsyntax2/src/yellow/mod.rs
index 82eda79d6..0596e702f 100644
--- a/crates/libsyntax2/src/yellow/mod.rs
+++ b/crates/libsyntax2/src/yellow/mod.rs
@@ -8,7 +8,7 @@ use std::{
8 sync::Arc, 8 sync::Arc,
9 ptr, 9 ptr,
10}; 10};
11pub use self::syntax::{SyntaxNode, SyntaxNodeRef, SyntaxError}; 11pub use self::syntax::{SyntaxNode, SyntaxNodeRef, SyntaxError, SyntaxNodeChildren};
12pub(crate) use self::{ 12pub(crate) use self::{
13 builder::GreenBuilder, 13 builder::GreenBuilder,
14 green::GreenNode, 14 green::GreenNode,
diff --git a/crates/libsyntax2/src/yellow/syntax.rs b/crates/libsyntax2/src/yellow/syntax.rs
index 444dbeb30..1d99cab4a 100644
--- a/crates/libsyntax2/src/yellow/syntax.rs
+++ b/crates/libsyntax2/src/yellow/syntax.rs
@@ -1,6 +1,7 @@
1use std::{ 1use std::{
2 fmt, sync::Arc, 2 fmt, sync::Arc,
3 hash::{Hasher, Hash}, 3 hash::{Hasher, Hash},
4 ops::Range,
4}; 5};
5 6
6use smol_str::SmolStr; 7use smol_str::SmolStr;
@@ -93,17 +94,11 @@ impl<R: TreeRoot> SyntaxNode<R> {
93 SyntaxText::new(self.borrowed()) 94 SyntaxText::new(self.borrowed())
94 } 95 }
95 96
96 pub fn children(&self) -> impl Iterator<Item = SyntaxNode<R>> { 97 pub fn children(&self) -> SyntaxNodeChildren<R> {
97 let red = self.red; 98 SyntaxNodeChildren {
98 let n_children = self.red().n_children(); 99 parent: self.clone(),
99 let root = self.root.clone(); 100 iter: (0..self.red().n_children())
100 (0..n_children).map(move |i| { 101 }
101 let red = unsafe { red.get(root.syntax_root()) };
102 SyntaxNode {
103 root: root.clone(),
104 red: red.get_child(i).unwrap(),
105 }
106 })
107 } 102 }
108 103
109 pub fn parent(&self) -> Option<SyntaxNode<R>> { 104 pub fn parent(&self) -> Option<SyntaxNode<R>> {
@@ -192,6 +187,26 @@ impl<R: TreeRoot> fmt::Debug for SyntaxNode<R> {
192 } 187 }
193} 188}
194 189
190#[derive(Debug)]
191pub struct SyntaxNodeChildren<R: TreeRoot> {
192 parent: SyntaxNode<R>,
193 iter: Range<usize>,
194}
195
196impl<R: TreeRoot> Iterator for SyntaxNodeChildren<R> {
197 type Item = SyntaxNode<R>;
198
199 fn next(&mut self) -> Option<SyntaxNode<R>> {
200 self.iter.next().map(|i| {
201 let red = self.parent.red();
202 SyntaxNode {
203 root: self.parent.root.clone(),
204 red: red.get_child(i).unwrap(),
205 }
206 })
207 }
208}
209
195fn has_short_text(kind: SyntaxKind) -> bool { 210fn has_short_text(kind: SyntaxKind) -> bool {
196 match kind { 211 match kind {
197 IDENT | LIFETIME | INT_NUMBER | FLOAT_NUMBER => true, 212 IDENT | LIFETIME | INT_NUMBER | FLOAT_NUMBER => true,