diff options
Diffstat (limited to 'crates/ra_editor/src/line_index.rs')
-rw-r--r-- | crates/ra_editor/src/line_index.rs | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index 5304fbcf6..898fee7e0 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs | |||
@@ -128,8 +128,8 @@ impl LineIndex { | |||
128 | } | 128 | } |
129 | } | 129 | } |
130 | 130 | ||
131 | #[cfg(test)] | ||
131 | /// Simple reference implementation to use in proptests | 132 | /// Simple reference implementation to use in proptests |
132 | /// and benchmarks as baseline | ||
133 | pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { | 133 | pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { |
134 | let mut res = LineCol { | 134 | let mut res = LineCol { |
135 | line: 0, | 135 | line: 0, |
@@ -270,6 +270,27 @@ mod test_line_index { | |||
270 | .boxed() | 270 | .boxed() |
271 | } | 271 | } |
272 | 272 | ||
273 | fn to_line_col(text: &str, offset: TextUnit) -> LineCol { | ||
274 | let mut res = LineCol { | ||
275 | line: 0, | ||
276 | col_utf16: 0, | ||
277 | }; | ||
278 | for (i, c) in text.char_indices() { | ||
279 | if i + c.len_utf8() > offset.to_usize() { | ||
280 | // if it's an invalid offset, inside a multibyte char | ||
281 | // return as if it was at the start of the char | ||
282 | break; | ||
283 | } | ||
284 | if c == '\n' { | ||
285 | res.line += 1; | ||
286 | res.col_utf16 = 0; | ||
287 | } else { | ||
288 | res.col_utf16 += 1; | ||
289 | } | ||
290 | } | ||
291 | res | ||
292 | } | ||
293 | |||
273 | proptest! { | 294 | proptest! { |
274 | #[test] | 295 | #[test] |
275 | fn test_line_index_proptest((offset, text) in arb_text_with_offset()) { | 296 | fn test_line_index_proptest((offset, text) in arb_text_with_offset()) { |