aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_db
diff options
context:
space:
mode:
authorBenjamin Coenen <[email protected]>2020-05-06 08:57:00 +0100
committerBenjamin Coenen <[email protected]>2020-05-06 08:57:00 +0100
commitc4d128e454448191c4b21d8e151c673e4c42376e (patch)
tree17cf5bbf429642c52708cd0d3c1d8885b63543f0 /crates/ra_ide_db
parent0bf02f5ccac99c91f10ef46bb06ff2ea316c382c (diff)
parent30eb458b4fa8adcecd8cbf731bd1cfa9a7a8b88b (diff)
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into fix_4311
Diffstat (limited to 'crates/ra_ide_db')
-rw-r--r--crates/ra_ide_db/src/line_index.rs17
-rw-r--r--crates/ra_ide_db/src/line_index_utils.rs6
2 files changed, 18 insertions, 5 deletions
diff --git a/crates/ra_ide_db/src/line_index.rs b/crates/ra_ide_db/src/line_index.rs
index 212cb7b5b..c7c744fce 100644
--- a/crates/ra_ide_db/src/line_index.rs
+++ b/crates/ra_ide_db/src/line_index.rs
@@ -31,9 +31,19 @@ pub(crate) struct Utf16Char {
31} 31}
32 32
33impl Utf16Char { 33impl Utf16Char {
34 /// Returns the length in 8-bit UTF-8 code units.
34 fn len(&self) -> TextSize { 35 fn len(&self) -> TextSize {
35 self.end - self.start 36 self.end - self.start
36 } 37 }
38
39 /// Returns the length in 16-bit UTF-16 code units.
40 fn len_utf16(&self) -> usize {
41 if self.len() == TextSize::from(4) {
42 2
43 } else {
44 1
45 }
46 }
37} 47}
38 48
39impl LineIndex { 49impl LineIndex {
@@ -110,7 +120,7 @@ impl LineIndex {
110 if let Some(utf16_chars) = self.utf16_lines.get(&line) { 120 if let Some(utf16_chars) = self.utf16_lines.get(&line) {
111 for c in utf16_chars { 121 for c in utf16_chars {
112 if c.end <= col { 122 if c.end <= col {
113 res -= usize::from(c.len()) - 1; 123 res -= usize::from(c.len()) - c.len_utf16();
114 } else { 124 } else {
115 // From here on, all utf16 characters come *after* the character we are mapping, 125 // From here on, all utf16 characters come *after* the character we are mapping,
116 // so we don't need to take them into account 126 // so we don't need to take them into account
@@ -125,7 +135,7 @@ impl LineIndex {
125 if let Some(utf16_chars) = self.utf16_lines.get(&line) { 135 if let Some(utf16_chars) = self.utf16_lines.get(&line) {
126 for c in utf16_chars { 136 for c in utf16_chars {
127 if col > u32::from(c.start) { 137 if col > u32::from(c.start) {
128 col += u32::from(c.len()) - 1; 138 col += u32::from(c.len()) - c.len_utf16() as u32;
129 } else { 139 } else {
130 // From here on, all utf16 characters come *after* the character we are mapping, 140 // From here on, all utf16 characters come *after* the character we are mapping,
131 // so we don't need to take them into account 141 // so we don't need to take them into account
@@ -204,6 +214,9 @@ const C: char = 'メ';
204 214
205 // UTF-16 to UTF-8 215 // UTF-16 to UTF-8
206 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(21)); 216 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(21));
217
218 let col_index = LineIndex::new("a𐐏b");
219 assert_eq!(col_index.utf16_to_utf8_col(0, 3), TextSize::from(5));
207 } 220 }
208 221
209 #[test] 222 #[test]
diff --git a/crates/ra_ide_db/src/line_index_utils.rs b/crates/ra_ide_db/src/line_index_utils.rs
index 039a12c0d..7fa6fc448 100644
--- a/crates/ra_ide_db/src/line_index_utils.rs
+++ b/crates/ra_ide_db/src/line_index_utils.rs
@@ -10,7 +10,7 @@
10use std::convert::TryInto; 10use std::convert::TryInto;
11 11
12use ra_syntax::{TextRange, TextSize}; 12use ra_syntax::{TextRange, TextSize};
13use ra_text_edit::{AtomTextEdit, TextEdit}; 13use ra_text_edit::{Indel, TextEdit};
14 14
15use crate::line_index::{LineCol, LineIndex, Utf16Char}; 15use crate::line_index::{LineCol, LineIndex, Utf16Char};
16 16
@@ -182,14 +182,14 @@ struct TranslatedEdit<'a> {
182} 182}
183 183
184struct Edits<'a> { 184struct Edits<'a> {
185 edits: &'a [AtomTextEdit], 185 edits: &'a [Indel],
186 current: Option<TranslatedEdit<'a>>, 186 current: Option<TranslatedEdit<'a>>,
187 acc_diff: i64, 187 acc_diff: i64,
188} 188}
189 189
190impl<'a> Edits<'a> { 190impl<'a> Edits<'a> {
191 fn from_text_edit(text_edit: &'a TextEdit) -> Edits<'a> { 191 fn from_text_edit(text_edit: &'a TextEdit) -> Edits<'a> {
192 let mut x = Edits { edits: text_edit.as_atoms(), current: None, acc_diff: 0 }; 192 let mut x = Edits { edits: text_edit.as_indels(), current: None, acc_diff: 0 };
193 x.advance_edit(); 193 x.advance_edit();
194 x 194 x
195 } 195 }