From dc2afae991892719b97b0e4b40d8483b43b08680 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sat, 22 Dec 2018 20:52:43 +0100 Subject: fix arbitrary offset generation, col translation working --- crates/ra_editor/src/line_index.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'crates/ra_editor/src/line_index.rs') diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index 6dbabd97e..b01760313 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -62,6 +62,12 @@ impl LineIndex { curr_col += char_len; } + + // Save any utf-16 characters seen in the last line + if utf16_chars.len() > 0 { + utf16_lines.insert(line, utf16_chars); + } + LineIndex { newlines, utf16_lines, @@ -122,6 +128,28 @@ impl LineIndex { } } +// for bench and test +pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { + let mut res = LineCol { + line: 0, + col_utf16: 0, + }; + for (i, c) in text.char_indices() { + if i + c.len_utf8() > offset.to_usize() { + // if it's an invalid offset, inside a multibyte char + // return as if it was at the start of the char + break; + } + if c == '\n' { + res.line += 1; + res.col_utf16 = 0; + } else { + res.col_utf16 += 1; + } + } + res +} + #[test] fn test_line_index() { let text = "hello\nworld"; -- cgit v1.2.3