aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db/src/line_index.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_db/src/line_index.rs')
-rw-r--r--crates/ide_db/src/line_index.rs29
1 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 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)]
18pub struct LineCol { 18pub 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)]
26pub 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> + '_ {