aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/yellow
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/yellow')
-rw-r--r--crates/ra_syntax/src/yellow/builder.rs12
-rw-r--r--crates/ra_syntax/src/yellow/mod.rs23
-rw-r--r--crates/ra_syntax/src/yellow/syntax_text.rs36
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 @@
1use rowan::GreenNodeBuilder;
2use crate::{ 1use 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};
6use rowan::GreenNodeBuilder;
8 7
9pub(crate) struct GreenBuilder { 8pub(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 @@
1mod builder; 1mod builder;
2mod syntax_text; 2mod syntax_text;
3 3
4use self::syntax_text::SyntaxText;
5use crate::{SmolStr, SyntaxKind, TextRange, TextUnit};
6use rowan::Types;
4use std::{ 7use std::{
5 fmt, 8 fmt,
6 hash::{Hash, Hasher}, 9 hash::{Hash, Hasher},
7}; 10};
8use rowan::Types;
9use crate::{SyntaxKind, TextUnit, TextRange, SmolStr};
10use self::syntax_text::SyntaxText;
11 11
12pub use rowan::{TreeRoot};
13pub(crate) use self::builder::GreenBuilder; 12pub(crate) use self::builder::GreenBuilder;
13pub use rowan::TreeRoot;
14 14
15#[derive(Debug, Clone, Copy)] 15#[derive(Debug, Clone, Copy)]
16pub enum RaTypes {} 16pub enum RaTypes {}
@@ -31,9 +31,7 @@ pub struct SyntaxError {
31} 31}
32 32
33#[derive(Clone, Copy)] 33#[derive(Clone, Copy)]
34pub struct SyntaxNode<R: TreeRoot<RaTypes> = OwnedRoot>( 34pub struct SyntaxNode<R: TreeRoot<RaTypes> = OwnedRoot>(::rowan::SyntaxNode<RaTypes, R>);
35 ::rowan::SyntaxNode<RaTypes, R>,
36);
37pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>; 35pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>;
38 36
39impl<R1, R2> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> 37impl<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)]
145pub struct SyntaxNodeChildren<R: TreeRoot<RaTypes>>( 143pub struct SyntaxNodeChildren<R: TreeRoot<RaTypes>>(::rowan::SyntaxNodeChildren<RaTypes, R>);
146 ::rowan::SyntaxNodeChildren<RaTypes, R>
147);
148 144
149impl<R: TreeRoot<RaTypes>> Iterator for SyntaxNodeChildren<R> { 145impl<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
158fn has_short_text(kind: SyntaxKind) -> bool { 153fn 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 @@
1use std::{ 1use std::{fmt, ops};
2 fmt, ops,
3};
4 2
5use crate::{ 3use 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();