diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-22 12:38:59 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-22 12:38:59 +0000 |
commit | 708a7d24c5bfa3fd87d9b3b762d71c2a9916c6e1 (patch) | |
tree | 2241ded0c8a4a0c0b5abc719db2b81262bda6eff /crates/ra_ide_api_light/src/line_index.rs | |
parent | 6bc09e2ff82c588d3923aed3fe3c869e8aa70dc1 (diff) | |
parent | fda70badf8957eba6650cc12d2f485d5e213bb82 (diff) |
Merge #1017
1017: line_index and line_index_utils moved to ra_ide_api r=matklad a=pasa
line_index and line_index_utils for issue #1009
Co-authored-by: Sergey Parilin <[email protected]>
Diffstat (limited to 'crates/ra_ide_api_light/src/line_index.rs')
-rw-r--r-- | crates/ra_ide_api_light/src/line_index.rs | 280 |
1 files changed, 0 insertions, 280 deletions
diff --git a/crates/ra_ide_api_light/src/line_index.rs b/crates/ra_ide_api_light/src/line_index.rs deleted file mode 100644 index bf004c33a..000000000 --- a/crates/ra_ide_api_light/src/line_index.rs +++ /dev/null | |||
@@ -1,280 +0,0 @@ | |||
1 | use crate::TextUnit; | ||
2 | use rustc_hash::FxHashMap; | ||
3 | use superslice::Ext; | ||
4 | |||
5 | #[derive(Clone, Debug, PartialEq, Eq)] | ||
6 | pub struct LineIndex { | ||
7 | pub(crate) newlines: Vec<TextUnit>, | ||
8 | pub(crate) utf16_lines: FxHashMap<u32, Vec<Utf16Char>>, | ||
9 | } | ||
10 | |||
11 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
12 | pub struct LineCol { | ||
13 | pub line: u32, | ||
14 | pub col_utf16: u32, | ||
15 | } | ||
16 | |||
17 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] | ||
18 | pub(crate) struct Utf16Char { | ||
19 | pub(crate) start: TextUnit, | ||
20 | pub(crate) end: TextUnit, | ||
21 | } | ||
22 | |||
23 | impl Utf16Char { | ||
24 | fn len(&self) -> TextUnit { | ||
25 | self.end - self.start | ||
26 | } | ||
27 | } | ||
28 | |||
29 | impl LineIndex { | ||
30 | pub fn new(text: &str) -> LineIndex { | ||
31 | let mut utf16_lines = FxHashMap::default(); | ||
32 | let mut utf16_chars = Vec::new(); | ||
33 | |||
34 | let mut newlines = vec![0.into()]; | ||
35 | let mut curr_row = 0.into(); | ||
36 | let mut curr_col = 0.into(); | ||
37 | let mut line = 0; | ||
38 | for c in text.chars() { | ||
39 | curr_row += TextUnit::of_char(c); | ||
40 | if c == '\n' { | ||
41 | newlines.push(curr_row); | ||
42 | |||
43 | // Save any utf-16 characters seen in the previous line | ||
44 | if utf16_chars.len() > 0 { | ||
45 | utf16_lines.insert(line, utf16_chars); | ||
46 | utf16_chars = Vec::new(); | ||
47 | } | ||
48 | |||
49 | // Prepare for processing the next line | ||
50 | curr_col = 0.into(); | ||
51 | line += 1; | ||
52 | continue; | ||
53 | } | ||
54 | |||
55 | let char_len = TextUnit::of_char(c); | ||
56 | if char_len.to_usize() > 1 { | ||
57 | utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + char_len }); | ||
58 | } | ||
59 | |||
60 | curr_col += char_len; | ||
61 | } | ||
62 | |||
63 | // Save any utf-16 characters seen in the last line | ||
64 | if utf16_chars.len() > 0 { | ||
65 | utf16_lines.insert(line, utf16_chars); | ||
66 | } | ||
67 | |||
68 | LineIndex { newlines, utf16_lines } | ||
69 | } | ||
70 | |||
71 | pub fn line_col(&self, offset: TextUnit) -> LineCol { | ||
72 | let line = self.newlines.upper_bound(&offset) - 1; | ||
73 | let line_start_offset = self.newlines[line]; | ||
74 | let col = offset - line_start_offset; | ||
75 | |||
76 | LineCol { line: line as u32, col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32 } | ||
77 | } | ||
78 | |||
79 | pub fn offset(&self, line_col: LineCol) -> TextUnit { | ||
80 | //TODO: return Result | ||
81 | let col = self.utf16_to_utf8_col(line_col.line, line_col.col_utf16); | ||
82 | self.newlines[line_col.line as usize] + col | ||
83 | } | ||
84 | |||
85 | fn utf8_to_utf16_col(&self, line: u32, mut col: TextUnit) -> usize { | ||
86 | if let Some(utf16_chars) = self.utf16_lines.get(&line) { | ||
87 | let mut correction = TextUnit::from_usize(0); | ||
88 | for c in utf16_chars { | ||
89 | if col >= c.end { | ||
90 | correction += c.len() - TextUnit::from_usize(1); | ||
91 | } else { | ||
92 | // From here on, all utf16 characters come *after* the character we are mapping, | ||
93 | // so we don't need to take them into account | ||
94 | break; | ||
95 | } | ||
96 | } | ||
97 | |||
98 | col -= correction; | ||
99 | } | ||
100 | |||
101 | col.to_usize() | ||
102 | } | ||
103 | |||
104 | fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextUnit { | ||
105 | let mut col: TextUnit = col.into(); | ||
106 | if let Some(utf16_chars) = self.utf16_lines.get(&line) { | ||
107 | for c in utf16_chars { | ||
108 | if col >= c.start { | ||
109 | col += c.len() - TextUnit::from_usize(1); | ||
110 | } else { | ||
111 | // From here on, all utf16 characters come *after* the character we are mapping, | ||
112 | // so we don't need to take them into account | ||
113 | break; | ||
114 | } | ||
115 | } | ||
116 | } | ||
117 | |||
118 | col | ||
119 | } | ||
120 | } | ||
121 | |||
122 | #[cfg(test)] | ||
123 | /// Simple reference implementation to use in proptests | ||
124 | pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { | ||
125 | let mut res = LineCol { line: 0, col_utf16: 0 }; | ||
126 | for (i, c) in text.char_indices() { | ||
127 | if i + c.len_utf8() > offset.to_usize() { | ||
128 | // if it's an invalid offset, inside a multibyte char | ||
129 | // return as if it was at the start of the char | ||
130 | break; | ||
131 | } | ||
132 | if c == '\n' { | ||
133 | res.line += 1; | ||
134 | res.col_utf16 = 0; | ||
135 | } else { | ||
136 | res.col_utf16 += 1; | ||
137 | } | ||
138 | } | ||
139 | res | ||
140 | } | ||
141 | |||
142 | #[cfg(test)] | ||
143 | mod test_line_index { | ||
144 | use super::*; | ||
145 | use proptest::{prelude::*, proptest}; | ||
146 | use ra_text_edit::test_utils::{arb_text, arb_offset}; | ||
147 | |||
148 | #[test] | ||
149 | fn test_line_index() { | ||
150 | let text = "hello\nworld"; | ||
151 | let index = LineIndex::new(text); | ||
152 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 }); | ||
153 | assert_eq!(index.line_col(1.into()), LineCol { line: 0, col_utf16: 1 }); | ||
154 | assert_eq!(index.line_col(5.into()), LineCol { line: 0, col_utf16: 5 }); | ||
155 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 0 }); | ||
156 | assert_eq!(index.line_col(7.into()), LineCol { line: 1, col_utf16: 1 }); | ||
157 | assert_eq!(index.line_col(8.into()), LineCol { line: 1, col_utf16: 2 }); | ||
158 | assert_eq!(index.line_col(10.into()), LineCol { line: 1, col_utf16: 4 }); | ||
159 | assert_eq!(index.line_col(11.into()), LineCol { line: 1, col_utf16: 5 }); | ||
160 | assert_eq!(index.line_col(12.into()), LineCol { line: 1, col_utf16: 6 }); | ||
161 | |||
162 | let text = "\nhello\nworld"; | ||
163 | let index = LineIndex::new(text); | ||
164 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 }); | ||
165 | assert_eq!(index.line_col(1.into()), LineCol { line: 1, col_utf16: 0 }); | ||
166 | assert_eq!(index.line_col(2.into()), LineCol { line: 1, col_utf16: 1 }); | ||
167 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 5 }); | ||
168 | assert_eq!(index.line_col(7.into()), LineCol { line: 2, col_utf16: 0 }); | ||
169 | } | ||
170 | |||
171 | fn arb_text_with_offset() -> BoxedStrategy<(TextUnit, String)> { | ||
172 | arb_text().prop_flat_map(|text| (arb_offset(&text), Just(text))).boxed() | ||
173 | } | ||
174 | |||
175 | fn to_line_col(text: &str, offset: TextUnit) -> LineCol { | ||
176 | let mut res = LineCol { line: 0, col_utf16: 0 }; | ||
177 | for (i, c) in text.char_indices() { | ||
178 | if i + c.len_utf8() > offset.to_usize() { | ||
179 | // if it's an invalid offset, inside a multibyte char | ||
180 | // return as if it was at the start of the char | ||
181 | break; | ||
182 | } | ||
183 | if c == '\n' { | ||
184 | res.line += 1; | ||
185 | res.col_utf16 = 0; | ||
186 | } else { | ||
187 | res.col_utf16 += 1; | ||
188 | } | ||
189 | } | ||
190 | res | ||
191 | } | ||
192 | |||
193 | proptest! { | ||
194 | #[test] | ||
195 | fn test_line_index_proptest((offset, text) in arb_text_with_offset()) { | ||
196 | let expected = to_line_col(&text, offset); | ||
197 | let line_index = LineIndex::new(&text); | ||
198 | let actual = line_index.line_col(offset); | ||
199 | |||
200 | assert_eq!(actual, expected); | ||
201 | } | ||
202 | } | ||
203 | } | ||
204 | |||
205 | #[cfg(test)] | ||
206 | mod test_utf8_utf16_conv { | ||
207 | use super::*; | ||
208 | |||
209 | #[test] | ||
210 | fn test_char_len() { | ||
211 | assert_eq!('メ'.len_utf8(), 3); | ||
212 | assert_eq!('メ'.len_utf16(), 1); | ||
213 | } | ||
214 | |||
215 | #[test] | ||
216 | fn test_empty_index() { | ||
217 | let col_index = LineIndex::new( | ||
218 | " | ||
219 | const C: char = 'x'; | ||
220 | ", | ||
221 | ); | ||
222 | assert_eq!(col_index.utf16_lines.len(), 0); | ||
223 | } | ||
224 | |||
225 | #[test] | ||
226 | fn test_single_char() { | ||
227 | let col_index = LineIndex::new( | ||
228 | " | ||
229 | const C: char = 'メ'; | ||
230 | ", | ||
231 | ); | ||
232 | |||
233 | assert_eq!(col_index.utf16_lines.len(), 1); | ||
234 | assert_eq!(col_index.utf16_lines[&1].len(), 1); | ||
235 | assert_eq!(col_index.utf16_lines[&1][0], Utf16Char { start: 17.into(), end: 20.into() }); | ||
236 | |||
237 | // UTF-8 to UTF-16, no changes | ||
238 | assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15); | ||
239 | |||
240 | // UTF-8 to UTF-16 | ||
241 | assert_eq!(col_index.utf8_to_utf16_col(1, 22.into()), 20); | ||
242 | |||
243 | // UTF-16 to UTF-8, no changes | ||
244 | assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextUnit::from(15)); | ||
245 | |||
246 | // UTF-16 to UTF-8 | ||
247 | assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextUnit::from(21)); | ||
248 | } | ||
249 | |||
250 | #[test] | ||
251 | fn test_string() { | ||
252 | let col_index = LineIndex::new( | ||
253 | " | ||
254 | const C: char = \"メ メ\"; | ||
255 | ", | ||
256 | ); | ||
257 | |||
258 | assert_eq!(col_index.utf16_lines.len(), 1); | ||
259 | assert_eq!(col_index.utf16_lines[&1].len(), 2); | ||
260 | assert_eq!(col_index.utf16_lines[&1][0], Utf16Char { start: 17.into(), end: 20.into() }); | ||
261 | assert_eq!(col_index.utf16_lines[&1][1], Utf16Char { start: 21.into(), end: 24.into() }); | ||
262 | |||
263 | // UTF-8 to UTF-16 | ||
264 | assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15); | ||
265 | |||
266 | assert_eq!(col_index.utf8_to_utf16_col(1, 21.into()), 19); | ||
267 | assert_eq!(col_index.utf8_to_utf16_col(1, 25.into()), 21); | ||
268 | |||
269 | assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15); | ||
270 | |||
271 | // UTF-16 to UTF-8 | ||
272 | assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextUnit::from_usize(15)); | ||
273 | |||
274 | assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextUnit::from_usize(20)); | ||
275 | assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextUnit::from_usize(23)); | ||
276 | |||
277 | assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15)); | ||
278 | } | ||
279 | |||
280 | } | ||