diff options
Diffstat (limited to 'crates/ide_db/src/line_index.rs')
-rw-r--r-- | crates/ide_db/src/line_index.rs | 281 |
1 files changed, 281 insertions, 0 deletions
diff --git a/crates/ide_db/src/line_index.rs b/crates/ide_db/src/line_index.rs new file mode 100644 index 000000000..a381f7fb8 --- /dev/null +++ b/crates/ide_db/src/line_index.rs | |||
@@ -0,0 +1,281 @@ | |||
1 | //! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)` | ||
2 | //! representation. | ||
3 | use std::iter; | ||
4 | |||
5 | use rustc_hash::FxHashMap; | ||
6 | use stdx::partition_point; | ||
7 | use syntax::{TextRange, TextSize}; | ||
8 | |||
9 | #[derive(Clone, Debug, PartialEq, Eq)] | ||
10 | pub struct LineIndex { | ||
11 | /// Offset the the beginning of each line, zero-based | ||
12 | pub(crate) newlines: Vec<TextSize>, | ||
13 | /// List of non-ASCII characters on each line | ||
14 | pub(crate) utf16_lines: FxHashMap<u32, Vec<Utf16Char>>, | ||
15 | } | ||
16 | |||
17 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
18 | pub struct LineCol { | ||
19 | /// Zero-based | ||
20 | pub line: u32, | ||
21 | /// Zero-based | ||
22 | pub col_utf16: u32, | ||
23 | } | ||
24 | |||
25 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] | ||
26 | pub(crate) struct Utf16Char { | ||
27 | /// Start offset of a character inside a line, zero-based | ||
28 | pub(crate) start: TextSize, | ||
29 | /// End offset of a character inside a line, zero-based | ||
30 | pub(crate) end: TextSize, | ||
31 | } | ||
32 | |||
33 | impl Utf16Char { | ||
34 | /// Returns the length in 8-bit UTF-8 code units. | ||
35 | fn len(&self) -> TextSize { | ||
36 | self.end - self.start | ||
37 | } | ||
38 | |||
39 | /// Returns the length in 16-bit UTF-16 code units. | ||
40 | fn len_utf16(&self) -> usize { | ||
41 | if self.len() == TextSize::from(4) { | ||
42 | 2 | ||
43 | } else { | ||
44 | 1 | ||
45 | } | ||
46 | } | ||
47 | } | ||
48 | |||
49 | impl LineIndex { | ||
50 | pub fn new(text: &str) -> LineIndex { | ||
51 | let mut utf16_lines = FxHashMap::default(); | ||
52 | let mut utf16_chars = Vec::new(); | ||
53 | |||
54 | let mut newlines = vec![0.into()]; | ||
55 | let mut curr_row = 0.into(); | ||
56 | let mut curr_col = 0.into(); | ||
57 | let mut line = 0; | ||
58 | for c in text.chars() { | ||
59 | let c_len = TextSize::of(c); | ||
60 | curr_row += c_len; | ||
61 | if c == '\n' { | ||
62 | newlines.push(curr_row); | ||
63 | |||
64 | // Save any utf-16 characters seen in the previous line | ||
65 | if !utf16_chars.is_empty() { | ||
66 | utf16_lines.insert(line, utf16_chars); | ||
67 | utf16_chars = Vec::new(); | ||
68 | } | ||
69 | |||
70 | // Prepare for processing the next line | ||
71 | curr_col = 0.into(); | ||
72 | line += 1; | ||
73 | continue; | ||
74 | } | ||
75 | |||
76 | if !c.is_ascii() { | ||
77 | utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + c_len }); | ||
78 | } | ||
79 | |||
80 | curr_col += c_len; | ||
81 | } | ||
82 | |||
83 | // Save any utf-16 characters seen in the last line | ||
84 | if !utf16_chars.is_empty() { | ||
85 | utf16_lines.insert(line, utf16_chars); | ||
86 | } | ||
87 | |||
88 | LineIndex { newlines, utf16_lines } | ||
89 | } | ||
90 | |||
91 | pub fn line_col(&self, offset: TextSize) -> LineCol { | ||
92 | let line = partition_point(&self.newlines, |&it| it <= offset) - 1; | ||
93 | let line_start_offset = self.newlines[line]; | ||
94 | let col = offset - line_start_offset; | ||
95 | |||
96 | LineCol { line: line as u32, col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32 } | ||
97 | } | ||
98 | |||
99 | pub fn offset(&self, line_col: LineCol) -> TextSize { | ||
100 | //FIXME: return Result | ||
101 | let col = self.utf16_to_utf8_col(line_col.line, line_col.col_utf16); | ||
102 | self.newlines[line_col.line as usize] + col | ||
103 | } | ||
104 | |||
105 | pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ { | ||
106 | let lo = partition_point(&self.newlines, |&it| it < range.start()); | ||
107 | let hi = partition_point(&self.newlines, |&it| it <= range.end()); | ||
108 | let all = iter::once(range.start()) | ||
109 | .chain(self.newlines[lo..hi].iter().copied()) | ||
110 | .chain(iter::once(range.end())); | ||
111 | |||
112 | all.clone() | ||
113 | .zip(all.skip(1)) | ||
114 | .map(|(lo, hi)| TextRange::new(lo, hi)) | ||
115 | .filter(|it| !it.is_empty()) | ||
116 | } | ||
117 | |||
118 | fn utf8_to_utf16_col(&self, line: u32, col: TextSize) -> usize { | ||
119 | let mut res: usize = col.into(); | ||
120 | if let Some(utf16_chars) = self.utf16_lines.get(&line) { | ||
121 | for c in utf16_chars { | ||
122 | if c.end <= col { | ||
123 | res -= usize::from(c.len()) - c.len_utf16(); | ||
124 | } else { | ||
125 | // From here on, all utf16 characters come *after* the character we are mapping, | ||
126 | // so we don't need to take them into account | ||
127 | break; | ||
128 | } | ||
129 | } | ||
130 | } | ||
131 | res | ||
132 | } | ||
133 | |||
134 | fn utf16_to_utf8_col(&self, line: u32, mut col: u32) -> TextSize { | ||
135 | if let Some(utf16_chars) = self.utf16_lines.get(&line) { | ||
136 | for c in utf16_chars { | ||
137 | if col > u32::from(c.start) { | ||
138 | col += u32::from(c.len()) - c.len_utf16() as u32; | ||
139 | } else { | ||
140 | // From here on, all utf16 characters come *after* the character we are mapping, | ||
141 | // so we don't need to take them into account | ||
142 | break; | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | |||
147 | col.into() | ||
148 | } | ||
149 | } | ||
150 | |||
151 | #[cfg(test)] | ||
152 | mod tests { | ||
153 | use super::*; | ||
154 | |||
155 | #[test] | ||
156 | fn test_line_index() { | ||
157 | let text = "hello\nworld"; | ||
158 | let index = LineIndex::new(text); | ||
159 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 }); | ||
160 | assert_eq!(index.line_col(1.into()), LineCol { line: 0, col_utf16: 1 }); | ||
161 | assert_eq!(index.line_col(5.into()), LineCol { line: 0, col_utf16: 5 }); | ||
162 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 0 }); | ||
163 | assert_eq!(index.line_col(7.into()), LineCol { line: 1, col_utf16: 1 }); | ||
164 | assert_eq!(index.line_col(8.into()), LineCol { line: 1, col_utf16: 2 }); | ||
165 | assert_eq!(index.line_col(10.into()), LineCol { line: 1, col_utf16: 4 }); | ||
166 | assert_eq!(index.line_col(11.into()), LineCol { line: 1, col_utf16: 5 }); | ||
167 | assert_eq!(index.line_col(12.into()), LineCol { line: 1, col_utf16: 6 }); | ||
168 | |||
169 | let text = "\nhello\nworld"; | ||
170 | let index = LineIndex::new(text); | ||
171 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 }); | ||
172 | assert_eq!(index.line_col(1.into()), LineCol { line: 1, col_utf16: 0 }); | ||
173 | assert_eq!(index.line_col(2.into()), LineCol { line: 1, col_utf16: 1 }); | ||
174 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 5 }); | ||
175 | assert_eq!(index.line_col(7.into()), LineCol { line: 2, col_utf16: 0 }); | ||
176 | } | ||
177 | |||
178 | #[test] | ||
179 | fn test_char_len() { | ||
180 | assert_eq!('メ'.len_utf8(), 3); | ||
181 | assert_eq!('メ'.len_utf16(), 1); | ||
182 | } | ||
183 | |||
184 | #[test] | ||
185 | fn test_empty_index() { | ||
186 | let col_index = LineIndex::new( | ||
187 | " | ||
188 | const C: char = 'x'; | ||
189 | ", | ||
190 | ); | ||
191 | assert_eq!(col_index.utf16_lines.len(), 0); | ||
192 | } | ||
193 | |||
194 | #[test] | ||
195 | fn test_single_char() { | ||
196 | let col_index = LineIndex::new( | ||
197 | " | ||
198 | const C: char = 'メ'; | ||
199 | ", | ||
200 | ); | ||
201 | |||
202 | assert_eq!(col_index.utf16_lines.len(), 1); | ||
203 | assert_eq!(col_index.utf16_lines[&1].len(), 1); | ||
204 | assert_eq!(col_index.utf16_lines[&1][0], Utf16Char { start: 17.into(), end: 20.into() }); | ||
205 | |||
206 | // UTF-8 to UTF-16, no changes | ||
207 | assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15); | ||
208 | |||
209 | // UTF-8 to UTF-16 | ||
210 | assert_eq!(col_index.utf8_to_utf16_col(1, 22.into()), 20); | ||
211 | |||
212 | // UTF-16 to UTF-8, no changes | ||
213 | assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from(15)); | ||
214 | |||
215 | // UTF-16 to UTF-8 | ||
216 | assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(21)); | ||
217 | |||
218 | let col_index = LineIndex::new("a𐐏b"); | ||
219 | assert_eq!(col_index.utf16_to_utf8_col(0, 3), TextSize::from(5)); | ||
220 | } | ||
221 | |||
222 | #[test] | ||
223 | fn test_string() { | ||
224 | let col_index = LineIndex::new( | ||
225 | " | ||
226 | const C: char = \"メ メ\"; | ||
227 | ", | ||
228 | ); | ||
229 | |||
230 | assert_eq!(col_index.utf16_lines.len(), 1); | ||
231 | assert_eq!(col_index.utf16_lines[&1].len(), 2); | ||
232 | assert_eq!(col_index.utf16_lines[&1][0], Utf16Char { start: 17.into(), end: 20.into() }); | ||
233 | assert_eq!(col_index.utf16_lines[&1][1], Utf16Char { start: 21.into(), end: 24.into() }); | ||
234 | |||
235 | // UTF-8 to UTF-16 | ||
236 | assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15); | ||
237 | |||
238 | assert_eq!(col_index.utf8_to_utf16_col(1, 21.into()), 19); | ||
239 | assert_eq!(col_index.utf8_to_utf16_col(1, 25.into()), 21); | ||
240 | |||
241 | assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15); | ||
242 | |||
243 | // UTF-16 to UTF-8 | ||
244 | assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from(15)); | ||
245 | |||
246 | // メ UTF-8: 0xE3 0x83 0xA1, UTF-16: 0x30E1 | ||
247 | assert_eq!(col_index.utf16_to_utf8_col(1, 17), TextSize::from(17)); // first メ at 17..20 | ||
248 | assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextSize::from(20)); // space | ||
249 | assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(21)); // second メ at 21..24 | ||
250 | |||
251 | assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextSize::from(15)); | ||
252 | } | ||
253 | |||
254 | #[test] | ||
255 | fn test_splitlines() { | ||
256 | fn r(lo: u32, hi: u32) -> TextRange { | ||
257 | TextRange::new(lo.into(), hi.into()) | ||
258 | } | ||
259 | |||
260 | let text = "a\nbb\nccc\n"; | ||
261 | let line_index = LineIndex::new(text); | ||
262 | |||
263 | let actual = line_index.lines(r(0, 9)).collect::<Vec<_>>(); | ||
264 | let expected = vec![r(0, 2), r(2, 5), r(5, 9)]; | ||
265 | assert_eq!(actual, expected); | ||
266 | |||
267 | let text = ""; | ||
268 | let line_index = LineIndex::new(text); | ||
269 | |||
270 | let actual = line_index.lines(r(0, 0)).collect::<Vec<_>>(); | ||
271 | let expected = vec![]; | ||
272 | assert_eq!(actual, expected); | ||
273 | |||
274 | let text = "\n"; | ||
275 | let line_index = LineIndex::new(text); | ||
276 | |||
277 | let actual = line_index.lines(r(0, 1)).collect::<Vec<_>>(); | ||
278 | let expected = vec![r(0, 1)]; | ||
279 | assert_eq!(actual, expected) | ||
280 | } | ||
281 | } | ||