diff options
author | Aleksey Kladov <[email protected]> | 2021-02-12 19:09:53 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-02-16 16:17:32 +0000 |
commit | 95209aa3f8e4b149da6adb374611ece76c2b82ca (patch) | |
tree | 37e5b2aa72b597586f27c5200d25abb349d6c11f /crates/ide_db/src/line_index.rs | |
parent | 00cc778c8c62e8f68531c1cadcce8b05b7287d84 (diff) |
Make utf8 default, implement utf16 in terms of it
Diffstat (limited to 'crates/ide_db/src/line_index.rs')
-rw-r--r-- | crates/ide_db/src/line_index.rs | 25 |
1 files changed, 20 insertions, 5 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> + '_ { |