diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/yellow/syntax.rs | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/src/yellow/syntax.rs b/src/yellow/syntax.rs index 19a9b8ac2..5c31a3f35 100644 --- a/src/yellow/syntax.rs +++ b/src/yellow/syntax.rs | |||
@@ -11,8 +11,8 @@ impl TreeRoot for Arc<SyntaxRoot> {} | |||
11 | impl<'a> TreeRoot for &'a SyntaxRoot {} | 11 | impl<'a> TreeRoot for &'a SyntaxRoot {} |
12 | 12 | ||
13 | #[derive(Clone, Copy)] | 13 | #[derive(Clone, Copy)] |
14 | pub struct SyntaxNode<ROOT: TreeRoot = Arc<SyntaxRoot>> { | 14 | pub struct SyntaxNode<R: TreeRoot = Arc<SyntaxRoot>> { |
15 | pub(crate) root: ROOT, | 15 | pub(crate) root: R, |
16 | // Guaranteed to not dangle, because `root` holds a | 16 | // Guaranteed to not dangle, because `root` holds a |
17 | // strong reference to red's ancestor | 17 | // strong reference to red's ancestor |
18 | red: ptr::NonNull<RedNode>, | 18 | red: ptr::NonNull<RedNode>, |
@@ -52,7 +52,7 @@ impl SyntaxNode<Arc<SyntaxRoot>> { | |||
52 | } | 52 | } |
53 | } | 53 | } |
54 | 54 | ||
55 | impl<ROOT: TreeRoot> SyntaxNode<ROOT> { | 55 | impl<R: TreeRoot> SyntaxNode<R> { |
56 | pub fn borrow<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> { | 56 | pub fn borrow<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> { |
57 | SyntaxNode { | 57 | SyntaxNode { |
58 | root: &*self.root, | 58 | root: &*self.root, |
@@ -73,20 +73,18 @@ impl<ROOT: TreeRoot> SyntaxNode<ROOT> { | |||
73 | self.red().green().text() | 73 | self.red().green().text() |
74 | } | 74 | } |
75 | 75 | ||
76 | pub fn children(&self) -> Vec<SyntaxNode<ROOT>> { | 76 | pub fn children<'a>(&'a self) -> impl Iterator<Item=SyntaxNode<R>> + 'a { |
77 | let red = self.red(); | 77 | let red = self.red(); |
78 | let n_children = red.n_children(); | 78 | let n_children = red.n_children(); |
79 | let mut res = Vec::with_capacity(n_children); | 79 | (0..n_children).map(move |i| { |
80 | for i in 0..n_children { | 80 | SyntaxNode { |
81 | res.push(SyntaxNode { | ||
82 | root: self.root.clone(), | 81 | root: self.root.clone(), |
83 | red: red.nth_child(i), | 82 | red: red.nth_child(i), |
84 | }); | 83 | } |
85 | } | 84 | }) |
86 | res | ||
87 | } | 85 | } |
88 | 86 | ||
89 | pub fn parent(&self) -> Option<SyntaxNode<ROOT>> { | 87 | pub fn parent(&self) -> Option<SyntaxNode<R>> { |
90 | let parent = self.red().parent()?; | 88 | let parent = self.red().parent()?; |
91 | Some(SyntaxNode { | 89 | Some(SyntaxNode { |
92 | root: self.root.clone(), | 90 | root: self.root.clone(), |
@@ -99,7 +97,7 @@ impl<ROOT: TreeRoot> SyntaxNode<ROOT> { | |||
99 | } | 97 | } |
100 | } | 98 | } |
101 | 99 | ||
102 | impl<ROOT: TreeRoot> fmt::Debug for SyntaxNode<ROOT> { | 100 | impl<R: TreeRoot> fmt::Debug for SyntaxNode<R> { |
103 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | 101 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
104 | write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; | 102 | write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; |
105 | if has_short_text(self.kind()) { | 103 | if has_short_text(self.kind()) { |