diff options
author | Aleksey Kladov <[email protected]> | 2019-02-08 11:49:43 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-02-08 11:49:43 +0000 |
commit | 12e3b4c70b5ef23b2fdfc197296d483680e125f9 (patch) | |
tree | 71baa0e0a62f9f6b61450501c5f821f67badf9e4 /crates/ra_ide_api_light/src/line_index.rs | |
parent | 5cb1d41a30d25cbe136402644bf5434dd667f1e5 (diff) |
reformat the world
Diffstat (limited to 'crates/ra_ide_api_light/src/line_index.rs')
-rw-r--r-- | crates/ra_ide_api_light/src/line_index.rs | 165 |
1 files changed, 23 insertions, 142 deletions
diff --git a/crates/ra_ide_api_light/src/line_index.rs b/crates/ra_ide_api_light/src/line_index.rs index 131737743..bf004c33a 100644 --- a/crates/ra_ide_api_light/src/line_index.rs +++ b/crates/ra_ide_api_light/src/line_index.rs | |||
@@ -54,10 +54,7 @@ impl LineIndex { | |||
54 | 54 | ||
55 | let char_len = TextUnit::of_char(c); | 55 | let char_len = TextUnit::of_char(c); |
56 | if char_len.to_usize() > 1 { | 56 | if char_len.to_usize() > 1 { |
57 | utf16_chars.push(Utf16Char { | 57 | utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + char_len }); |
58 | start: curr_col, | ||
59 | end: curr_col + char_len, | ||
60 | }); | ||
61 | } | 58 | } |
62 | 59 | ||
63 | curr_col += char_len; | 60 | curr_col += char_len; |
@@ -68,10 +65,7 @@ impl LineIndex { | |||
68 | utf16_lines.insert(line, utf16_chars); | 65 | utf16_lines.insert(line, utf16_chars); |
69 | } | 66 | } |
70 | 67 | ||
71 | LineIndex { | 68 | LineIndex { newlines, utf16_lines } |
72 | newlines, | ||
73 | utf16_lines, | ||
74 | } | ||
75 | } | 69 | } |
76 | 70 | ||
77 | pub fn line_col(&self, offset: TextUnit) -> LineCol { | 71 | pub fn line_col(&self, offset: TextUnit) -> LineCol { |
@@ -79,10 +73,7 @@ impl LineIndex { | |||
79 | let line_start_offset = self.newlines[line]; | 73 | let line_start_offset = self.newlines[line]; |
80 | let col = offset - line_start_offset; | 74 | let col = offset - line_start_offset; |
81 | 75 | ||
82 | LineCol { | 76 | LineCol { line: line as u32, col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32 } |
83 | line: line as u32, | ||
84 | col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32, | ||
85 | } | ||
86 | } | 77 | } |
87 | 78 | ||
88 | pub fn offset(&self, line_col: LineCol) -> TextUnit { | 79 | pub fn offset(&self, line_col: LineCol) -> TextUnit { |
@@ -131,10 +122,7 @@ impl LineIndex { | |||
131 | #[cfg(test)] | 122 | #[cfg(test)] |
132 | /// Simple reference implementation to use in proptests | 123 | /// Simple reference implementation to use in proptests |
133 | pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { | 124 | pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { |
134 | let mut res = LineCol { | 125 | let mut res = LineCol { line: 0, col_utf16: 0 }; |
135 | line: 0, | ||
136 | col_utf16: 0, | ||
137 | }; | ||
138 | for (i, c) in text.char_indices() { | 126 | for (i, c) in text.char_indices() { |
139 | if i + c.len_utf8() > offset.to_usize() { | 127 | if i + c.len_utf8() > offset.to_usize() { |
140 | // if it's an invalid offset, inside a multibyte char | 128 | // if it's an invalid offset, inside a multibyte char |
@@ -161,120 +149,31 @@ mod test_line_index { | |||
161 | fn test_line_index() { | 149 | fn test_line_index() { |
162 | let text = "hello\nworld"; | 150 | let text = "hello\nworld"; |
163 | let index = LineIndex::new(text); | 151 | let index = LineIndex::new(text); |
164 | assert_eq!( | 152 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 }); |
165 | index.line_col(0.into()), | 153 | assert_eq!(index.line_col(1.into()), LineCol { line: 0, col_utf16: 1 }); |
166 | LineCol { | 154 | assert_eq!(index.line_col(5.into()), LineCol { line: 0, col_utf16: 5 }); |
167 | line: 0, | 155 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 0 }); |
168 | col_utf16: 0 | 156 | assert_eq!(index.line_col(7.into()), LineCol { line: 1, col_utf16: 1 }); |
169 | } | 157 | assert_eq!(index.line_col(8.into()), LineCol { line: 1, col_utf16: 2 }); |
170 | ); | 158 | assert_eq!(index.line_col(10.into()), LineCol { line: 1, col_utf16: 4 }); |
171 | assert_eq!( | 159 | assert_eq!(index.line_col(11.into()), LineCol { line: 1, col_utf16: 5 }); |
172 | index.line_col(1.into()), | 160 | assert_eq!(index.line_col(12.into()), LineCol { line: 1, col_utf16: 6 }); |
173 | LineCol { | ||
174 | line: 0, | ||
175 | col_utf16: 1 | ||
176 | } | ||
177 | ); | ||
178 | assert_eq!( | ||
179 | index.line_col(5.into()), | ||
180 | LineCol { | ||
181 | line: 0, | ||
182 | col_utf16: 5 | ||
183 | } | ||
184 | ); | ||
185 | assert_eq!( | ||
186 | index.line_col(6.into()), | ||
187 | LineCol { | ||
188 | line: 1, | ||
189 | col_utf16: 0 | ||
190 | } | ||
191 | ); | ||
192 | assert_eq!( | ||
193 | index.line_col(7.into()), | ||
194 | LineCol { | ||
195 | line: 1, | ||
196 | col_utf16: 1 | ||
197 | } | ||
198 | ); | ||
199 | assert_eq!( | ||
200 | index.line_col(8.into()), | ||
201 | LineCol { | ||
202 | line: 1, | ||
203 | col_utf16: 2 | ||
204 | } | ||
205 | ); | ||
206 | assert_eq!( | ||
207 | index.line_col(10.into()), | ||
208 | LineCol { | ||
209 | line: 1, | ||
210 | col_utf16: 4 | ||
211 | } | ||
212 | ); | ||
213 | assert_eq!( | ||
214 | index.line_col(11.into()), | ||
215 | LineCol { | ||
216 | line: 1, | ||
217 | col_utf16: 5 | ||
218 | } | ||
219 | ); | ||
220 | assert_eq!( | ||
221 | index.line_col(12.into()), | ||
222 | LineCol { | ||
223 | line: 1, | ||
224 | col_utf16: 6 | ||
225 | } | ||
226 | ); | ||
227 | 161 | ||
228 | let text = "\nhello\nworld"; | 162 | let text = "\nhello\nworld"; |
229 | let index = LineIndex::new(text); | 163 | let index = LineIndex::new(text); |
230 | assert_eq!( | 164 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 }); |
231 | index.line_col(0.into()), | 165 | assert_eq!(index.line_col(1.into()), LineCol { line: 1, col_utf16: 0 }); |
232 | LineCol { | 166 | assert_eq!(index.line_col(2.into()), LineCol { line: 1, col_utf16: 1 }); |
233 | line: 0, | 167 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 5 }); |
234 | col_utf16: 0 | 168 | assert_eq!(index.line_col(7.into()), LineCol { line: 2, col_utf16: 0 }); |
235 | } | ||
236 | ); | ||
237 | assert_eq!( | ||
238 | index.line_col(1.into()), | ||
239 | LineCol { | ||
240 | line: 1, | ||
241 | col_utf16: 0 | ||
242 | } | ||
243 | ); | ||
244 | assert_eq!( | ||
245 | index.line_col(2.into()), | ||
246 | LineCol { | ||
247 | line: 1, | ||
248 | col_utf16: 1 | ||
249 | } | ||
250 | ); | ||
251 | assert_eq!( | ||
252 | index.line_col(6.into()), | ||
253 | LineCol { | ||
254 | line: 1, | ||
255 | col_utf16: 5 | ||
256 | } | ||
257 | ); | ||
258 | assert_eq!( | ||
259 | index.line_col(7.into()), | ||
260 | LineCol { | ||
261 | line: 2, | ||
262 | col_utf16: 0 | ||
263 | } | ||
264 | ); | ||
265 | } | 169 | } |
266 | 170 | ||
267 | fn arb_text_with_offset() -> BoxedStrategy<(TextUnit, String)> { | 171 | fn arb_text_with_offset() -> BoxedStrategy<(TextUnit, String)> { |
268 | arb_text() | 172 | arb_text().prop_flat_map(|text| (arb_offset(&text), Just(text))).boxed() |
269 | .prop_flat_map(|text| (arb_offset(&text), Just(text))) | ||
270 | .boxed() | ||
271 | } | 173 | } |
272 | 174 | ||
273 | fn to_line_col(text: &str, offset: TextUnit) -> LineCol { | 175 | fn to_line_col(text: &str, offset: TextUnit) -> LineCol { |
274 | let mut res = LineCol { | 176 | let mut res = LineCol { line: 0, col_utf16: 0 }; |
275 | line: 0, | ||
276 | col_utf16: 0, | ||
277 | }; | ||
278 | for (i, c) in text.char_indices() { | 177 | for (i, c) in text.char_indices() { |
279 | if i + c.len_utf8() > offset.to_usize() { | 178 | if i + c.len_utf8() > offset.to_usize() { |
280 | // if it's an invalid offset, inside a multibyte char | 179 | // if it's an invalid offset, inside a multibyte char |
@@ -333,13 +232,7 @@ const C: char = 'メ'; | |||
333 | 232 | ||
334 | assert_eq!(col_index.utf16_lines.len(), 1); | 233 | assert_eq!(col_index.utf16_lines.len(), 1); |
335 | assert_eq!(col_index.utf16_lines[&1].len(), 1); | 234 | assert_eq!(col_index.utf16_lines[&1].len(), 1); |
336 | assert_eq!( | 235 | assert_eq!(col_index.utf16_lines[&1][0], Utf16Char { start: 17.into(), end: 20.into() }); |
337 | col_index.utf16_lines[&1][0], | ||
338 | Utf16Char { | ||
339 | start: 17.into(), | ||
340 | end: 20.into() | ||
341 | } | ||
342 | ); | ||
343 | 236 | ||
344 | // UTF-8 to UTF-16, no changes | 237 | // UTF-8 to UTF-16, no changes |
345 | assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15); | 238 | assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15); |
@@ -364,20 +257,8 @@ const C: char = \"メ メ\"; | |||
364 | 257 | ||
365 | assert_eq!(col_index.utf16_lines.len(), 1); | 258 | assert_eq!(col_index.utf16_lines.len(), 1); |
366 | assert_eq!(col_index.utf16_lines[&1].len(), 2); | 259 | assert_eq!(col_index.utf16_lines[&1].len(), 2); |
367 | assert_eq!( | 260 | assert_eq!(col_index.utf16_lines[&1][0], Utf16Char { start: 17.into(), end: 20.into() }); |
368 | col_index.utf16_lines[&1][0], | 261 | assert_eq!(col_index.utf16_lines[&1][1], Utf16Char { start: 21.into(), end: 24.into() }); |
369 | Utf16Char { | ||
370 | start: 17.into(), | ||
371 | end: 20.into() | ||
372 | } | ||
373 | ); | ||
374 | assert_eq!( | ||
375 | col_index.utf16_lines[&1][1], | ||
376 | Utf16Char { | ||
377 | start: 21.into(), | ||
378 | end: 24.into() | ||
379 | } | ||
380 | ); | ||
381 | 262 | ||
382 | // UTF-8 to UTF-16 | 263 | // UTF-8 to UTF-16 |
383 | assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15); | 264 | assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15); |