aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/yellow/syntax_text.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/yellow/syntax_text.rs')
-rw-r--r--crates/ra_syntax/src/yellow/syntax_text.rs36
1 files changed, 17 insertions, 19 deletions
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();