diff options
Diffstat (limited to 'crates/ide_db')
-rw-r--r-- | crates/ide_db/src/line_index.rs | 25 | ||||
-rw-r--r-- | crates/ide_db/src/line_index/tests.rs | 4 |
2 files changed, 22 insertions, 7 deletions
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 { | |||
22 | pub col: u32, | 22 | pub col: u32, |
23 | } | 23 | } |
24 | 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, | ||
31 | } | ||
32 | |||
25 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] | 33 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] |
26 | pub(crate) struct Utf16Char { | 34 | pub(crate) struct Utf16Char { |
27 | /// Start offset of a character inside a line, zero-based | 35 | /// Start offset of a character inside a line, zero-based |
@@ -88,18 +96,25 @@ impl LineIndex { | |||
88 | LineIndex { newlines, utf16_lines } | 96 | LineIndex { newlines, utf16_lines } |
89 | } | 97 | } |
90 | 98 | ||
91 | pub fn line_col(&self, offset: TextSize) -> LineColUtf16 { | 99 | pub fn line_col(&self, offset: TextSize) -> LineCol { |
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; |
103 | LineCol { line: line as u32, col: col.into() } | ||
104 | } | ||
105 | |||
106 | pub fn offset(&self, line_col: LineCol) -> TextSize { | ||
107 | self.newlines[line_col.line as usize] + TextSize::from(line_col.col) | ||
108 | } | ||
95 | 109 | ||
96 | LineColUtf16 { line: line as u32, col: self.utf8_to_utf16_col(line as u32, col) as u32 } | 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 } | ||
97 | } | 113 | } |
98 | 114 | ||
99 | pub fn offset(&self, line_col: LineColUtf16) -> TextSize { | 115 | pub fn to_utf8(&self, line_col: LineColUtf16) -> LineCol { |
100 | //FIXME: return Result | ||
101 | let col = self.utf16_to_utf8_col(line_col.line, line_col.col); | 116 | let col = self.utf16_to_utf8_col(line_col.line, line_col.col); |
102 | self.newlines[line_col.line as usize] + 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 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() { | |||
17 | 17 | ||
18 | let index = LineIndex::new(text); | 18 | let index = LineIndex::new(text); |
19 | for &(offset, line, col) in &table { | 19 | for &(offset, line, col) in &table { |
20 | assert_eq!(index.line_col(offset.into()), LineColUtf16 { line, col }); | 20 | assert_eq!(index.line_col(offset.into()), LineCol { line, col }); |
21 | } | 21 | } |
22 | 22 | ||
23 | 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)]; | 24 | let table = [(0, 0, 0), (1, 1, 0), (2, 1, 1), (6, 1, 5), (7, 2, 0)]; |
25 | let index = LineIndex::new(text); | 25 | let index = LineIndex::new(text); |
26 | for &(offset, line, col) in &table { | 26 | for &(offset, line, col) in &table { |
27 | assert_eq!(index.line_col(offset.into()), LineColUtf16 { line, col }); | 27 | assert_eq!(index.line_col(offset.into()), LineCol { line, col }); |
28 | } | 28 | } |
29 | } | 29 | } |
30 | 30 | ||