From 1d794e859028a71d182daf2fa5826aeeeab2876b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Tue, 5 May 2020 20:29:04 +0300 Subject: Fix column conversion for supplementary plane characters --- crates/ra_ide_db/src/line_index.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide_db/src') 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 { } impl Utf16Char { + /// Returns the length in 8-bit UTF-8 code units. fn len(&self) -> TextSize { self.end - self.start } + + /// Returns the length in 16-bit UTF-16 code units. + fn len_utf16(&self) -> usize { + if self.len() == TextSize::from(4) { + 2 + } else { + 1 + } + } } impl LineIndex { @@ -110,7 +120,7 @@ impl LineIndex { if let Some(utf16_chars) = self.utf16_lines.get(&line) { for c in utf16_chars { if c.end <= col { - res -= usize::from(c.len()) - 1; + res -= usize::from(c.len()) - c.len_utf16(); } else { // From here on, all utf16 characters come *after* the character we are mapping, // so we don't need to take them into account @@ -125,7 +135,7 @@ impl LineIndex { if let Some(utf16_chars) = self.utf16_lines.get(&line) { for c in utf16_chars { if col > u32::from(c.start) { - col += u32::from(c.len()) - 1; + col += u32::from(c.len()) - c.len_utf16() as u32; } else { // From here on, all utf16 characters come *after* the character we are mapping, // so we don't need to take them into account @@ -204,6 +214,9 @@ const C: char = 'メ'; // UTF-16 to UTF-8 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(21)); + + let col_index = LineIndex::new("a𐐏b"); + assert_eq!(col_index.utf16_to_utf8_col(0, 3), TextSize::from(5)); } #[test] -- cgit v1.2.3 From 4a6fa8f0dfcebbb4ea80394e5e4ca21f076f58f2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 5 May 2020 23:15:49 +0200 Subject: Rename AtomTextEdit -> Indel --- crates/ra_ide_db/src/line_index_utils.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide_db/src') 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 @@ use std::convert::TryInto; use ra_syntax::{TextRange, TextSize}; -use ra_text_edit::{AtomTextEdit, TextEdit}; +use ra_text_edit::{Indel, TextEdit}; use crate::line_index::{LineCol, LineIndex, Utf16Char}; @@ -182,14 +182,14 @@ struct TranslatedEdit<'a> { } struct Edits<'a> { - edits: &'a [AtomTextEdit], + edits: &'a [Indel], current: Option>, acc_diff: i64, } impl<'a> Edits<'a> { fn from_text_edit(text_edit: &'a TextEdit) -> Edits<'a> { - let mut x = Edits { edits: text_edit.as_atoms(), current: None, acc_diff: 0 }; + let mut x = Edits { edits: text_edit.as_indels(), current: None, acc_diff: 0 }; x.advance_edit(); x } -- cgit v1.2.3