From 2cb4ac9eb4cb02e7d14ec50a9e7d8e9fe49a4ec1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 12 Feb 2021 21:24:10 +0300 Subject: Prepare for utf-8 offsets --- crates/ide_db/src/line_index.rs | 12 ++++++------ crates/ide_db/src/line_index/tests.rs | 28 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 20 deletions(-) (limited to 'crates/ide_db/src') diff --git a/crates/ide_db/src/line_index.rs b/crates/ide_db/src/line_index.rs index 41226305e..490c172e3 100644 --- a/crates/ide_db/src/line_index.rs +++ b/crates/ide_db/src/line_index.rs @@ -15,11 +15,11 @@ pub struct LineIndex { } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub struct LineCol { +pub struct LineColUtf16 { /// Zero-based pub line: u32, /// Zero-based - pub col_utf16: u32, + pub col: u32, } #[derive(Clone, Debug, Hash, PartialEq, Eq)] @@ -88,17 +88,17 @@ impl LineIndex { LineIndex { newlines, utf16_lines } } - pub fn line_col(&self, offset: TextSize) -> LineCol { + pub fn line_col(&self, offset: TextSize) -> LineColUtf16 { let line = partition_point(&self.newlines, |&it| it <= offset) - 1; let line_start_offset = self.newlines[line]; let col = offset - line_start_offset; - LineCol { line: line as u32, col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32 } + LineColUtf16 { line: line as u32, col: self.utf8_to_utf16_col(line as u32, col) as u32 } } - pub fn offset(&self, line_col: LineCol) -> TextSize { + pub fn offset(&self, line_col: LineColUtf16) -> TextSize { //FIXME: return Result - let col = self.utf16_to_utf8_col(line_col.line, line_col.col_utf16); + let col = self.utf16_to_utf8_col(line_col.line, line_col.col); self.newlines[line_col.line as usize] + col } diff --git a/crates/ide_db/src/line_index/tests.rs b/crates/ide_db/src/line_index/tests.rs index 05f7484e8..1a109654e 100644 --- a/crates/ide_db/src/line_index/tests.rs +++ b/crates/ide_db/src/line_index/tests.rs @@ -4,23 +4,23 @@ use super::*; fn test_line_index() { let text = "hello\nworld"; let index = LineIndex::new(text); - assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 }); - assert_eq!(index.line_col(1.into()), LineCol { line: 0, col_utf16: 1 }); - assert_eq!(index.line_col(5.into()), LineCol { line: 0, col_utf16: 5 }); - assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 0 }); - assert_eq!(index.line_col(7.into()), LineCol { line: 1, col_utf16: 1 }); - assert_eq!(index.line_col(8.into()), LineCol { line: 1, col_utf16: 2 }); - assert_eq!(index.line_col(10.into()), LineCol { line: 1, col_utf16: 4 }); - assert_eq!(index.line_col(11.into()), LineCol { line: 1, col_utf16: 5 }); - assert_eq!(index.line_col(12.into()), LineCol { line: 1, col_utf16: 6 }); + assert_eq!(index.line_col(0.into()), LineColUtf16 { line: 0, col: 0 }); + assert_eq!(index.line_col(1.into()), LineColUtf16 { line: 0, col: 1 }); + assert_eq!(index.line_col(5.into()), LineColUtf16 { line: 0, col: 5 }); + assert_eq!(index.line_col(6.into()), LineColUtf16 { line: 1, col: 0 }); + assert_eq!(index.line_col(7.into()), LineColUtf16 { line: 1, col: 1 }); + assert_eq!(index.line_col(8.into()), LineColUtf16 { line: 1, col: 2 }); + assert_eq!(index.line_col(10.into()), LineColUtf16 { line: 1, col: 4 }); + assert_eq!(index.line_col(11.into()), LineColUtf16 { line: 1, col: 5 }); + assert_eq!(index.line_col(12.into()), LineColUtf16 { line: 1, col: 6 }); let text = "\nhello\nworld"; let index = LineIndex::new(text); - assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 }); - assert_eq!(index.line_col(1.into()), LineCol { line: 1, col_utf16: 0 }); - assert_eq!(index.line_col(2.into()), LineCol { line: 1, col_utf16: 1 }); - assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 5 }); - assert_eq!(index.line_col(7.into()), LineCol { line: 2, col_utf16: 0 }); + assert_eq!(index.line_col(0.into()), LineColUtf16 { line: 0, col: 0 }); + assert_eq!(index.line_col(1.into()), LineColUtf16 { line: 1, col: 0 }); + assert_eq!(index.line_col(2.into()), LineColUtf16 { line: 1, col: 1 }); + assert_eq!(index.line_col(6.into()), LineColUtf16 { line: 1, col: 5 }); + assert_eq!(index.line_col(7.into()), LineColUtf16 { line: 2, col: 0 }); } #[test] -- cgit v1.2.3 From 00cc778c8c62e8f68531c1cadcce8b05b7287d84 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 12 Feb 2021 21:31:32 +0300 Subject: reduce code duplication in tests --- crates/ide_db/src/line_index/tests.rs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'crates/ide_db/src') diff --git a/crates/ide_db/src/line_index/tests.rs b/crates/ide_db/src/line_index/tests.rs index 1a109654e..af51d7348 100644 --- a/crates/ide_db/src/line_index/tests.rs +++ b/crates/ide_db/src/line_index/tests.rs @@ -3,24 +3,29 @@ use super::*; #[test] fn test_line_index() { let text = "hello\nworld"; + let table = [ + (00, 0, 0), + (01, 0, 1), + (05, 0, 5), + (06, 1, 0), + (07, 1, 1), + (08, 1, 2), + (10, 1, 4), + (11, 1, 5), + (12, 1, 6), + ]; + let index = LineIndex::new(text); - assert_eq!(index.line_col(0.into()), LineColUtf16 { line: 0, col: 0 }); - assert_eq!(index.line_col(1.into()), LineColUtf16 { line: 0, col: 1 }); - assert_eq!(index.line_col(5.into()), LineColUtf16 { line: 0, col: 5 }); - assert_eq!(index.line_col(6.into()), LineColUtf16 { line: 1, col: 0 }); - assert_eq!(index.line_col(7.into()), LineColUtf16 { line: 1, col: 1 }); - assert_eq!(index.line_col(8.into()), LineColUtf16 { line: 1, col: 2 }); - assert_eq!(index.line_col(10.into()), LineColUtf16 { line: 1, col: 4 }); - assert_eq!(index.line_col(11.into()), LineColUtf16 { line: 1, col: 5 }); - assert_eq!(index.line_col(12.into()), LineColUtf16 { line: 1, col: 6 }); + for &(offset, line, col) in &table { + assert_eq!(index.line_col(offset.into()), LineColUtf16 { line, col }); + } let text = "\nhello\nworld"; + let table = [(0, 0, 0), (1, 1, 0), (2, 1, 1), (6, 1, 5), (7, 2, 0)]; let index = LineIndex::new(text); - assert_eq!(index.line_col(0.into()), LineColUtf16 { line: 0, col: 0 }); - assert_eq!(index.line_col(1.into()), LineColUtf16 { line: 1, col: 0 }); - assert_eq!(index.line_col(2.into()), LineColUtf16 { line: 1, col: 1 }); - assert_eq!(index.line_col(6.into()), LineColUtf16 { line: 1, col: 5 }); - assert_eq!(index.line_col(7.into()), LineColUtf16 { line: 2, col: 0 }); + for &(offset, line, col) in &table { + assert_eq!(index.line_col(offset.into()), LineColUtf16 { line, col }); + } } #[test] -- cgit v1.2.3 From 95209aa3f8e4b149da6adb374611ece76c2b82ca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 12 Feb 2021 22:09:53 +0300 Subject: Make utf8 default, implement utf16 in terms of it --- crates/ide_db/src/line_index.rs | 25 ++++++++++++++++++++----- crates/ide_db/src/line_index/tests.rs | 4 ++-- 2 files changed, 22 insertions(+), 7 deletions(-) (limited to 'crates/ide_db/src') diff --git a/crates/ide_db/src/line_index.rs b/crates/ide_db/src/line_index.rs index 490c172e3..8e9d8cca2 100644 --- a/crates/ide_db/src/line_index.rs +++ b/crates/ide_db/src/line_index.rs @@ -22,6 +22,14 @@ pub struct LineColUtf16 { pub col: u32, } +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub struct LineCol { + /// Zero-based + pub line: u32, + /// Zero-based utf8 offset + pub col: u32, +} + #[derive(Clone, Debug, Hash, PartialEq, Eq)] pub(crate) struct Utf16Char { /// Start offset of a character inside a line, zero-based @@ -88,18 +96,25 @@ impl LineIndex { LineIndex { newlines, utf16_lines } } - pub fn line_col(&self, offset: TextSize) -> LineColUtf16 { + pub fn line_col(&self, offset: TextSize) -> LineCol { let line = partition_point(&self.newlines, |&it| it <= offset) - 1; let line_start_offset = self.newlines[line]; let col = offset - line_start_offset; + LineCol { line: line as u32, col: col.into() } + } + + pub fn offset(&self, line_col: LineCol) -> TextSize { + self.newlines[line_col.line as usize] + TextSize::from(line_col.col) + } - LineColUtf16 { line: line as u32, col: self.utf8_to_utf16_col(line as u32, col) as u32 } + pub fn to_utf16(&self, line_col: LineCol) -> LineColUtf16 { + let col = self.utf8_to_utf16_col(line_col.line, line_col.col.into()); + LineColUtf16 { line: line_col.line, col: col as u32 } } - pub fn offset(&self, line_col: LineColUtf16) -> TextSize { - //FIXME: return Result + pub fn to_utf8(&self, line_col: LineColUtf16) -> LineCol { let col = self.utf16_to_utf8_col(line_col.line, line_col.col); - self.newlines[line_col.line as usize] + col + LineCol { line: line_col.line, col: col.into() } } pub fn lines(&self, range: TextRange) -> impl Iterator + '_ { diff --git a/crates/ide_db/src/line_index/tests.rs b/crates/ide_db/src/line_index/tests.rs index af51d7348..09f3bca62 100644 --- a/crates/ide_db/src/line_index/tests.rs +++ b/crates/ide_db/src/line_index/tests.rs @@ -17,14 +17,14 @@ fn test_line_index() { let index = LineIndex::new(text); for &(offset, line, col) in &table { - assert_eq!(index.line_col(offset.into()), LineColUtf16 { line, col }); + assert_eq!(index.line_col(offset.into()), LineCol { line, col }); } let text = "\nhello\nworld"; let table = [(0, 0, 0), (1, 1, 0), (2, 1, 1), (6, 1, 5), (7, 2, 0)]; let index = LineIndex::new(text); for &(offset, line, col) in &table { - assert_eq!(index.line_col(offset.into()), LineColUtf16 { line, col }); + assert_eq!(index.line_col(offset.into()), LineCol { line, col }); } } -- cgit v1.2.3