diff options
Diffstat (limited to 'crates/libsyntax2/src/yellow/syntax_text.rs')
-rw-r--r-- | crates/libsyntax2/src/yellow/syntax_text.rs | 122 |
1 files changed, 0 insertions, 122 deletions
diff --git a/crates/libsyntax2/src/yellow/syntax_text.rs b/crates/libsyntax2/src/yellow/syntax_text.rs deleted file mode 100644 index 280bedd78..000000000 --- a/crates/libsyntax2/src/yellow/syntax_text.rs +++ /dev/null | |||
@@ -1,122 +0,0 @@ | |||
1 | use std::{ | ||
2 | fmt, ops, | ||
3 | }; | ||
4 | |||
5 | use { | ||
6 | SyntaxNodeRef, TextRange, TextUnit, | ||
7 | algo::walk::preorder, | ||
8 | text_utils::{intersect, contains_offset_nonstrict}, | ||
9 | }; | ||
10 | |||
11 | #[derive(Clone)] | ||
12 | pub struct SyntaxText<'a> { | ||
13 | node: SyntaxNodeRef<'a>, | ||
14 | range: TextRange, | ||
15 | } | ||
16 | |||
17 | impl<'a> SyntaxText<'a> { | ||
18 | pub(crate) fn new(node: SyntaxNodeRef<'a>) -> SyntaxText<'a> { | ||
19 | SyntaxText { | ||
20 | node, | ||
21 | range: node.range() | ||
22 | } | ||
23 | } | ||
24 | pub fn chunks(&self) -> impl Iterator<Item=&'a str> { | ||
25 | let range = self.range; | ||
26 | preorder(self.node) | ||
27 | .filter_map(move |node| { | ||
28 | let text = node.leaf_text_ref()?; | ||
29 | let range = intersect(range, node.range())?; | ||
30 | let range = range - node.range().start(); | ||
31 | Some(&text[range]) | ||
32 | }) | ||
33 | } | ||
34 | pub fn push_to(&self, buf: &mut String) { | ||
35 | self.chunks().for_each(|it| buf.push_str(it)); | ||
36 | } | ||
37 | pub fn to_string(&self) -> String { | ||
38 | self.chunks().collect() | ||
39 | } | ||
40 | pub fn contains(&self, c: char) -> bool { | ||
41 | self.chunks().any(|it| it.contains(c)) | ||
42 | } | ||
43 | pub fn find(&self, c: char) -> Option<TextUnit> { | ||
44 | let mut acc: TextUnit = 0.into(); | ||
45 | for chunk in self.chunks() { | ||
46 | if let Some(pos) = chunk.find(c) { | ||
47 | let pos: TextUnit = (pos as u32).into(); | ||
48 | return Some(acc + pos); | ||
49 | } | ||
50 | acc += TextUnit::of_str(chunk); | ||
51 | } | ||
52 | None | ||
53 | } | ||
54 | pub fn len(&self) -> TextUnit { | ||
55 | self.range.len() | ||
56 | } | ||
57 | pub fn slice(&self, range: impl SyntaxTextSlice) -> SyntaxText<'a> { | ||
58 | let range = range.restrict(self.range) | ||
59 | .unwrap_or_else(|| { | ||
60 | panic!("invalid slice, range: {:?}, slice: {:?}", self.range, range) | ||
61 | }); | ||
62 | SyntaxText { node: self.node, range } | ||
63 | } | ||
64 | pub fn char_at(&self, offset: TextUnit) -> Option<char> { | ||
65 | let mut start: TextUnit = 0.into(); | ||
66 | for chunk in self.chunks() { | ||
67 | let end = start + TextUnit::of_str(chunk); | ||
68 | if start <= offset && offset < end { | ||
69 | let off: usize = u32::from(offset - start) as usize; | ||
70 | return Some(chunk[off..].chars().next().unwrap()); | ||
71 | } | ||
72 | start = end; | ||
73 | } | ||
74 | None | ||
75 | } | ||
76 | } | ||
77 | |||
78 | impl<'a> fmt::Debug for SyntaxText<'a> { | ||
79 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
80 | fmt::Debug::fmt(&self.to_string(), f) | ||
81 | } | ||
82 | } | ||
83 | |||
84 | impl<'a> fmt::Display for SyntaxText<'a> { | ||
85 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
86 | fmt::Display::fmt(&self.to_string(), f) | ||
87 | } | ||
88 | } | ||
89 | |||
90 | pub trait SyntaxTextSlice: fmt::Debug { | ||
91 | fn restrict(&self, range: TextRange) -> Option<TextRange>; | ||
92 | } | ||
93 | |||
94 | impl SyntaxTextSlice for TextRange { | ||
95 | fn restrict(&self, range: TextRange) -> Option<TextRange> { | ||
96 | intersect(*self, range) | ||
97 | } | ||
98 | } | ||
99 | |||
100 | impl SyntaxTextSlice for ops::RangeTo<TextUnit> { | ||
101 | fn restrict(&self, range: TextRange) -> Option<TextRange> { | ||
102 | if !contains_offset_nonstrict(range, self.end) { | ||
103 | return None; | ||
104 | } | ||
105 | Some(TextRange::from_to(range.start(), self.end)) | ||
106 | } | ||
107 | } | ||
108 | |||
109 | impl SyntaxTextSlice for ops::RangeFrom<TextUnit> { | ||
110 | fn restrict(&self, range: TextRange) -> Option<TextRange> { | ||
111 | if !contains_offset_nonstrict(range, self.start) { | ||
112 | return None; | ||
113 | } | ||
114 | Some(TextRange::from_to(self.start, range.end())) | ||
115 | } | ||
116 | } | ||
117 | |||
118 | impl SyntaxTextSlice for ops::Range<TextUnit> { | ||
119 | fn restrict(&self, range: TextRange) -> Option<TextRange> { | ||
120 | TextRange::from_to(self.start, self.end).restrict(range) | ||
121 | } | ||
122 | } | ||