diff options
Diffstat (limited to 'crates/ra_syntax/src/yellow')
-rw-r--r-- | crates/ra_syntax/src/yellow/builder.rs | 12 | ||||
-rw-r--r-- | crates/ra_syntax/src/yellow/mod.rs | 23 | ||||
-rw-r--r-- | crates/ra_syntax/src/yellow/syntax_text.rs | 36 |
3 files changed, 33 insertions, 38 deletions
diff --git a/crates/ra_syntax/src/yellow/builder.rs b/crates/ra_syntax/src/yellow/builder.rs index 67a1a382b..d64053409 100644 --- a/crates/ra_syntax/src/yellow/builder.rs +++ b/crates/ra_syntax/src/yellow/builder.rs | |||
@@ -1,10 +1,9 @@ | |||
1 | use rowan::GreenNodeBuilder; | ||
2 | use crate::{ | 1 | use crate::{ |
3 | TextUnit, SmolStr, | ||
4 | parser_impl::Sink, | 2 | parser_impl::Sink, |
5 | yellow::{GreenNode, SyntaxError, RaTypes}, | 3 | yellow::{GreenNode, RaTypes, SyntaxError}, |
6 | SyntaxKind, | 4 | SmolStr, SyntaxKind, TextUnit, |
7 | }; | 5 | }; |
6 | use rowan::GreenNodeBuilder; | ||
8 | 7 | ||
9 | pub(crate) struct GreenBuilder { | 8 | pub(crate) struct GreenBuilder { |
10 | errors: Vec<SyntaxError>, | 9 | errors: Vec<SyntaxError>, |
@@ -36,7 +35,10 @@ impl Sink for GreenBuilder { | |||
36 | } | 35 | } |
37 | 36 | ||
38 | fn error(&mut self, message: String, offset: TextUnit) { | 37 | fn error(&mut self, message: String, offset: TextUnit) { |
39 | let error = SyntaxError { msg: message, offset }; | 38 | let error = SyntaxError { |
39 | msg: message, | ||
40 | offset, | ||
41 | }; | ||
40 | self.errors.push(error) | 42 | self.errors.push(error) |
41 | } | 43 | } |
42 | 44 | ||
diff --git a/crates/ra_syntax/src/yellow/mod.rs b/crates/ra_syntax/src/yellow/mod.rs index ab9bca0f0..b5c9da813 100644 --- a/crates/ra_syntax/src/yellow/mod.rs +++ b/crates/ra_syntax/src/yellow/mod.rs | |||
@@ -1,16 +1,16 @@ | |||
1 | mod builder; | 1 | mod builder; |
2 | mod syntax_text; | 2 | mod syntax_text; |
3 | 3 | ||
4 | use self::syntax_text::SyntaxText; | ||
5 | use crate::{SmolStr, SyntaxKind, TextRange, TextUnit}; | ||
6 | use rowan::Types; | ||
4 | use std::{ | 7 | use std::{ |
5 | fmt, | 8 | fmt, |
6 | hash::{Hash, Hasher}, | 9 | hash::{Hash, Hasher}, |
7 | }; | 10 | }; |
8 | use rowan::Types; | ||
9 | use crate::{SyntaxKind, TextUnit, TextRange, SmolStr}; | ||
10 | use self::syntax_text::SyntaxText; | ||
11 | 11 | ||
12 | pub use rowan::{TreeRoot}; | ||
13 | pub(crate) use self::builder::GreenBuilder; | 12 | pub(crate) use self::builder::GreenBuilder; |
13 | pub use rowan::TreeRoot; | ||
14 | 14 | ||
15 | #[derive(Debug, Clone, Copy)] | 15 | #[derive(Debug, Clone, Copy)] |
16 | pub enum RaTypes {} | 16 | pub enum RaTypes {} |
@@ -31,9 +31,7 @@ pub struct SyntaxError { | |||
31 | } | 31 | } |
32 | 32 | ||
33 | #[derive(Clone, Copy)] | 33 | #[derive(Clone, Copy)] |
34 | pub struct SyntaxNode<R: TreeRoot<RaTypes> = OwnedRoot>( | 34 | pub struct SyntaxNode<R: TreeRoot<RaTypes> = OwnedRoot>(::rowan::SyntaxNode<RaTypes, R>); |
35 | ::rowan::SyntaxNode<RaTypes, R>, | ||
36 | ); | ||
37 | pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>; | 35 | pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>; |
38 | 36 | ||
39 | impl<R1, R2> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> | 37 | impl<R1, R2> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> |
@@ -69,16 +67,16 @@ impl<'a> SyntaxNodeRef<'a> { | |||
69 | pub fn leaf_text(self) -> Option<&'a SmolStr> { | 67 | pub fn leaf_text(self) -> Option<&'a SmolStr> { |
70 | self.0.leaf_text() | 68 | self.0.leaf_text() |
71 | } | 69 | } |
72 | pub fn ancestors(self) -> impl Iterator<Item=SyntaxNodeRef<'a>> { | 70 | pub fn ancestors(self) -> impl Iterator<Item = SyntaxNodeRef<'a>> { |
73 | crate::algo::generate(Some(self), |&node| node.parent()) | 71 | crate::algo::generate(Some(self), |&node| node.parent()) |
74 | } | 72 | } |
75 | pub fn descendants(self) -> impl Iterator<Item=SyntaxNodeRef<'a>> { | 73 | pub fn descendants(self) -> impl Iterator<Item = SyntaxNodeRef<'a>> { |
76 | crate::algo::walk::walk(self).filter_map(|event| match event { | 74 | crate::algo::walk::walk(self).filter_map(|event| match event { |
77 | crate::algo::walk::WalkEvent::Enter(node) => Some(node), | 75 | crate::algo::walk::WalkEvent::Enter(node) => Some(node), |
78 | crate::algo::walk::WalkEvent::Exit(_) => None, | 76 | crate::algo::walk::WalkEvent::Exit(_) => None, |
79 | }) | 77 | }) |
80 | } | 78 | } |
81 | pub fn siblings(self, direction: Direction) -> impl Iterator<Item=SyntaxNodeRef<'a>> { | 79 | pub fn siblings(self, direction: Direction) -> impl Iterator<Item = SyntaxNodeRef<'a>> { |
82 | crate::algo::generate(Some(self), move |&node| match direction { | 80 | crate::algo::generate(Some(self), move |&node| match direction { |
83 | Direction::Next => node.next_sibling(), | 81 | Direction::Next => node.next_sibling(), |
84 | Direction::Prev => node.prev_sibling(), | 82 | Direction::Prev => node.prev_sibling(), |
@@ -142,9 +140,7 @@ impl<R: TreeRoot<RaTypes>> fmt::Debug for SyntaxNode<R> { | |||
142 | } | 140 | } |
143 | 141 | ||
144 | #[derive(Debug)] | 142 | #[derive(Debug)] |
145 | pub struct SyntaxNodeChildren<R: TreeRoot<RaTypes>>( | 143 | pub struct SyntaxNodeChildren<R: TreeRoot<RaTypes>>(::rowan::SyntaxNodeChildren<RaTypes, R>); |
146 | ::rowan::SyntaxNodeChildren<RaTypes, R> | ||
147 | ); | ||
148 | 144 | ||
149 | impl<R: TreeRoot<RaTypes>> Iterator for SyntaxNodeChildren<R> { | 145 | impl<R: TreeRoot<RaTypes>> Iterator for SyntaxNodeChildren<R> { |
150 | type Item = SyntaxNode<R>; | 146 | type Item = SyntaxNode<R>; |
@@ -154,7 +150,6 @@ impl<R: TreeRoot<RaTypes>> Iterator for SyntaxNodeChildren<R> { | |||
154 | } | 150 | } |
155 | } | 151 | } |
156 | 152 | ||
157 | |||
158 | fn has_short_text(kind: SyntaxKind) -> bool { | 153 | fn has_short_text(kind: SyntaxKind) -> bool { |
159 | use crate::SyntaxKind::*; | 154 | use crate::SyntaxKind::*; |
160 | match kind { | 155 | match kind { |
diff --git a/crates/ra_syntax/src/yellow/syntax_text.rs b/crates/ra_syntax/src/yellow/syntax_text.rs index ae33b993d..5395ca90b 100644 --- a/crates/ra_syntax/src/yellow/syntax_text.rs +++ b/crates/ra_syntax/src/yellow/syntax_text.rs | |||
@@ -1,10 +1,8 @@ | |||
1 | use std::{ | 1 | use std::{fmt, ops}; |
2 | fmt, ops, | ||
3 | }; | ||
4 | 2 | ||
5 | use crate::{ | 3 | use crate::{ |
4 | text_utils::{contains_offset_nonstrict, intersect}, | ||
6 | SyntaxNodeRef, TextRange, TextUnit, | 5 | SyntaxNodeRef, TextRange, TextUnit, |
7 | text_utils::{intersect, contains_offset_nonstrict}, | ||
8 | }; | 6 | }; |
9 | 7 | ||
10 | #[derive(Clone)] | 8 | #[derive(Clone)] |
@@ -17,19 +15,17 @@ impl<'a> SyntaxText<'a> { | |||
17 | pub(crate) fn new(node: SyntaxNodeRef<'a>) -> SyntaxText<'a> { | 15 | pub(crate) fn new(node: SyntaxNodeRef<'a>) -> SyntaxText<'a> { |
18 | SyntaxText { | 16 | SyntaxText { |
19 | node, | 17 | node, |
20 | range: node.range() | 18 | range: node.range(), |
21 | } | 19 | } |
22 | } | 20 | } |
23 | pub fn chunks(&self) -> impl Iterator<Item=&'a str> { | 21 | pub fn chunks(&self) -> impl Iterator<Item = &'a str> { |
24 | let range = self.range; | 22 | let range = self.range; |
25 | self.node | 23 | self.node.descendants().filter_map(move |node| { |
26 | .descendants() | 24 | let text = node.leaf_text()?; |
27 | .filter_map(move |node| { | 25 | let range = intersect(range, node.range())?; |
28 | let text = node.leaf_text()?; | 26 | let range = range - node.range().start(); |
29 | let range = intersect(range, node.range())?; | 27 | Some(&text[range]) |
30 | let range = range - node.range().start(); | 28 | }) |
31 | Some(&text[range]) | ||
32 | }) | ||
33 | } | 29 | } |
34 | pub fn push_to(&self, buf: &mut String) { | 30 | pub fn push_to(&self, buf: &mut String) { |
35 | self.chunks().for_each(|it| buf.push_str(it)); | 31 | self.chunks().for_each(|it| buf.push_str(it)); |
@@ -55,11 +51,13 @@ impl<'a> SyntaxText<'a> { | |||
55 | self.range.len() | 51 | self.range.len() |
56 | } | 52 | } |
57 | pub fn slice(&self, range: impl SyntaxTextSlice) -> SyntaxText<'a> { | 53 | pub fn slice(&self, range: impl SyntaxTextSlice) -> SyntaxText<'a> { |
58 | let range = range.restrict(self.range) | 54 | let range = range.restrict(self.range).unwrap_or_else(|| { |
59 | .unwrap_or_else(|| { | 55 | panic!("invalid slice, range: {:?}, slice: {:?}", self.range, range) |
60 | panic!("invalid slice, range: {:?}, slice: {:?}", self.range, range) | 56 | }); |
61 | }); | 57 | SyntaxText { |
62 | SyntaxText { node: self.node, range } | 58 | node: self.node, |
59 | range, | ||
60 | } | ||
63 | } | 61 | } |
64 | pub fn char_at(&self, offset: TextUnit) -> Option<char> { | 62 | pub fn char_at(&self, offset: TextUnit) -> Option<char> { |
65 | let mut start: TextUnit = 0.into(); | 63 | let mut start: TextUnit = 0.into(); |