diff options
Diffstat (limited to 'crates/ra_editor/src/line_index.rs')
-rw-r--r-- | crates/ra_editor/src/line_index.rs | 28 |
1 files changed, 28 insertions, 0 deletions
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 { | |||
62 | 62 | ||
63 | curr_col += char_len; | 63 | curr_col += char_len; |
64 | } | 64 | } |
65 | |||
66 | // Save any utf-16 characters seen in the last line | ||
67 | if utf16_chars.len() > 0 { | ||
68 | utf16_lines.insert(line, utf16_chars); | ||
69 | } | ||
70 | |||
65 | LineIndex { | 71 | LineIndex { |
66 | newlines, | 72 | newlines, |
67 | utf16_lines, | 73 | utf16_lines, |
@@ -122,6 +128,28 @@ impl LineIndex { | |||
122 | } | 128 | } |
123 | } | 129 | } |
124 | 130 | ||
131 | // for bench and test | ||
132 | pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { | ||
133 | let mut res = LineCol { | ||
134 | line: 0, | ||
135 | col_utf16: 0, | ||
136 | }; | ||
137 | for (i, c) in text.char_indices() { | ||
138 | if i + c.len_utf8() > offset.to_usize() { | ||
139 | // if it's an invalid offset, inside a multibyte char | ||
140 | // return as if it was at the start of the char | ||
141 | break; | ||
142 | } | ||
143 | if c == '\n' { | ||
144 | res.line += 1; | ||
145 | res.col_utf16 = 0; | ||
146 | } else { | ||
147 | res.col_utf16 += 1; | ||
148 | } | ||
149 | } | ||
150 | res | ||
151 | } | ||
152 | |||
125 | #[test] | 153 | #[test] |
126 | fn test_line_index() { | 154 | fn test_line_index() { |
127 | let text = "hello\nworld"; | 155 | let text = "hello\nworld"; |