aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_db/src/line_index.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-04-24 23:57:47 +0100
committerAleksey Kladov <[email protected]>2020-04-25 10:59:18 +0100
commit63a462f37ca584e1a585a69e30823ce25d4d252f (patch)
tree005ab4d5b50f7d031be9f4056bd1fccd68473587 /crates/ra_ide_db/src/line_index.rs
parentdc2151085e9b117bc87307bf47edf3d17a170b49 (diff)
Switch to TryFrom
Diffstat (limited to 'crates/ra_ide_db/src/line_index.rs')
-rw-r--r--crates/ra_ide_db/src/line_index.rs22
1 files changed, 11 insertions, 11 deletions
diff --git a/crates/ra_ide_db/src/line_index.rs b/crates/ra_ide_db/src/line_index.rs
index 81eebc711..00ba95913 100644
--- a/crates/ra_ide_db/src/line_index.rs
+++ b/crates/ra_ide_db/src/line_index.rs
@@ -1,8 +1,9 @@
1//! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)` 1//! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)`
2//! representation. 2//! representation.
3use std::iter;
4
3use ra_syntax::{TextRange, TextSize}; 5use ra_syntax::{TextRange, TextSize};
4use rustc_hash::FxHashMap; 6use rustc_hash::FxHashMap;
5use std::iter;
6use superslice::Ext; 7use superslice::Ext;
7 8
8#[derive(Clone, Debug, PartialEq, Eq)] 9#[derive(Clone, Debug, PartialEq, Eq)]
@@ -116,12 +117,11 @@ impl LineIndex {
116 res 117 res
117 } 118 }
118 119
119 fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextSize { 120 fn utf16_to_utf8_col(&self, line: u32, mut col: u32) -> TextSize {
120 let mut col: TextSize = col.into();
121 if let Some(utf16_chars) = self.utf16_lines.get(&line) { 121 if let Some(utf16_chars) = self.utf16_lines.get(&line) {
122 for c in utf16_chars { 122 for c in utf16_chars {
123 if col >= c.start { 123 if col >= u32::from(c.start) {
124 col += c.len() - TextSize::from_usize(1); 124 col += u32::from(c.len()) - 1;
125 } else { 125 } else {
126 // From here on, all utf16 characters come *after* the character we are mapping, 126 // From here on, all utf16 characters come *after* the character we are mapping,
127 // so we don't need to take them into account 127 // so we don't need to take them into account
@@ -130,12 +130,12 @@ impl LineIndex {
130 } 130 }
131 } 131 }
132 132
133 col 133 col.into()
134 } 134 }
135} 135}
136 136
137#[cfg(test)] 137#[cfg(test)]
138mod test_line_index { 138mod tests {
139 use super::*; 139 use super::*;
140 140
141 #[test] 141 #[test]
@@ -224,12 +224,12 @@ const C: char = \"メ メ\";
224 assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15); 224 assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15);
225 225
226 // UTF-16 to UTF-8 226 // UTF-16 to UTF-8
227 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from_usize(15)); 227 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from(15));
228 228
229 assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextSize::from_usize(20)); 229 assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextSize::from(20));
230 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from_usize(23)); 230 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(23));
231 231
232 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextSize::from_usize(15)); 232 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextSize::from(15));
233 } 233 }
234 234
235 #[test] 235 #[test]