aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/lib.rs5
-rw-r--r--crates/ra_syntax/src/yellow.rs12
-rw-r--r--crates/ra_syntax/src/yellow/syntax_text.rs11
3 files changed, 27 insertions, 1 deletions
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index 2a095817a..6181df9d7 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -59,24 +59,29 @@ impl SourceFile {
59 assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE); 59 assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE);
60 TreeArc::cast(root) 60 TreeArc::cast(root)
61 } 61 }
62
62 pub fn parse(text: &str) -> TreeArc<SourceFile> { 63 pub fn parse(text: &str) -> TreeArc<SourceFile> {
63 let tokens = tokenize(&text); 64 let tokens = tokenize(&text);
64 let (green, errors) = 65 let (green, errors) =
65 parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root); 66 parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root);
66 SourceFile::new(green, errors) 67 SourceFile::new(green, errors)
67 } 68 }
69
68 pub fn reparse(&self, edit: &AtomTextEdit) -> TreeArc<SourceFile> { 70 pub fn reparse(&self, edit: &AtomTextEdit) -> TreeArc<SourceFile> {
69 self.incremental_reparse(edit) 71 self.incremental_reparse(edit)
70 .unwrap_or_else(|| self.full_reparse(edit)) 72 .unwrap_or_else(|| self.full_reparse(edit))
71 } 73 }
74
72 pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<TreeArc<SourceFile>> { 75 pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<TreeArc<SourceFile>> {
73 reparsing::incremental_reparse(self.syntax(), edit, self.errors()) 76 reparsing::incremental_reparse(self.syntax(), edit, self.errors())
74 .map(|(green_node, errors)| SourceFile::new(green_node, errors)) 77 .map(|(green_node, errors)| SourceFile::new(green_node, errors))
75 } 78 }
79
76 fn full_reparse(&self, edit: &AtomTextEdit) -> TreeArc<SourceFile> { 80 fn full_reparse(&self, edit: &AtomTextEdit) -> TreeArc<SourceFile> {
77 let text = edit.apply(self.syntax().text().to_string()); 81 let text = edit.apply(self.syntax().text().to_string());
78 SourceFile::parse(&text) 82 SourceFile::parse(&text)
79 } 83 }
84
80 pub fn errors(&self) -> Vec<SyntaxError> { 85 pub fn errors(&self) -> Vec<SyntaxError> {
81 let mut errors = self.syntax.root_data().clone(); 86 let mut errors = self.syntax.root_data().clone();
82 errors.extend(validation::validate(self)); 87 errors.extend(validation::validate(self));
diff --git a/crates/ra_syntax/src/yellow.rs b/crates/ra_syntax/src/yellow.rs
index 93621d08a..03df00fc6 100644
--- a/crates/ra_syntax/src/yellow.rs
+++ b/crates/ra_syntax/src/yellow.rs
@@ -128,40 +128,52 @@ impl SyntaxNode {
128 pub(crate) fn root_data(&self) -> &Vec<SyntaxError> { 128 pub(crate) fn root_data(&self) -> &Vec<SyntaxError> {
129 self.0.root_data() 129 self.0.root_data()
130 } 130 }
131
131 pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { 132 pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode {
132 self.0.replace_self(replacement) 133 self.0.replace_self(replacement)
133 } 134 }
135
134 pub fn to_owned(&self) -> TreeArc<SyntaxNode> { 136 pub fn to_owned(&self) -> TreeArc<SyntaxNode> {
135 let ptr = TreeArc(self.0.to_owned()); 137 let ptr = TreeArc(self.0.to_owned());
136 TreeArc::cast(ptr) 138 TreeArc::cast(ptr)
137 } 139 }
140
138 pub fn kind(&self) -> SyntaxKind { 141 pub fn kind(&self) -> SyntaxKind {
139 self.0.kind() 142 self.0.kind()
140 } 143 }
144
141 pub fn range(&self) -> TextRange { 145 pub fn range(&self) -> TextRange {
142 self.0.range() 146 self.0.range()
143 } 147 }
148
144 pub fn text(&self) -> SyntaxText { 149 pub fn text(&self) -> SyntaxText {
145 SyntaxText::new(self) 150 SyntaxText::new(self)
146 } 151 }
152
147 pub fn is_leaf(&self) -> bool { 153 pub fn is_leaf(&self) -> bool {
148 self.0.is_leaf() 154 self.0.is_leaf()
149 } 155 }
156
150 pub fn parent(&self) -> Option<&SyntaxNode> { 157 pub fn parent(&self) -> Option<&SyntaxNode> {
151 self.0.parent().map(SyntaxNode::from_repr) 158 self.0.parent().map(SyntaxNode::from_repr)
152 } 159 }
160
153 pub fn first_child(&self) -> Option<&SyntaxNode> { 161 pub fn first_child(&self) -> Option<&SyntaxNode> {
154 self.0.first_child().map(SyntaxNode::from_repr) 162 self.0.first_child().map(SyntaxNode::from_repr)
155 } 163 }
164
156 pub fn last_child(&self) -> Option<&SyntaxNode> { 165 pub fn last_child(&self) -> Option<&SyntaxNode> {
157 self.0.last_child().map(SyntaxNode::from_repr) 166 self.0.last_child().map(SyntaxNode::from_repr)
158 } 167 }
168
159 pub fn next_sibling(&self) -> Option<&SyntaxNode> { 169 pub fn next_sibling(&self) -> Option<&SyntaxNode> {
160 self.0.next_sibling().map(SyntaxNode::from_repr) 170 self.0.next_sibling().map(SyntaxNode::from_repr)
161 } 171 }
172
162 pub fn prev_sibling(&self) -> Option<&SyntaxNode> { 173 pub fn prev_sibling(&self) -> Option<&SyntaxNode> {
163 self.0.prev_sibling().map(SyntaxNode::from_repr) 174 self.0.prev_sibling().map(SyntaxNode::from_repr)
164 } 175 }
176
165 pub fn children(&self) -> SyntaxNodeChildren { 177 pub fn children(&self) -> SyntaxNodeChildren {
166 SyntaxNodeChildren(self.0.children()) 178 SyntaxNodeChildren(self.0.children())
167 } 179 }
diff --git a/crates/ra_syntax/src/yellow/syntax_text.rs b/crates/ra_syntax/src/yellow/syntax_text.rs
index 08dbe57a2..378cd1b2e 100644
--- a/crates/ra_syntax/src/yellow/syntax_text.rs
+++ b/crates/ra_syntax/src/yellow/syntax_text.rs
@@ -15,6 +15,7 @@ impl<'a> SyntaxText<'a> {
15 range: node.range(), 15 range: node.range(),
16 } 16 }
17 } 17 }
18
18 pub fn chunks(&self) -> impl Iterator<Item = &'a str> { 19 pub fn chunks(&self) -> impl Iterator<Item = &'a str> {
19 let range = self.range; 20 let range = self.range;
20 self.node.descendants().filter_map(move |node| { 21 self.node.descendants().filter_map(move |node| {
@@ -24,15 +25,19 @@ impl<'a> SyntaxText<'a> {
24 Some(&text[range]) 25 Some(&text[range])
25 }) 26 })
26 } 27 }
28
27 pub fn push_to(&self, buf: &mut String) { 29 pub fn push_to(&self, buf: &mut String) {
28 self.chunks().for_each(|it| buf.push_str(it)); 30 self.chunks().for_each(|it| buf.push_str(it));
29 } 31 }
32
30 pub fn to_string(&self) -> String { 33 pub fn to_string(&self) -> String {
31 self.chunks().collect() 34 self.chunks().collect()
32 } 35 }
36
33 pub fn contains(&self, c: char) -> bool { 37 pub fn contains(&self, c: char) -> bool {
34 self.chunks().any(|it| it.contains(c)) 38 self.chunks().any(|it| it.contains(c))
35 } 39 }
40
36 pub fn find(&self, c: char) -> Option<TextUnit> { 41 pub fn find(&self, c: char) -> Option<TextUnit> {
37 let mut acc: TextUnit = 0.into(); 42 let mut acc: TextUnit = 0.into();
38 for chunk in self.chunks() { 43 for chunk in self.chunks() {
@@ -44,9 +49,11 @@ impl<'a> SyntaxText<'a> {
44 } 49 }
45 None 50 None
46 } 51 }
52
47 pub fn len(&self) -> TextUnit { 53 pub fn len(&self) -> TextUnit {
48 self.range.len() 54 self.range.len()
49 } 55 }
56
50 pub fn slice(&self, range: impl SyntaxTextSlice) -> SyntaxText<'a> { 57 pub fn slice(&self, range: impl SyntaxTextSlice) -> SyntaxText<'a> {
51 let range = range.restrict(self.range).unwrap_or_else(|| { 58 let range = range.restrict(self.range).unwrap_or_else(|| {
52 panic!("invalid slice, range: {:?}, slice: {:?}", self.range, range) 59 panic!("invalid slice, range: {:?}, slice: {:?}", self.range, range)
@@ -56,8 +63,10 @@ impl<'a> SyntaxText<'a> {
56 range, 63 range,
57 } 64 }
58 } 65 }
59 pub fn char_at(&self, offset: TextUnit) -> Option<char> { 66
67 pub fn char_at(&self, offset: impl Into<TextUnit>) -> Option<char> {
60 let mut start: TextUnit = 0.into(); 68 let mut start: TextUnit = 0.into();
69 let offset = offset.into();
61 for chunk in self.chunks() { 70 for chunk in self.chunks() {
62 let end = start + TextUnit::of_str(chunk); 71 let end = start + TextUnit::of_str(chunk);
63 if start <= offset && offset < end { 72 if start <= offset && offset < end {