|
1 |
| -//! FIXME: write short doc here |
| 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. |
2 | 9 |
|
3 | 10 | use ra_syntax::{TextRange, TextUnit};
|
4 | 11 | use ra_text_edit::{AtomTextEdit, TextEdit};
|
5 | 12 |
|
6 | 13 | use crate::line_index::{LineCol, LineIndex, Utf16Char};
|
7 | 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 | + |
8 | 85 | #[derive(Debug, Clone)]
|
9 | 86 | enum Step {
|
10 | 87 | Newline(TextUnit),
|
@@ -55,7 +132,7 @@ struct OffsetStepIter<'a> {
|
55 | 132 | offset: TextUnit,
|
56 | 133 | }
|
57 | 134 |
|
58 |
| -impl<'a> Iterator for OffsetStepIter<'a> { |
| 135 | +impl Iterator for OffsetStepIter<'_> { |
59 | 136 | type Item = Step;
|
60 | 137 | fn next(&mut self) -> Option<Step> {
|
61 | 138 | let (next, next_offset) = self
|
@@ -221,76 +298,6 @@ impl RunningLineCol {
|
221 | 298 | }
|
222 | 299 | }
|
223 | 300 |
|
224 |
| -pub fn translate_offset_with_edit( |
225 |
| - line_index: &LineIndex, |
226 |
| - offset: TextUnit, |
227 |
| - text_edit: &TextEdit, |
228 |
| -) -> LineCol { |
229 |
| - let mut state = Edits::from_text_edit(&text_edit); |
230 |
| - |
231 |
| - let mut res = RunningLineCol::new(); |
232 |
| - |
233 |
| - macro_rules! test_step { |
234 |
| - ($x:ident) => { |
235 |
| - match &$x { |
236 |
| - Step::Newline(n) => { |
237 |
| - if offset < *n { |
238 |
| - return res.to_line_col(offset); |
239 |
| - } else { |
240 |
| - res.add_line(*n); |
241 |
| - } |
242 |
| - } |
243 |
| - Step::Utf16Char(x) => { |
244 |
| - if offset < x.end() { |
245 |
| - // if the offset is inside a multibyte char it's invalid |
246 |
| - // clamp it to the start of the char |
247 |
| - let clamp = offset.min(x.start()); |
248 |
| - return res.to_line_col(clamp); |
249 |
| - } else { |
250 |
| - res.adjust_col(*x); |
251 |
| - } |
252 |
| - } |
253 |
| - } |
254 |
| - }; |
255 |
| - } |
256 |
| - |
257 |
| - for orig_step in LineIndexStepIter::from(line_index) { |
258 |
| - loop { |
259 |
| - let translated_step = state.translate_step(&orig_step); |
260 |
| - match state.next_steps(&translated_step) { |
261 |
| - NextSteps::Use => { |
262 |
| - test_step!(translated_step); |
263 |
| - break; |
264 |
| - } |
265 |
| - NextSteps::ReplaceMany(ns) => { |
266 |
| - for n in ns { |
267 |
| - test_step!(n); |
268 |
| - } |
269 |
| - break; |
270 |
| - } |
271 |
| - NextSteps::AddMany(ns) => { |
272 |
| - for n in ns { |
273 |
| - test_step!(n); |
274 |
| - } |
275 |
| - } |
276 |
| - } |
277 |
| - } |
278 |
| - } |
279 |
| - |
280 |
| - loop { |
281 |
| - match state.next_inserted_steps() { |
282 |
| - None => break, |
283 |
| - Some(ns) => { |
284 |
| - for n in ns { |
285 |
| - test_step!(n); |
286 |
| - } |
287 |
| - } |
288 |
| - } |
289 |
| - } |
290 |
| - |
291 |
| - res.to_line_col(offset) |
292 |
| -} |
293 |
| - |
294 | 301 | #[cfg(test)]
|
295 | 302 | mod test {
|
296 | 303 | use proptest::{prelude::*, proptest};
|
|
0 commit comments