diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-02-06 14:14:47 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-02-06 14:14:47 +0000 |
commit | ff2d77bde6acffc5e4c42878606b3d6d92300e11 (patch) | |
tree | 76b0a8bbf94b0c68dd3aa891e0d9ea5cdf067863 /crates/ra_ide_db/src/line_index_utils.rs | |
parent | 19de59a9233a09a9b70a96a6c49213b119819c46 (diff) | |
parent | 355c98fd0861acf0f0fddad08cbc923fee0698fb (diff) |
Merge #3029
3029: Docs r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_db/src/line_index_utils.rs')
-rw-r--r-- | crates/ra_ide_db/src/line_index_utils.rs | 341 |
1 files changed, 341 insertions, 0 deletions
diff --git a/crates/ra_ide_db/src/line_index_utils.rs b/crates/ra_ide_db/src/line_index_utils.rs new file mode 100644 index 000000000..435b06511 --- /dev/null +++ b/crates/ra_ide_db/src/line_index_utils.rs | |||
@@ -0,0 +1,341 @@ | |||
1 | //! Code actions can specify desirable final position of the cursor. | ||
2 | //! | ||
3 | //! The position is specified as a `TextUnit` in the final file. We need to send | ||
4 | //! it in `(Line, Column)` coordinate though. However, we only have a LineIndex | ||
5 | //! for a file pre-edit! | ||
6 | //! | ||
7 | //! Code in this module applies this "to (Line, Column) after edit" | ||
8 | //! transformation. | ||
9 | |||
10 | use ra_syntax::{TextRange, TextUnit}; | ||
11 | use ra_text_edit::{AtomTextEdit, TextEdit}; | ||
12 | |||
13 | use crate::line_index::{LineCol, LineIndex, Utf16Char}; | ||
14 | |||
15 | pub fn translate_offset_with_edit( | ||
16 | line_index: &LineIndex, | ||
17 | offset: TextUnit, | ||
18 | text_edit: &TextEdit, | ||
19 | ) -> LineCol { | ||
20 | let mut state = Edits::from_text_edit(&text_edit); | ||
21 | |||
22 | let mut res = RunningLineCol::new(); | ||
23 | |||
24 | macro_rules! test_step { | ||
25 | ($x:ident) => { | ||
26 | match &$x { | ||
27 | Step::Newline(n) => { | ||
28 | if offset < *n { | ||
29 | return res.to_line_col(offset); | ||
30 | } else { | ||
31 | res.add_line(*n); | ||
32 | } | ||
33 | } | ||
34 | Step::Utf16Char(x) => { | ||
35 | if offset < x.end() { | ||
36 | // if the offset is inside a multibyte char it's invalid | ||
37 | // clamp it to the start of the char | ||
38 | let clamp = offset.min(x.start()); | ||
39 | return res.to_line_col(clamp); | ||
40 | } else { | ||
41 | res.adjust_col(*x); | ||
42 | } | ||
43 | } | ||
44 | } | ||
45 | }; | ||
46 | } | ||
47 | |||
48 | for orig_step in LineIndexStepIter::from(line_index) { | ||
49 | loop { | ||
50 | let translated_step = state.translate_step(&orig_step); | ||
51 | match state.next_steps(&translated_step) { | ||
52 | NextSteps::Use => { | ||
53 | test_step!(translated_step); | ||
54 | break; | ||
55 | } | ||
56 | NextSteps::ReplaceMany(ns) => { | ||
57 | for n in ns { | ||
58 | test_step!(n); | ||
59 | } | ||
60 | break; | ||
61 | } | ||
62 | NextSteps::AddMany(ns) => { | ||
63 | for n in ns { | ||
64 | test_step!(n); | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | |||
71 | loop { | ||
72 | match state.next_inserted_steps() { | ||
73 | None => break, | ||
74 | Some(ns) => { | ||
75 | for n in ns { | ||
76 | test_step!(n); | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | |||
82 | res.to_line_col(offset) | ||
83 | } | ||
84 | |||
85 | #[derive(Debug, Clone)] | ||
86 | enum Step { | ||
87 | Newline(TextUnit), | ||
88 | Utf16Char(TextRange), | ||
89 | } | ||
90 | |||
91 | #[derive(Debug)] | ||
92 | struct LineIndexStepIter<'a> { | ||
93 | line_index: &'a LineIndex, | ||
94 | next_newline_idx: usize, | ||
95 | utf16_chars: Option<(TextUnit, std::slice::Iter<'a, Utf16Char>)>, | ||
96 | } | ||
97 | |||
98 | impl LineIndexStepIter<'_> { | ||
99 | fn from(line_index: &LineIndex) -> LineIndexStepIter { | ||
100 | let mut x = LineIndexStepIter { line_index, next_newline_idx: 0, utf16_chars: None }; | ||
101 | // skip first newline since it's not real | ||
102 | x.next(); | ||
103 | x | ||
104 | } | ||
105 | } | ||
106 | |||
107 | impl Iterator for LineIndexStepIter<'_> { | ||
108 | type Item = Step; | ||
109 | fn next(&mut self) -> Option<Step> { | ||
110 | self.utf16_chars | ||
111 | .as_mut() | ||
112 | .and_then(|(newline, x)| { | ||
113 | let x = x.next()?; | ||
114 | Some(Step::Utf16Char(TextRange::from_to(*newline + x.start, *newline + x.end))) | ||
115 | }) | ||
116 | .or_else(|| { | ||
117 | let next_newline = *self.line_index.newlines.get(self.next_newline_idx)?; | ||
118 | self.utf16_chars = self | ||
119 | .line_index | ||
120 | .utf16_lines | ||
121 | .get(&(self.next_newline_idx as u32)) | ||
122 | .map(|x| (next_newline, x.iter())); | ||
123 | self.next_newline_idx += 1; | ||
124 | Some(Step::Newline(next_newline)) | ||
125 | }) | ||
126 | } | ||
127 | } | ||
128 | |||
129 | #[derive(Debug)] | ||
130 | struct OffsetStepIter<'a> { | ||
131 | text: &'a str, | ||
132 | offset: TextUnit, | ||
133 | } | ||
134 | |||
135 | impl Iterator for OffsetStepIter<'_> { | ||
136 | type Item = Step; | ||
137 | fn next(&mut self) -> Option<Step> { | ||
138 | let (next, next_offset) = self | ||
139 | .text | ||
140 | .char_indices() | ||
141 | .filter_map(|(i, c)| { | ||
142 | if c == '\n' { | ||
143 | let next_offset = self.offset + TextUnit::from_usize(i + 1); | ||
144 | let next = Step::Newline(next_offset); | ||
145 | Some((next, next_offset)) | ||
146 | } else { | ||
147 | let char_len = TextUnit::of_char(c); | ||
148 | if char_len.to_usize() > 1 { | ||
149 | let start = self.offset + TextUnit::from_usize(i); | ||
150 | let end = start + char_len; | ||
151 | let next = Step::Utf16Char(TextRange::from_to(start, end)); | ||
152 | let next_offset = end; | ||
153 | Some((next, next_offset)) | ||
154 | } else { | ||
155 | None | ||
156 | } | ||
157 | } | ||
158 | }) | ||
159 | .next()?; | ||
160 | let next_idx = (next_offset - self.offset).to_usize(); | ||
161 | self.text = &self.text[next_idx..]; | ||
162 | self.offset = next_offset; | ||
163 | Some(next) | ||
164 | } | ||
165 | } | ||
166 | |||
167 | #[derive(Debug)] | ||
168 | enum NextSteps<'a> { | ||
169 | Use, | ||
170 | ReplaceMany(OffsetStepIter<'a>), | ||
171 | AddMany(OffsetStepIter<'a>), | ||
172 | } | ||
173 | |||
174 | #[derive(Debug)] | ||
175 | struct TranslatedEdit<'a> { | ||
176 | delete: TextRange, | ||
177 | insert: &'a str, | ||
178 | diff: i64, | ||
179 | } | ||
180 | |||
181 | struct Edits<'a> { | ||
182 | edits: &'a [AtomTextEdit], | ||
183 | current: Option<TranslatedEdit<'a>>, | ||
184 | acc_diff: i64, | ||
185 | } | ||
186 | |||
187 | impl<'a> Edits<'a> { | ||
188 | fn from_text_edit(text_edit: &'a TextEdit) -> Edits<'a> { | ||
189 | let mut x = Edits { edits: text_edit.as_atoms(), current: None, acc_diff: 0 }; | ||
190 | x.advance_edit(); | ||
191 | x | ||
192 | } | ||
193 | fn advance_edit(&mut self) { | ||
194 | self.acc_diff += self.current.as_ref().map_or(0, |x| x.diff); | ||
195 | match self.edits.split_first() { | ||
196 | Some((next, rest)) => { | ||
197 | let delete = self.translate_range(next.delete); | ||
198 | let diff = next.insert.len() as i64 - next.delete.len().to_usize() as i64; | ||
199 | self.current = Some(TranslatedEdit { delete, insert: &next.insert, diff }); | ||
200 | self.edits = rest; | ||
201 | } | ||
202 | None => { | ||
203 | self.current = None; | ||
204 | } | ||
205 | } | ||
206 | } | ||
207 | |||
208 | fn next_inserted_steps(&mut self) -> Option<OffsetStepIter<'a>> { | ||
209 | let cur = self.current.as_ref()?; | ||
210 | let res = Some(OffsetStepIter { offset: cur.delete.start(), text: &cur.insert }); | ||
211 | self.advance_edit(); | ||
212 | res | ||
213 | } | ||
214 | |||
215 | fn next_steps(&mut self, step: &Step) -> NextSteps { | ||
216 | let step_pos = match *step { | ||
217 | Step::Newline(n) => n, | ||
218 | Step::Utf16Char(r) => r.end(), | ||
219 | }; | ||
220 | match &mut self.current { | ||
221 | Some(edit) => { | ||
222 | if step_pos <= edit.delete.start() { | ||
223 | NextSteps::Use | ||
224 | } else if step_pos <= edit.delete.end() { | ||
225 | let iter = OffsetStepIter { offset: edit.delete.start(), text: &edit.insert }; | ||
226 | // empty slice to avoid returning steps again | ||
227 | edit.insert = &edit.insert[edit.insert.len()..]; | ||
228 | NextSteps::ReplaceMany(iter) | ||
229 | } else { | ||
230 | let iter = OffsetStepIter { offset: edit.delete.start(), text: &edit.insert }; | ||
231 | // empty slice to avoid returning steps again | ||
232 | edit.insert = &edit.insert[edit.insert.len()..]; | ||
233 | self.advance_edit(); | ||
234 | NextSteps::AddMany(iter) | ||
235 | } | ||
236 | } | ||
237 | None => NextSteps::Use, | ||
238 | } | ||
239 | } | ||
240 | |||
241 | fn translate_range(&self, range: TextRange) -> TextRange { | ||
242 | if self.acc_diff == 0 { | ||
243 | range | ||
244 | } else { | ||
245 | let start = self.translate(range.start()); | ||
246 | let end = self.translate(range.end()); | ||
247 | TextRange::from_to(start, end) | ||
248 | } | ||
249 | } | ||
250 | |||
251 | fn translate(&self, x: TextUnit) -> TextUnit { | ||
252 | if self.acc_diff == 0 { | ||
253 | x | ||
254 | } else { | ||
255 | TextUnit::from((x.to_usize() as i64 + self.acc_diff) as u32) | ||
256 | } | ||
257 | } | ||
258 | |||
259 | fn translate_step(&self, x: &Step) -> Step { | ||
260 | if self.acc_diff == 0 { | ||
261 | x.clone() | ||
262 | } else { | ||
263 | match *x { | ||
264 | Step::Newline(n) => Step::Newline(self.translate(n)), | ||
265 | Step::Utf16Char(r) => Step::Utf16Char(self.translate_range(r)), | ||
266 | } | ||
267 | } | ||
268 | } | ||
269 | } | ||
270 | |||
271 | #[derive(Debug)] | ||
272 | struct RunningLineCol { | ||
273 | line: u32, | ||
274 | last_newline: TextUnit, | ||
275 | col_adjust: TextUnit, | ||
276 | } | ||
277 | |||
278 | impl RunningLineCol { | ||
279 | fn new() -> RunningLineCol { | ||
280 | RunningLineCol { line: 0, last_newline: TextUnit::from(0), col_adjust: TextUnit::from(0) } | ||
281 | } | ||
282 | |||
283 | fn to_line_col(&self, offset: TextUnit) -> LineCol { | ||
284 | LineCol { | ||
285 | line: self.line, | ||
286 | col_utf16: ((offset - self.last_newline) - self.col_adjust).into(), | ||
287 | } | ||
288 | } | ||
289 | |||
290 | fn add_line(&mut self, newline: TextUnit) { | ||
291 | self.line += 1; | ||
292 | self.last_newline = newline; | ||
293 | self.col_adjust = TextUnit::from(0); | ||
294 | } | ||
295 | |||
296 | fn adjust_col(&mut self, range: TextRange) { | ||
297 | self.col_adjust += range.len() - TextUnit::from(1); | ||
298 | } | ||
299 | } | ||
300 | |||
301 | #[cfg(test)] | ||
302 | mod test { | ||
303 | use proptest::{prelude::*, proptest}; | ||
304 | use ra_text_edit::test_utils::{arb_offset, arb_text_with_edit}; | ||
305 | use ra_text_edit::TextEdit; | ||
306 | |||
307 | use crate::line_index; | ||
308 | |||
309 | use super::*; | ||
310 | |||
311 | #[derive(Debug)] | ||
312 | struct ArbTextWithEditAndOffset { | ||
313 | text: String, | ||
314 | edit: TextEdit, | ||
315 | edited_text: String, | ||
316 | offset: TextUnit, | ||
317 | } | ||
318 | |||
319 | fn arb_text_with_edit_and_offset() -> BoxedStrategy<ArbTextWithEditAndOffset> { | ||
320 | arb_text_with_edit() | ||
321 | .prop_flat_map(|x| { | ||
322 | let edited_text = x.edit.apply(&x.text); | ||
323 | let arb_offset = arb_offset(&edited_text); | ||
324 | (Just(x), Just(edited_text), arb_offset).prop_map(|(x, edited_text, offset)| { | ||
325 | ArbTextWithEditAndOffset { text: x.text, edit: x.edit, edited_text, offset } | ||
326 | }) | ||
327 | }) | ||
328 | .boxed() | ||
329 | } | ||
330 | |||
331 | proptest! { | ||
332 | #[test] | ||
333 | fn test_translate_offset_with_edit(x in arb_text_with_edit_and_offset()) { | ||
334 | let expected = line_index::to_line_col(&x.edited_text, x.offset); | ||
335 | let line_index = LineIndex::new(&x.text); | ||
336 | let actual = translate_offset_with_edit(&line_index, x.offset, &x.edit); | ||
337 | |||
338 | assert_eq!(actual, expected); | ||
339 | } | ||
340 | } | ||
341 | } | ||