aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_editor/src')
-rw-r--r--crates/ra_editor/src/line_index.rs23
-rw-r--r--crates/ra_editor/src/line_index_utils.rs63
2 files changed, 41 insertions, 45 deletions
diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs
index 5304fbcf6..898fee7e0 100644
--- a/crates/ra_editor/src/line_index.rs
+++ b/crates/ra_editor/src/line_index.rs
@@ -128,8 +128,8 @@ impl LineIndex {
128 } 128 }
129} 129}
130 130
131#[cfg(test)]
131/// Simple reference implementation to use in proptests 132/// Simple reference implementation to use in proptests
132/// and benchmarks as baseline
133pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { 133pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol {
134 let mut res = LineCol { 134 let mut res = LineCol {
135 line: 0, 135 line: 0,
@@ -270,6 +270,27 @@ mod test_line_index {
270 .boxed() 270 .boxed()
271 } 271 }
272 272
273 fn to_line_col(text: &str, offset: TextUnit) -> LineCol {
274 let mut res = LineCol {
275 line: 0,
276 col_utf16: 0,
277 };
278 for (i, c) in text.char_indices() {
279 if i + c.len_utf8() > offset.to_usize() {
280 // if it's an invalid offset, inside a multibyte char
281 // return as if it was at the start of the char
282 break;
283 }
284 if c == '\n' {
285 res.line += 1;
286 res.col_utf16 = 0;
287 } else {
288 res.col_utf16 += 1;
289 }
290 }
291 res
292 }
293
273 proptest! { 294 proptest! {
274 #[test] 295 #[test]
275 fn test_line_index_proptest((offset, text) in arb_text_with_offset()) { 296 fn test_line_index_proptest((offset, text) in arb_text_with_offset()) {
diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs
index ba3ac8aeb..b8b149442 100644
--- a/crates/ra_editor/src/line_index_utils.rs
+++ b/crates/ra_editor/src/line_index_utils.rs
@@ -1,6 +1,6 @@
1use ra_text_edit::AtomTextEdit; 1use ra_text_edit::AtomTextEdit;
2use ra_syntax::{TextUnit, TextRange}; 2use ra_syntax::{TextUnit, TextRange};
3use crate::{LineIndex, LineCol, line_index::{self, Utf16Char}}; 3use crate::{LineIndex, LineCol, line_index::Utf16Char};
4use superslice::Ext; 4use superslice::Ext;
5 5
6#[derive(Debug, Clone)] 6#[derive(Debug, Clone)]
@@ -325,59 +325,34 @@ pub fn translate_offset_with_edit(
325 res.to_line_col(offset) 325 res.to_line_col(offset)
326} 326}
327 327
328/// Simplest implementation to use as reference in proptest and benchmarks
329pub fn translate_after_edit(
330 pre_edit_text: &str,
331 offset: TextUnit,
332 edits: Vec<AtomTextEdit>,
333) -> LineCol {
334 let text = edit_text(pre_edit_text, edits);
335 line_index::to_line_col(&text, offset)
336}
337
338fn edit_text(pre_edit_text: &str, mut edits: Vec<AtomTextEdit>) -> String {
339 // apply edits ordered from last to first
340 // since they should not overlap we can just use start()
341 edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize));
342
343 let mut text = pre_edit_text.to_owned();
344
345 for edit in &edits {
346 let range = edit.delete.start().to_usize()..edit.delete.end().to_usize();
347 text.replace_range(range, &edit.insert);
348 }
349
350 text
351}
352
353#[cfg(test)] 328#[cfg(test)]
354mod test { 329mod test {
355 use super::*; 330 use super::*;
356 use proptest::{prelude::*, proptest, proptest_helper}; 331 use proptest::{prelude::*, proptest, proptest_helper};
357 use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; 332 use crate::line_index;
333 use ra_text_edit::test_utils::{arb_offset, arb_text_with_edits};
334 use ra_text_edit::TextEdit;
358 335
359 #[derive(Debug)] 336 #[derive(Debug)]
360 struct ArbTextWithOffsetAndEdits { 337 struct ArbTextWithOffsetAndEdits {
361 text: String, 338 text: String,
339 edits: TextEdit,
362 edited_text: String, 340 edited_text: String,
363 offset: TextUnit, 341 offset: TextUnit,
364 edits: Vec<AtomTextEdit>,
365 } 342 }
366 343
367 fn arb_text_with_offset_and_edits() -> BoxedStrategy<ArbTextWithOffsetAndEdits> { 344 fn arb_text_with_edits_and_offset() -> BoxedStrategy<ArbTextWithOffsetAndEdits> {
368 arb_text() 345 arb_text_with_edits()
369 .prop_flat_map(|text| { 346 .prop_flat_map(|x| {
370 (arb_edits(&text), Just(text)).prop_flat_map(|(edits, text)| { 347 let edited_text = x.edits.apply(&x.text);
371 let edited_text = edit_text(&text, edits.clone()); 348 let arb_offset = arb_offset(&edited_text);
372 let arb_offset = arb_offset(&edited_text); 349 (Just(x), Just(edited_text), arb_offset).prop_map(|(x, edited_text, offset)| {
373 (Just(text), Just(edited_text), Just(edits), arb_offset).prop_map( 350 ArbTextWithOffsetAndEdits {
374 |(text, edited_text, edits, offset)| ArbTextWithOffsetAndEdits { 351 text: x.text,
375 text, 352 edits: x.edits,
376 edits, 353 edited_text,
377 edited_text, 354 offset,
378 offset, 355 }
379 },
380 )
381 }) 356 })
382 }) 357 })
383 .boxed() 358 .boxed()
@@ -385,10 +360,10 @@ mod test {
385 360
386 proptest! { 361 proptest! {
387 #[test] 362 #[test]
388 fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { 363 fn test_translate_offset_with_edit(x in arb_text_with_edits_and_offset()) {
389 let expected = line_index::to_line_col(&x.edited_text, x.offset); 364 let expected = line_index::to_line_col(&x.edited_text, x.offset);
390 let line_index = LineIndex::new(&x.text); 365 let line_index = LineIndex::new(&x.text);
391 let actual = translate_offset_with_edit(&line_index, x.offset, &x.edits); 366 let actual = translate_offset_with_edit(&line_index, x.offset, x.edits.as_atoms());
392 367
393 assert_eq!(actual, expected); 368 assert_eq!(actual, expected);
394 } 369 }