aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_db/src/line_index.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-04-24 22:40:41 +0100
committerAleksey Kladov <[email protected]>2020-04-25 10:59:18 +0100
commitb1d5817dd18b7b5fc102a63b084b1ee7ff4f9996 (patch)
treee5d136c5ba4a6ba96aeeb423e6e3f64ca7cea3f9 /crates/ra_ide_db/src/line_index.rs
parent27a7718880d93f55f905da606d108d3b3c682ab4 (diff)
Convert code to text-size
Diffstat (limited to 'crates/ra_ide_db/src/line_index.rs')
-rw-r--r--crates/ra_ide_db/src/line_index.rs54
1 files changed, 27 insertions, 27 deletions
diff --git a/crates/ra_ide_db/src/line_index.rs b/crates/ra_ide_db/src/line_index.rs
index 8ae745ff2..7794dc9fd 100644
--- a/crates/ra_ide_db/src/line_index.rs
+++ b/crates/ra_ide_db/src/line_index.rs
@@ -1,14 +1,14 @@
1//! `LineIndex` maps flat `TextUnit` offsets into `(Line, Column)` 1//! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)`
2//! representation. 2//! representation.
3use std::iter; 3use std::iter;
4 4// TODO: un TextSize
5use ra_syntax::{TextRange, TextUnit}; 5use ra_syntax::{TextRange, TextSize};
6use rustc_hash::FxHashMap; 6use rustc_hash::FxHashMap;
7use superslice::Ext; 7use superslice::Ext;
8 8
9#[derive(Clone, Debug, PartialEq, Eq)] 9#[derive(Clone, Debug, PartialEq, Eq)]
10pub struct LineIndex { 10pub struct LineIndex {
11 pub(crate) newlines: Vec<TextUnit>, 11 pub(crate) newlines: Vec<TextSize>,
12 pub(crate) utf16_lines: FxHashMap<u32, Vec<Utf16Char>>, 12 pub(crate) utf16_lines: FxHashMap<u32, Vec<Utf16Char>>,
13} 13}
14 14
@@ -22,12 +22,12 @@ pub struct LineCol {
22 22
23#[derive(Clone, Debug, Hash, PartialEq, Eq)] 23#[derive(Clone, Debug, Hash, PartialEq, Eq)]
24pub(crate) struct Utf16Char { 24pub(crate) struct Utf16Char {
25 pub(crate) start: TextUnit, 25 pub(crate) start: TextSize,
26 pub(crate) end: TextUnit, 26 pub(crate) end: TextSize,
27} 27}
28 28
29impl Utf16Char { 29impl Utf16Char {
30 fn len(&self) -> TextUnit { 30 fn len(&self) -> TextSize {
31 self.end - self.start 31 self.end - self.start
32 } 32 }
33} 33}
@@ -42,7 +42,7 @@ impl LineIndex {
42 let mut curr_col = 0.into(); 42 let mut curr_col = 0.into();
43 let mut line = 0; 43 let mut line = 0;
44 for c in text.chars() { 44 for c in text.chars() {
45 curr_row += TextUnit::of_char(c); 45 curr_row += TextSize::of(c);
46 if c == '\n' { 46 if c == '\n' {
47 newlines.push(curr_row); 47 newlines.push(curr_row);
48 48
@@ -58,8 +58,8 @@ impl LineIndex {
58 continue; 58 continue;
59 } 59 }
60 60
61 let char_len = TextUnit::of_char(c); 61 let char_len = TextSize::of(c);
62 if char_len > TextUnit::from_usize(1) { 62 if char_len > TextSize::from_usize(1) {
63 utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + char_len }); 63 utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + char_len });
64 } 64 }
65 65
@@ -74,7 +74,7 @@ impl LineIndex {
74 LineIndex { newlines, utf16_lines } 74 LineIndex { newlines, utf16_lines }
75 } 75 }
76 76
77 pub fn line_col(&self, offset: TextUnit) -> LineCol { 77 pub fn line_col(&self, offset: TextSize) -> LineCol {
78 let line = self.newlines.upper_bound(&offset) - 1; 78 let line = self.newlines.upper_bound(&offset) - 1;
79 let line_start_offset = self.newlines[line]; 79 let line_start_offset = self.newlines[line];
80 let col = offset - line_start_offset; 80 let col = offset - line_start_offset;
@@ -82,7 +82,7 @@ impl LineIndex {
82 LineCol { line: line as u32, col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32 } 82 LineCol { line: line as u32, col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32 }
83 } 83 }
84 84
85 pub fn offset(&self, line_col: LineCol) -> TextUnit { 85 pub fn offset(&self, line_col: LineCol) -> TextSize {
86 //FIXME: return Result 86 //FIXME: return Result
87 let col = self.utf16_to_utf8_col(line_col.line, line_col.col_utf16); 87 let col = self.utf16_to_utf8_col(line_col.line, line_col.col_utf16);
88 self.newlines[line_col.line as usize] + col 88 self.newlines[line_col.line as usize] + col
@@ -97,16 +97,16 @@ impl LineIndex {
97 97
98 all.clone() 98 all.clone()
99 .zip(all.skip(1)) 99 .zip(all.skip(1))
100 .map(|(lo, hi)| TextRange::from_to(lo, hi)) 100 .map(|(lo, hi)| TextRange::new(lo, hi))
101 .filter(|it| !it.is_empty()) 101 .filter(|it| !it.is_empty())
102 } 102 }
103 103
104 fn utf8_to_utf16_col(&self, line: u32, col: TextUnit) -> usize { 104 fn utf8_to_utf16_col(&self, line: u32, col: TextSize) -> usize {
105 if let Some(utf16_chars) = self.utf16_lines.get(&line) { 105 if let Some(utf16_chars) = self.utf16_lines.get(&line) {
106 let mut correction = 0; 106 let mut correction = 0;
107 for c in utf16_chars { 107 for c in utf16_chars {
108 if col >= c.end { 108 if col >= c.end {
109 correction += c.len().to_usize() - 1; 109 correction += usize::from(c.len()) - 1;
110 } else { 110 } else {
111 // From here on, all utf16 characters come *after* the character we are mapping, 111 // From here on, all utf16 characters come *after* the character we are mapping,
112 // so we don't need to take them into account 112 // so we don't need to take them into account
@@ -114,18 +114,18 @@ impl LineIndex {
114 } 114 }
115 } 115 }
116 116
117 col.to_usize() - correction 117 usize::from(col) - correction
118 } else { 118 } else {
119 col.to_usize() 119 usize::from(col)
120 } 120 }
121 } 121 }
122 122
123 fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextUnit { 123 fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextSize {
124 let mut col: TextUnit = col.into(); 124 let mut col: TextSize = col.into();
125 if let Some(utf16_chars) = self.utf16_lines.get(&line) { 125 if let Some(utf16_chars) = self.utf16_lines.get(&line) {
126 for c in utf16_chars { 126 for c in utf16_chars {
127 if col >= c.start { 127 if col >= c.start {
128 col += c.len() - TextUnit::from_usize(1); 128 col += c.len() - TextSize::from_usize(1);
129 } else { 129 } else {
130 // From here on, all utf16 characters come *after* the character we are mapping, 130 // From here on, all utf16 characters come *after* the character we are mapping,
131 // so we don't need to take them into account 131 // so we don't need to take them into account
@@ -200,10 +200,10 @@ const C: char = 'メ';
200 assert_eq!(col_index.utf8_to_utf16_col(1, 22.into()), 20); 200 assert_eq!(col_index.utf8_to_utf16_col(1, 22.into()), 20);
201 201
202 // UTF-16 to UTF-8, no changes 202 // UTF-16 to UTF-8, no changes
203 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextUnit::from(15)); 203 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from(15));
204 204
205 // UTF-16 to UTF-8 205 // UTF-16 to UTF-8
206 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextUnit::from(21)); 206 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(21));
207 } 207 }
208 208
209 #[test] 209 #[test]
@@ -228,18 +228,18 @@ const C: char = \"メ メ\";
228 assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15); 228 assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15);
229 229
230 // UTF-16 to UTF-8 230 // UTF-16 to UTF-8
231 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextUnit::from_usize(15)); 231 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from_usize(15));
232 232
233 assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextUnit::from_usize(20)); 233 assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextSize::from_usize(20));
234 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextUnit::from_usize(23)); 234 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from_usize(23));
235 235
236 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15)); 236 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextSize::from_usize(15));
237 } 237 }
238 238
239 #[test] 239 #[test]
240 fn test_splitlines() { 240 fn test_splitlines() {
241 fn r(lo: u32, hi: u32) -> TextRange { 241 fn r(lo: u32, hi: u32) -> TextRange {
242 TextRange::from_to(lo.into(), hi.into()) 242 TextRange::new(lo.into(), hi.into())
243 } 243 }
244 244
245 let text = "a\nbb\nccc\n"; 245 let text = "a\nbb\nccc\n";