diff options
author | Bernardo <[email protected]> | 2018-12-24 16:01:25 +0000 |
---|---|---|
committer | Bernardo <[email protected]> | 2018-12-25 19:14:27 +0000 |
commit | 863ed19946d6f707ce09dd77bf26b26be73e097c (patch) | |
tree | 32cecbe803073ef30127c764199678bf2d618601 /crates/ra_editor/src/line_index_utils.rs | |
parent | 6b2da4e5474ec064e44b7cf19523de1bab72ba27 (diff) |
remove benchmark and simplify tests
Diffstat (limited to 'crates/ra_editor/src/line_index_utils.rs')
-rw-r--r-- | crates/ra_editor/src/line_index_utils.rs | 63 |
1 files changed, 19 insertions, 44 deletions
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 @@ | |||
1 | use ra_text_edit::AtomTextEdit; | 1 | use ra_text_edit::AtomTextEdit; |
2 | use ra_syntax::{TextUnit, TextRange}; | 2 | use ra_syntax::{TextUnit, TextRange}; |
3 | use crate::{LineIndex, LineCol, line_index::{self, Utf16Char}}; | 3 | use crate::{LineIndex, LineCol, line_index::Utf16Char}; |
4 | use superslice::Ext; | 4 | use 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 | ||
329 | pub 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 | |||
338 | fn 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)] |
354 | mod test { | 329 | mod 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 | } |