aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src/line_index.rs
diff options
context:
space:
mode:
authorBernardo <[email protected]>2018-12-22 19:52:43 +0000
committerBernardo <[email protected]>2018-12-25 19:03:14 +0000
commitdc2afae991892719b97b0e4b40d8483b43b08680 (patch)
tree67509ccaf4599caa102a494521e9597e1453f30f /crates/ra_editor/src/line_index.rs
parent5c8525ce4ae8bb969f2ac263bf14adad1c835c03 (diff)
fix arbitrary offset generation, col translation working
Diffstat (limited to 'crates/ra_editor/src/line_index.rs')
-rw-r--r--crates/ra_editor/src/line_index.rs28
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
132pub 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]
126fn test_line_index() { 154fn test_line_index() {
127 let text = "hello\nworld"; 155 let text = "hello\nworld";