diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-02-16 16:34:22 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-02-16 16:34:22 +0000 |
commit | c9672a0539a59b626619724092077f57c70e6ffe (patch) | |
tree | 77097454dc598a5e0a0b1d98b8c9645b9e213289 /crates/ide_db/src | |
parent | f7b7a09f752caba21f2b21ccb8f74421a599d2c6 (diff) | |
parent | 3f09e3fba62839f26da2f27ce27a2335b1dca7ef (diff) |
Merge #7657
7657: utf8 r=matklad a=matklad
- Prepare for utf-8 offsets
- reduce code duplication in tests
- Make utf8 default, implement utf16 in terms of it
- Make it easy to add additional context for offset conversion
- Implement utf8 offsets
closes #7453
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ide_db/src')
-rw-r--r-- | crates/ide_db/src/line_index.rs | 29 | ||||
-rw-r--r-- | crates/ide_db/src/line_index/tests.rs | 33 |
2 files changed, 41 insertions, 21 deletions
diff --git a/crates/ide_db/src/line_index.rs b/crates/ide_db/src/line_index.rs index 41226305e..8e9d8cca2 100644 --- a/crates/ide_db/src/line_index.rs +++ b/crates/ide_db/src/line_index.rs | |||
@@ -15,11 +15,19 @@ pub struct LineIndex { | |||
15 | } | 15 | } |
16 | 16 | ||
17 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 17 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
18 | pub struct LineCol { | 18 | pub struct LineColUtf16 { |
19 | /// Zero-based | 19 | /// Zero-based |
20 | pub line: u32, | 20 | pub line: u32, |
21 | /// Zero-based | 21 | /// Zero-based |
22 | pub col_utf16: u32, | 22 | pub col: u32, |
23 | } | ||
24 | |||
25 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
26 | pub struct LineCol { | ||
27 | /// Zero-based | ||
28 | pub line: u32, | ||
29 | /// Zero-based utf8 offset | ||
30 | pub col: u32, | ||
23 | } | 31 | } |
24 | 32 | ||
25 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] | 33 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] |
@@ -92,14 +100,21 @@ impl LineIndex { | |||
92 | let line = partition_point(&self.newlines, |&it| it <= offset) - 1; | 100 | let line = partition_point(&self.newlines, |&it| it <= offset) - 1; |
93 | let line_start_offset = self.newlines[line]; | 101 | let line_start_offset = self.newlines[line]; |
94 | let col = offset - line_start_offset; | 102 | let col = offset - line_start_offset; |
95 | 103 | LineCol { line: line as u32, col: col.into() } | |
96 | LineCol { line: line as u32, col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32 } | ||
97 | } | 104 | } |
98 | 105 | ||
99 | pub fn offset(&self, line_col: LineCol) -> TextSize { | 106 | pub fn offset(&self, line_col: LineCol) -> TextSize { |
100 | //FIXME: return Result | 107 | self.newlines[line_col.line as usize] + TextSize::from(line_col.col) |
101 | let col = self.utf16_to_utf8_col(line_col.line, line_col.col_utf16); | 108 | } |
102 | self.newlines[line_col.line as usize] + col | 109 | |
110 | pub fn to_utf16(&self, line_col: LineCol) -> LineColUtf16 { | ||
111 | let col = self.utf8_to_utf16_col(line_col.line, line_col.col.into()); | ||
112 | LineColUtf16 { line: line_col.line, col: col as u32 } | ||
113 | } | ||
114 | |||
115 | pub fn to_utf8(&self, line_col: LineColUtf16) -> LineCol { | ||
116 | let col = self.utf16_to_utf8_col(line_col.line, line_col.col); | ||
117 | LineCol { line: line_col.line, col: col.into() } | ||
103 | } | 118 | } |
104 | 119 | ||
105 | pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ { | 120 | pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ { |
diff --git a/crates/ide_db/src/line_index/tests.rs b/crates/ide_db/src/line_index/tests.rs index 05f7484e8..09f3bca62 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::*; | |||
3 | #[test] | 3 | #[test] |
4 | fn test_line_index() { | 4 | fn test_line_index() { |
5 | let text = "hello\nworld"; | 5 | let text = "hello\nworld"; |
6 | let table = [ | ||
7 | (00, 0, 0), | ||
8 | (01, 0, 1), | ||
9 | (05, 0, 5), | ||
10 | (06, 1, 0), | ||
11 | (07, 1, 1), | ||
12 | (08, 1, 2), | ||
13 | (10, 1, 4), | ||
14 | (11, 1, 5), | ||
15 | (12, 1, 6), | ||
16 | ]; | ||
17 | |||
6 | let index = LineIndex::new(text); | 18 | let index = LineIndex::new(text); |
7 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 }); | 19 | for &(offset, line, col) in &table { |
8 | assert_eq!(index.line_col(1.into()), LineCol { line: 0, col_utf16: 1 }); | 20 | assert_eq!(index.line_col(offset.into()), LineCol { line, col }); |
9 | assert_eq!(index.line_col(5.into()), LineCol { line: 0, col_utf16: 5 }); | 21 | } |
10 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 0 }); | ||
11 | assert_eq!(index.line_col(7.into()), LineCol { line: 1, col_utf16: 1 }); | ||
12 | assert_eq!(index.line_col(8.into()), LineCol { line: 1, col_utf16: 2 }); | ||
13 | assert_eq!(index.line_col(10.into()), LineCol { line: 1, col_utf16: 4 }); | ||
14 | assert_eq!(index.line_col(11.into()), LineCol { line: 1, col_utf16: 5 }); | ||
15 | assert_eq!(index.line_col(12.into()), LineCol { line: 1, col_utf16: 6 }); | ||
16 | 22 | ||
17 | let text = "\nhello\nworld"; | 23 | let text = "\nhello\nworld"; |
24 | let table = [(0, 0, 0), (1, 1, 0), (2, 1, 1), (6, 1, 5), (7, 2, 0)]; | ||
18 | let index = LineIndex::new(text); | 25 | let index = LineIndex::new(text); |
19 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 }); | 26 | for &(offset, line, col) in &table { |
20 | assert_eq!(index.line_col(1.into()), LineCol { line: 1, col_utf16: 0 }); | 27 | assert_eq!(index.line_col(offset.into()), LineCol { line, col }); |
21 | assert_eq!(index.line_col(2.into()), LineCol { line: 1, col_utf16: 1 }); | 28 | } |
22 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 5 }); | ||
23 | assert_eq!(index.line_col(7.into()), LineCol { line: 2, col_utf16: 0 }); | ||
24 | } | 29 | } |
25 | 30 | ||
26 | #[test] | 31 | #[test] |