From 3d98744c2a929c35852d20b5724ef5b6be1e3312 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Wed, 12 Dec 2018 18:51:43 +0100 Subject: [PATCH 01/18] proptest strategies for TextUnit and AtomTextEdit --- Cargo.lock | 67 +++++++++++++++ crates/ra_text_edit/Cargo.toml | 1 + crates/ra_text_edit/src/lib.rs | 2 + crates/ra_text_edit/src/test_utils.rs | 114 ++++++++++++++++++++++++++ 4 files changed, 184 insertions(+) create mode 100644 crates/ra_text_edit/src/test_utils.rs diff --git a/Cargo.lock b/Cargo.lock index 51cf1825d6b..3a37935818f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,6 +73,19 @@ dependencies = [ "safemem 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "bit-set" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bit-vec 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bit-vec" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "bitflags" version = "1.0.4" @@ -302,6 +315,11 @@ dependencies = [ "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "fnv" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "fst" version = "0.3.3" @@ -625,6 +643,28 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "proptest" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bit-set 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "rusty-fork 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "tempfile 3.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quick-error" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "quote" version = "0.6.10" @@ -763,6 +803,7 @@ dependencies = [ name = "ra_text_edit" version = "0.1.0" dependencies = [ + "proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)", "test_utils 0.1.0", "text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -983,6 +1024,17 @@ dependencies = [ "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rusty-fork" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "tempfile 3.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "wait-timeout 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ryu" version = "0.2.7" @@ -1412,6 +1464,14 @@ name = "void" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "wait-timeout" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "walkdir" version = "2.2.7" @@ -1459,6 +1519,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum backtrace 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)" = "b5b493b66e03090ebc4343eb02f94ff944e0cbc9ac6571491d170ba026741eb5" "checksum backtrace-sys 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)" = "3fcce89e5ad5c8949caa9434501f7b55415b3e7ad5270cb88c75a8d35e8f1279" "checksum base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643" +"checksum bit-set 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6f1efcc46c18245a69c38fcc5cc650f16d3a59d034f3106e9ed63748f695730a" +"checksum bit-vec 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4440d5cb623bb7390ae27fec0bb6c61111969860f8e3ae198bfa0663645e67cf" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" "checksum block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a076c298b9ecdb530ed9d967e74a6027d6a7478924520acddcddc24c1c8ab3ab" "checksum byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40" @@ -1487,6 +1549,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum failure_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "64c2d913fe8ed3b6c6518eedf4538255b989945c14c2a7d5cbff62a5e2120596" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" "checksum flexi_logger 0.10.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4dda06444ccc8b0a6da19d939989b4a4e83f328710ada449eedaed48c8b903cd" +"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" "checksum fst 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "db72126ca7dff566cdbbdd54af44668c544897d9d3862b198141f176f1238bdf" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" @@ -1527,6 +1590,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum pest_generator 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ebee4e9680be4fd162e6f3394ae4192a6b60b1e4d17d845e631f0c68d1a3386" "checksum pest_meta 2.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1f6d5f6f0e6082578c86af197d780dc38328e3f768cec06aac9bc46d714e8221" "checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09" +"checksum proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)" = "926d0604475349f463fe44130aae73f2294b5309ab2ca0310b998bd334ef191f" +"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" "checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c" "checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd" "checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c" @@ -1551,6 +1616,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rustc-demangle 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "01b90379b8664dd83460d59bdc5dd1fd3172b8913788db483ed1325171eab2f7" "checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +"checksum rusty-fork 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9591f190d2852720b679c21f66ad929f9f1d7bb09d1193c26167586029d8489c" "checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7" "checksum safemem 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dca453248a96cb0749e36ccdfe2b0b4e54a61bfef89fb97ec621eb8e0a93dd9" "checksum salsa 0.8.0 (git+https://github.com/matklad/salsa?branch=no-upgrade)" = "" @@ -1602,6 +1668,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum wait-timeout 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "b9f3bf741a801531993db6478b95682117471f76916f5e690dd8d45395b09349" "checksum walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d9d7ed3431229a144296213105a390676cc49c9b6a72bd19f3176c98e129fa1" "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" diff --git a/crates/ra_text_edit/Cargo.toml b/crates/ra_text_edit/Cargo.toml index 3c4157a4e72..67dfd0924dd 100644 --- a/crates/ra_text_edit/Cargo.toml +++ b/crates/ra_text_edit/Cargo.toml @@ -10,3 +10,4 @@ text_unit = "0.1.5" [dev-dependencies] test_utils = { path = "../test_utils" } +proptest = "0.8.7" diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs index 13e20f6fb05..0a407795e6f 100644 --- a/crates/ra_text_edit/src/lib.rs +++ b/crates/ra_text_edit/src/lib.rs @@ -1,5 +1,7 @@ mod text_edit; pub mod text_utils; +#[cfg(test)] +pub mod test_utils; pub use crate::text_edit::{TextEdit, TextEditBuilder}; diff --git a/crates/ra_text_edit/src/test_utils.rs b/crates/ra_text_edit/src/test_utils.rs new file mode 100644 index 00000000000..92b79ab5912 --- /dev/null +++ b/crates/ra_text_edit/src/test_utils.rs @@ -0,0 +1,114 @@ +use proptest::{prelude::*, proptest, proptest_helper}; +use text_unit::{TextUnit, TextRange}; +use crate::AtomTextEdit; + +pub fn arb_text() -> proptest::string::RegexGeneratorStrategy { + // generate multiple newlines + proptest::string::string_regex("(.*\n?)*").unwrap() +} + +fn text_offsets(text: &str) -> Vec { + text.char_indices() + .map(|(i, _)| TextUnit::from_usize(i)) + .collect() +} + +pub fn arb_offset(text: &str) -> BoxedStrategy { + let offsets = text_offsets(text); + // this is necessary to avoid "Uniform::new called with `low >= high`" panic + if offsets.is_empty() { + Just(TextUnit::from(0)).boxed() + } else { + prop::sample::select(offsets).boxed() + } +} + +pub fn arb_edits(text: &str) -> BoxedStrategy> { + let offsets = text_offsets(text); + let offsets_len = offsets.len(); + + if offsets_len == 0 { + return proptest::bool::ANY + .prop_flat_map(|b| { + // only valid edits + if b { + arb_text() + .prop_map(|text| vec![AtomTextEdit::insert(TextUnit::from(0), text)]) + .boxed() + } else { + Just(vec![]).boxed() + } + }) + .boxed(); + } + + proptest::sample::subsequence(offsets, 0..offsets_len) + .prop_flat_map(|xs| { + let strategies: Vec<_> = xs + .chunks(2) + .map(|chunk| match chunk { + &[from, to] => { + let range = TextRange::from_to(from, to); + (proptest::bool::ANY) + .prop_flat_map(move |b| { + if b { + Just(AtomTextEdit::delete(range)).boxed() + } else { + arb_text() + .prop_map(move |text| AtomTextEdit::replace(range, text)) + .boxed() + } + }) + .boxed() + } + &[x] => arb_text() + .prop_map(move |text| AtomTextEdit::insert(x, text)) + .boxed(), + _ => unreachable!(), + }) + .collect(); + strategies + }) + .boxed() +} + +fn arb_text_with_edits() -> BoxedStrategy<(String, Vec)> { + let text = arb_text(); + text.prop_flat_map(|s| { + let edits = arb_edits(&s); + (Just(s), edits) + }) + .boxed() +} + +fn intersect(r1: TextRange, r2: TextRange) -> Option { + let start = r1.start().max(r2.start()); + let end = r1.end().min(r2.end()); + if start <= end { + Some(TextRange::from_to(start, end)) + } else { + None + } +} + +proptest! { +#[test] + fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) { + proptest_atom_text_edits_are_valid(text, edits) + } +} + +fn proptest_atom_text_edits_are_valid(text: String, edits: Vec) { + // slicing doesn't panic + for e in &edits { + let _ = &text[e.delete]; + } + // ranges do not overlap + for (i1, e1) in edits.iter().skip(1).enumerate() { + for e2 in &edits[0..i1] { + if intersect(e1.delete, e2.delete).is_some() { + assert!(false, "Overlapping ranges {} {}", e1.delete, e2.delete); + } + } + } +} From dee426e1b120cd12fc608df77f977d4800b6655d Mon Sep 17 00:00:00 2001 From: Bernardo Date: Wed, 12 Dec 2018 19:25:50 +0100 Subject: [PATCH 02/18] simplify and reduce number of values explored --- crates/ra_text_edit/src/test_utils.rs | 52 ++++++++++++--------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/crates/ra_text_edit/src/test_utils.rs b/crates/ra_text_edit/src/test_utils.rs index 92b79ab5912..29301bff354 100644 --- a/crates/ra_text_edit/src/test_utils.rs +++ b/crates/ra_text_edit/src/test_utils.rs @@ -24,41 +24,35 @@ pub fn arb_offset(text: &str) -> BoxedStrategy { } pub fn arb_edits(text: &str) -> BoxedStrategy> { - let offsets = text_offsets(text); - let offsets_len = offsets.len(); - - if offsets_len == 0 { - return proptest::bool::ANY - .prop_flat_map(|b| { - // only valid edits - if b { - arb_text() - .prop_map(|text| vec![AtomTextEdit::insert(TextUnit::from(0), text)]) - .boxed() - } else { - Just(vec![]).boxed() - } - }) + if text.is_empty() { + // only valid edits + return Just(vec![]) + .boxed() + .prop_union( + arb_text() + .prop_map(|text| vec![AtomTextEdit::insert(TextUnit::from(0), text)]) + .boxed(), + ) .boxed(); } - proptest::sample::subsequence(offsets, 0..offsets_len) - .prop_flat_map(|xs| { - let strategies: Vec<_> = xs + let offsets = text_offsets(text); + let max_cuts = offsets.len().min(7); + + proptest::sample::subsequence(offsets, 0..max_cuts) + .prop_flat_map(|cuts| { + let strategies: Vec<_> = cuts .chunks(2) .map(|chunk| match chunk { &[from, to] => { let range = TextRange::from_to(from, to); - (proptest::bool::ANY) - .prop_flat_map(move |b| { - if b { - Just(AtomTextEdit::delete(range)).boxed() - } else { - arb_text() - .prop_map(move |text| AtomTextEdit::replace(range, text)) - .boxed() - } - }) + Just(AtomTextEdit::delete(range)) + .boxed() + .prop_union( + arb_text() + .prop_map(move |text| AtomTextEdit::replace(range, text)) + .boxed(), + ) .boxed() } &[x] => arb_text() @@ -92,7 +86,7 @@ fn intersect(r1: TextRange, r2: TextRange) -> Option { } proptest! { -#[test] + #[test] fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) { proptest_atom_text_edits_are_valid(text, edits) } From d9519791590170fd77aa45cc9a802539eaf16efc Mon Sep 17 00:00:00 2001 From: Bernardo Date: Fri, 14 Dec 2018 18:00:35 +0100 Subject: [PATCH 03/18] test translate_offset_with_edit against simple impl for single edits --- Cargo.lock | 1 + crates/ra_lsp_server/Cargo.toml | 4 +- crates/ra_lsp_server/src/conv.rs | 61 +++++++++++++++++++++++ crates/ra_text_edit/Cargo.toml | 2 +- crates/ra_text_edit/src/lib.rs | 1 - crates/ra_text_edit/src/test_utils.rs | 70 +++++++++++++++------------ 6 files changed, 104 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3a37935818f..a63eac57a13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -763,6 +763,7 @@ dependencies = [ "languageserver-types 0.53.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)", "ra_analysis 0.1.0", "ra_editor 0.1.0", "ra_syntax 0.1.0", diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml index 3c8c240cd80..d73ff158f97 100644 --- a/crates/ra_lsp_server/Cargo.toml +++ b/crates/ra_lsp_server/Cargo.toml @@ -34,6 +34,8 @@ ra_analysis = { path = "../ra_analysis" } gen_lsp_server = { path = "../gen_lsp_server" } ra_vfs = { path = "../ra_vfs" } +proptest = "0.8.7" + [dev-dependencies] tempdir = "0.3.7" -test_utils = { path = "../test_utils" } +test_utils = { path = "../test_utils" } \ No newline at end of file diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 051f1f995b3..1db4991759c 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -386,3 +386,64 @@ where self.iter.next().map(|item| item.conv_with(self.ctx)) } } + +#[cfg(test)] +mod test { + use proptest::{prelude::*, proptest, proptest_helper}; + use super::*; + use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; + + #[derive(Debug)] + struct ArbTextWithOffsetAndEdits { + text: String, + offset: TextUnit, + edits: Vec, + } + + fn arb_text_with_offset_and_edits() -> BoxedStrategy { + arb_text() + .prop_flat_map(|text| { + (arb_offset(&text), arb_edits(&text), Just(text)).prop_map( + |(offset, edits, text)| ArbTextWithOffsetAndEdits { + text, + offset, + edits, + }, + ) + }) + .boxed() + } + + fn translate_offset_with_edit_naive( + pre_edit_text: String, + offset: TextUnit, + edits: &[AtomTextEdit], + ) -> LineCol { + // apply edits ordered from last to first + // since they should not overlap we can just use start() + let mut edits: Vec = edits.to_vec(); + edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize)); + + let mut text = pre_edit_text; + + for edit in &edits { + let range = edit.delete.start().to_usize()..edit.delete.end().to_usize(); + text.replace_range(range, &edit.insert); + } + + let line_index = LineIndex::new(&text); + + line_index.line_col(offset) + } + + proptest! { + #[test] + fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { + if x.edits.len() <= 1 { + let expected = translate_offset_with_edit_naive(x.text.clone(), x.offset, &x.edits); + let actual = translate_offset_with_edit(&LineIndex::new(&x.text), x.offset, &x.edits); + assert_eq!(actual, expected); + } + } + } +} diff --git a/crates/ra_text_edit/Cargo.toml b/crates/ra_text_edit/Cargo.toml index 67dfd0924dd..e0db49688e1 100644 --- a/crates/ra_text_edit/Cargo.toml +++ b/crates/ra_text_edit/Cargo.toml @@ -7,7 +7,7 @@ publish = false [dependencies] text_unit = "0.1.5" +proptest = "0.8.7" [dev-dependencies] test_utils = { path = "../test_utils" } -proptest = "0.8.7" diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs index 0a407795e6f..89600413ad2 100644 --- a/crates/ra_text_edit/src/lib.rs +++ b/crates/ra_text_edit/src/lib.rs @@ -1,6 +1,5 @@ mod text_edit; pub mod text_utils; -#[cfg(test)] pub mod test_utils; pub use crate::text_edit::{TextEdit, TextEditBuilder}; diff --git a/crates/ra_text_edit/src/test_utils.rs b/crates/ra_text_edit/src/test_utils.rs index 29301bff354..4a0ebc08ecf 100644 --- a/crates/ra_text_edit/src/test_utils.rs +++ b/crates/ra_text_edit/src/test_utils.rs @@ -1,4 +1,4 @@ -use proptest::{prelude::*, proptest, proptest_helper}; +use proptest::prelude::*; use text_unit::{TextUnit, TextRange}; use crate::AtomTextEdit; @@ -66,42 +66,48 @@ pub fn arb_edits(text: &str) -> BoxedStrategy> { .boxed() } -fn arb_text_with_edits() -> BoxedStrategy<(String, Vec)> { - let text = arb_text(); - text.prop_flat_map(|s| { - let edits = arb_edits(&s); - (Just(s), edits) - }) - .boxed() -} +#[cfg(test)] +mod tests { + use super::*; + use proptest::{proptest, proptest_helper}; -fn intersect(r1: TextRange, r2: TextRange) -> Option { - let start = r1.start().max(r2.start()); - let end = r1.end().min(r2.end()); - if start <= end { - Some(TextRange::from_to(start, end)) - } else { - None + fn arb_text_with_edits() -> BoxedStrategy<(String, Vec)> { + let text = arb_text(); + text.prop_flat_map(|s| { + let edits = arb_edits(&s); + (Just(s), edits) + }) + .boxed() } -} -proptest! { - #[test] - fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) { - proptest_atom_text_edits_are_valid(text, edits) + fn intersect(r1: TextRange, r2: TextRange) -> Option { + let start = r1.start().max(r2.start()); + let end = r1.end().min(r2.end()); + if start <= end { + Some(TextRange::from_to(start, end)) + } else { + None + } + } + proptest! { + #[test] + fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) { + proptest_atom_text_edits_are_valid(text, edits) + } } -} -fn proptest_atom_text_edits_are_valid(text: String, edits: Vec) { - // slicing doesn't panic - for e in &edits { - let _ = &text[e.delete]; - } - // ranges do not overlap - for (i1, e1) in edits.iter().skip(1).enumerate() { - for e2 in &edits[0..i1] { - if intersect(e1.delete, e2.delete).is_some() { - assert!(false, "Overlapping ranges {} {}", e1.delete, e2.delete); + fn proptest_atom_text_edits_are_valid(text: String, edits: Vec) { + // slicing doesn't panic + for e in &edits { + let _ = &text[e.delete]; + } + // ranges do not overlap + for i in 1..edits.len() { + let e1 = &edits[i]; + for e2 in &edits[0..i] { + if intersect(e1.delete, e2.delete).is_some() { + assert!(false, "Overlapping ranges {} {}", e1.delete, e2.delete); + } } } } From 881c29192d39f657bf518baf399c47a5bfdc922f Mon Sep 17 00:00:00 2001 From: Bernardo Date: Mon, 17 Dec 2018 20:13:54 +0100 Subject: [PATCH 04/18] initial newline translation working todo: cleanup, simplify handle columns --- crates/ra_editor/src/line_index.rs | 6 +- crates/ra_lsp_server/src/conv.rs | 270 +++++++++++++++++++++++++++-- 2 files changed, 262 insertions(+), 14 deletions(-) diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index aab7e4081c6..7eddfd50252 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -1,4 +1,4 @@ -use crate::TextUnit; +use crate::{TextUnit, TextRange}; use rustc_hash::FxHashMap; use superslice::Ext; @@ -120,6 +120,10 @@ impl LineIndex { col } + + pub fn newlines(&self) -> &[TextUnit] { + &self.newlines[1..] + } } #[test] diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 1db4991759c..6d0ebbcd965 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -296,6 +296,202 @@ fn translate_offset_with_edit( } } +#[derive(Debug)] +struct OffsetNewlineIter<'a> { + text: &'a str, + offset: TextUnit, +} + +impl<'a> Iterator for OffsetNewlineIter<'a> { + type Item = TextUnit; + fn next(&mut self) -> Option { + let next_idx = self + .text + .char_indices() + .filter_map(|(i, c)| if c == '\n' { Some(i + 1) } else { None }) + .next()?; + let next = self.offset + TextUnit::from_usize(next_idx); + self.text = &self.text[next_idx..]; + self.offset = next; + Some(next) + } +} + +#[derive(Debug, Clone, Copy, PartialEq)] +enum TranslatedPos { + Before, + After, +} + +/// None means it was deleted +type TranslatedOffset = Option<(TranslatedPos, TextUnit)>; + +fn translate_offset(offset: TextUnit, edit: &TranslatedAtomEdit) -> TranslatedOffset { + if offset <= edit.delete.start() { + Some((TranslatedPos::Before, offset)) + } else if offset <= edit.delete.end() { + None + } else { + let diff = edit.insert.len() as i64 - edit.delete.len().to_usize() as i64; + let after = TextUnit::from((offset.to_usize() as i64 + diff) as u32); + Some((TranslatedPos::After, after)) + } +} + +trait TranslatedNewlineIterator { + fn translate(&self, offset: TextUnit) -> TextUnit; + fn translate_range(&self, range: TextRange) -> TextRange { + TextRange::from_to(self.translate(range.start()), self.translate(range.end())) + } + fn next_translated(&mut self) -> Option; + fn boxed<'a>(self) -> Box + where + Self: 'a + Sized, + { + Box::new(self) + } +} + +struct TranslatedAtomEdit<'a> { + delete: TextRange, + insert: &'a str, +} + +struct TranslatedNewlines<'a, T: TranslatedNewlineIterator> { + inner: T, + next_inner: Option, + edit: TranslatedAtomEdit<'a>, + insert: OffsetNewlineIter<'a>, +} + +impl<'a, T: TranslatedNewlineIterator> TranslatedNewlines<'a, T> { + fn from(inner: T, edit: &'a AtomTextEdit) -> Self { + let delete = inner.translate_range(edit.delete); + let mut res = TranslatedNewlines { + inner, + next_inner: None, + edit: TranslatedAtomEdit { + delete, + insert: &edit.insert, + }, + insert: OffsetNewlineIter { + offset: delete.start(), + text: &edit.insert, + }, + }; + // prepare next_inner + res.advance_inner(); + res + } + + fn advance_inner(&mut self) { + self.next_inner = self + .inner + .next_translated() + .map(|x| translate_offset(x, &self.edit)); + } +} + +impl<'a, T: TranslatedNewlineIterator> TranslatedNewlineIterator for TranslatedNewlines<'a, T> { + fn translate(&self, offset: TextUnit) -> TextUnit { + let offset = self.inner.translate(offset); + let (_, offset) = + translate_offset(offset, &self.edit).expect("translate_unit returned None"); + offset + } + + fn next_translated(&mut self) -> Option { + match self.next_inner { + None => self.insert.next(), + Some(next) => match next { + None => self.insert.next().or_else(|| { + self.advance_inner(); + self.next_translated() + }), + Some((TranslatedPos::Before, next)) => { + self.advance_inner(); + Some(next) + } + Some((TranslatedPos::After, next)) => self.insert.next().or_else(|| { + self.advance_inner(); + Some(next) + }), + }, + } + } +} + +impl<'a> Iterator for Box { + type Item = TextUnit; + fn next(&mut self) -> Option { + self.next_translated() + } +} + +impl TranslatedNewlineIterator for Box { + fn translate(&self, offset: TextUnit) -> TextUnit { + self.as_ref().translate(offset) + } + fn next_translated(&mut self) -> Option { + self.as_mut().next_translated() + } +} + +struct IteratorWrapper>(T); + +impl> TranslatedNewlineIterator for IteratorWrapper { + fn translate(&self, offset: TextUnit) -> TextUnit { + offset + } + fn next_translated(&mut self) -> Option { + self.0.next() + } +} + +impl> Iterator for IteratorWrapper { + type Item = TextUnit; + fn next(&mut self) -> Option { + self.0.next() + } +} + +fn translate_newlines<'a>( + mut newlines: Box, + edits: &'a [AtomTextEdit], +) -> Box { + for edit in edits { + newlines = TranslatedNewlines::from(newlines, edit).boxed(); + } + newlines +} + +#[allow(dead_code)] +fn translate_offset_with_edit_fast( + pre_edit_index: &LineIndex, + offset: TextUnit, + edits: &[AtomTextEdit], +) -> LineCol { + // println!("{:?}", pre_edit_index.newlines()); + let mut newlines: Box = Box::new(IteratorWrapper( + pre_edit_index.newlines().iter().map(|x| *x), + )); + + newlines = translate_newlines(newlines, edits); + + let mut line = 0; + for n in newlines { + if n > offset { + break; + } + line += 1; + } + + LineCol { + line: line, + col_utf16: 0, + } +} + impl TryConvWith for SourceFileEdit { type Ctx = ServerWorld; type Output = TextDocumentEdit; @@ -414,36 +610,84 @@ mod test { .boxed() } - fn translate_offset_with_edit_naive( - pre_edit_text: String, - offset: TextUnit, - edits: &[AtomTextEdit], - ) -> LineCol { + fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { // apply edits ordered from last to first // since they should not overlap we can just use start() - let mut edits: Vec = edits.to_vec(); edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize)); - let mut text = pre_edit_text; + let mut text = pre_edit_text.to_owned(); for edit in &edits { let range = edit.delete.start().to_usize()..edit.delete.end().to_usize(); text.replace_range(range, &edit.insert); } - let line_index = LineIndex::new(&text); + text + } + fn translate_after_edit( + pre_edit_text: &str, + offset: TextUnit, + edits: Vec, + ) -> LineCol { + let text = edit_text(pre_edit_text, edits); + let line_index = LineIndex::new(&text); line_index.line_col(offset) } proptest! { #[test] fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { - if x.edits.len() <= 1 { - let expected = translate_offset_with_edit_naive(x.text.clone(), x.offset, &x.edits); - let actual = translate_offset_with_edit(&LineIndex::new(&x.text), x.offset, &x.edits); - assert_eq!(actual, expected); - } + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual = translate_offset_with_edit_fast(&line_index, x.offset, &x.edits); + assert_eq!(actual.line, expected.line); } } + + #[test] + fn test_translate_offset_with_edit_1() { + let x = ArbTextWithOffsetAndEdits { + text: "jbnan".to_owned(), + offset: 3.into(), + edits: vec![ + AtomTextEdit::delete(TextRange::from_to(1.into(), 3.into())), + AtomTextEdit::insert(4.into(), "\n".into()), + ], + }; + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual = translate_offset_with_edit_fast(&line_index, x.offset, &x.edits); + // assert_eq!(actual, expected); + assert_eq!(actual.line, expected.line); + } + + #[test] + fn test_translate_offset_with_edit_2() { + let x = ArbTextWithOffsetAndEdits { + text: "aa\n".to_owned(), + offset: 1.into(), + edits: vec![AtomTextEdit::delete(TextRange::from_to(0.into(), 2.into()))], + }; + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual = translate_offset_with_edit_fast(&line_index, x.offset, &x.edits); + // assert_eq!(actual, expected); + assert_eq!(actual.line, expected.line); + } + + #[test] + fn test_translate_offset_with_edit_3() { + let x = ArbTextWithOffsetAndEdits { + text: "".to_owned(), + offset: 0.into(), + edits: vec![AtomTextEdit::insert(0.into(), "\n".into())], + }; + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual = translate_offset_with_edit_fast(&line_index, x.offset, &x.edits); + // assert_eq!(actual, expected); + assert_eq!(actual.line, expected.line); + } + } From 8c9df62c1c6a778a8df9ea028d1dce98c91c4d9d Mon Sep 17 00:00:00 2001 From: Bernardo Date: Tue, 18 Dec 2018 18:46:54 +0100 Subject: [PATCH 05/18] move translate_offset_with_edit to ra_editor --- Cargo.lock | 2 +- crates/ra_editor/Cargo.toml | 2 + crates/ra_editor/src/lib.rs | 2 + crates/ra_editor/src/line_index.rs | 4 +- crates/ra_editor/src/line_index_utils.rs | 260 +++++++++++++++++++ crates/ra_lsp_server/Cargo.toml | 4 +- crates/ra_lsp_server/src/conv.rs | 305 ----------------------- 7 files changed, 268 insertions(+), 311 deletions(-) create mode 100644 crates/ra_editor/src/line_index_utils.rs diff --git a/Cargo.lock b/Cargo.lock index a63eac57a13..127d7772a71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -723,6 +723,7 @@ version = "0.1.0" dependencies = [ "itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", "join_to_string 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)", "ra_syntax 0.1.0", "ra_text_edit 0.1.0", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -763,7 +764,6 @@ dependencies = [ "languageserver-types 0.53.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", - "proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)", "ra_analysis 0.1.0", "ra_editor 0.1.0", "ra_syntax 0.1.0", diff --git a/crates/ra_editor/Cargo.toml b/crates/ra_editor/Cargo.toml index c29be13509a..1ad99af2806 100644 --- a/crates/ra_editor/Cargo.toml +++ b/crates/ra_editor/Cargo.toml @@ -14,5 +14,7 @@ rustc-hash = "1.0" ra_syntax = { path = "../ra_syntax" } ra_text_edit = { path = "../ra_text_edit" } +proptest = "0.8.7" + [dev-dependencies] test_utils = { path = "../test_utils" } diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 7a689b0f25e..619497f0b4b 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -2,6 +2,7 @@ mod code_actions; mod extend_selection; mod folding_ranges; mod line_index; +mod line_index_utils; mod symbols; #[cfg(test)] mod test_utils; @@ -12,6 +13,7 @@ pub use self::{ extend_selection::extend_selection, folding_ranges::{folding_ranges, Fold, FoldKind}, line_index::{LineCol, LineIndex}, + line_index_utils::translate_offset_with_edit, symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, typing::{join_lines, on_enter, on_eq_typed}, }; diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index 7eddfd50252..7d9b8d79fe7 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -1,4 +1,4 @@ -use crate::{TextUnit, TextRange}; +use crate::TextUnit; use rustc_hash::FxHashMap; use superslice::Ext; @@ -121,7 +121,7 @@ impl LineIndex { col } - pub fn newlines(&self) -> &[TextUnit] { + pub(crate) fn newlines(&self) -> &[TextUnit] { &self.newlines[1..] } } diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs new file mode 100644 index 00000000000..e4fad04b2c3 --- /dev/null +++ b/crates/ra_editor/src/line_index_utils.rs @@ -0,0 +1,260 @@ +use ra_text_edit::{AtomTextEdit}; +use ra_syntax::{TextUnit, TextRange}; +use crate::{LineIndex, LineCol}; + +#[derive(Debug)] +struct OffsetNewlineIter<'a> { + text: &'a str, + offset: TextUnit, +} + +impl<'a> Iterator for OffsetNewlineIter<'a> { + type Item = TextUnit; + fn next(&mut self) -> Option { + let next_idx = self + .text + .char_indices() + .filter_map(|(i, c)| if c == '\n' { Some(i + 1) } else { None }) + .next()?; + let next = self.offset + TextUnit::from_usize(next_idx); + self.text = &self.text[next_idx..]; + self.offset = next; + Some(next) + } +} + +#[derive(Debug, Clone, Copy, PartialEq)] +enum TranslatedPos { + Before, + After, +} + +/// None means it was deleted +type TranslatedOffset = Option<(TranslatedPos, TextUnit)>; + +fn translate_offset(offset: TextUnit, edit: &TranslatedAtomEdit) -> TranslatedOffset { + if offset <= edit.delete.start() { + Some((TranslatedPos::Before, offset)) + } else if offset <= edit.delete.end() { + None + } else { + let diff = edit.insert.len() as i64 - edit.delete.len().to_usize() as i64; + let after = TextUnit::from((offset.to_usize() as i64 + diff) as u32); + Some((TranslatedPos::After, after)) + } +} + +trait TranslatedNewlineIterator { + fn translate(&self, offset: TextUnit) -> TextUnit; + fn translate_range(&self, range: TextRange) -> TextRange { + TextRange::from_to(self.translate(range.start()), self.translate(range.end())) + } + fn next_translated(&mut self) -> Option; + fn boxed<'a>(self) -> Box + where + Self: 'a + Sized, + { + Box::new(self) + } +} + +struct TranslatedAtomEdit<'a> { + delete: TextRange, + insert: &'a str, +} + +struct TranslatedNewlines<'a, T: TranslatedNewlineIterator> { + inner: T, + next_inner: Option, + edit: TranslatedAtomEdit<'a>, + insert: OffsetNewlineIter<'a>, +} + +impl<'a, T: TranslatedNewlineIterator> TranslatedNewlines<'a, T> { + fn from(inner: T, edit: &'a AtomTextEdit) -> Self { + let delete = inner.translate_range(edit.delete); + let mut res = TranslatedNewlines { + inner, + next_inner: None, + edit: TranslatedAtomEdit { + delete, + insert: &edit.insert, + }, + insert: OffsetNewlineIter { + offset: delete.start(), + text: &edit.insert, + }, + }; + // prepare next_inner + res.advance_inner(); + res + } + + fn advance_inner(&mut self) { + self.next_inner = self + .inner + .next_translated() + .map(|x| translate_offset(x, &self.edit)); + } +} + +impl<'a, T: TranslatedNewlineIterator> TranslatedNewlineIterator for TranslatedNewlines<'a, T> { + fn translate(&self, offset: TextUnit) -> TextUnit { + let offset = self.inner.translate(offset); + let (_, offset) = + translate_offset(offset, &self.edit).expect("translate_unit returned None"); + offset + } + + fn next_translated(&mut self) -> Option { + match self.next_inner { + None => self.insert.next(), + Some(next) => match next { + None => self.insert.next().or_else(|| { + self.advance_inner(); + self.next_translated() + }), + Some((TranslatedPos::Before, next)) => { + self.advance_inner(); + Some(next) + } + Some((TranslatedPos::After, next)) => self.insert.next().or_else(|| { + self.advance_inner(); + Some(next) + }), + }, + } + } +} + +impl<'a> Iterator for Box { + type Item = TextUnit; + fn next(&mut self) -> Option { + self.next_translated() + } +} + +impl TranslatedNewlineIterator for Box { + fn translate(&self, offset: TextUnit) -> TextUnit { + self.as_ref().translate(offset) + } + fn next_translated(&mut self) -> Option { + self.as_mut().next_translated() + } +} + +struct IteratorWrapper>(T); + +impl> TranslatedNewlineIterator for IteratorWrapper { + fn translate(&self, offset: TextUnit) -> TextUnit { + offset + } + fn next_translated(&mut self) -> Option { + self.0.next() + } +} + +impl> Iterator for IteratorWrapper { + type Item = TextUnit; + fn next(&mut self) -> Option { + self.0.next() + } +} + +fn translate_newlines<'a>( + mut newlines: Box, + edits: &'a [AtomTextEdit], +) -> Box { + for edit in edits { + newlines = TranslatedNewlines::from(newlines, edit).boxed(); + } + newlines +} + +pub fn translate_offset_with_edit( + pre_edit_index: &LineIndex, + offset: TextUnit, + edits: &[AtomTextEdit], +) -> LineCol { + let mut newlines: Box = Box::new(IteratorWrapper( + pre_edit_index.newlines().iter().map(|x| *x), + )); + + newlines = translate_newlines(newlines, edits); + + let mut line = 0; + for n in newlines { + if n > offset { + break; + } + line += 1; + } + + LineCol { + line: line, + col_utf16: 0, // TODO not implemented yet + } +} + +#[cfg(test)] +mod test { + use proptest::{prelude::*, proptest, proptest_helper}; + use super::*; + use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; + + #[derive(Debug)] + struct ArbTextWithOffsetAndEdits { + text: String, + offset: TextUnit, + edits: Vec, + } + + fn arb_text_with_offset_and_edits() -> BoxedStrategy { + arb_text() + .prop_flat_map(|text| { + (arb_offset(&text), arb_edits(&text), Just(text)).prop_map( + |(offset, edits, text)| ArbTextWithOffsetAndEdits { + text, + offset, + edits, + }, + ) + }) + .boxed() + } + + fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { + // apply edits ordered from last to first + // since they should not overlap we can just use start() + edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize)); + + let mut text = pre_edit_text.to_owned(); + + for edit in &edits { + let range = edit.delete.start().to_usize()..edit.delete.end().to_usize(); + text.replace_range(range, &edit.insert); + } + + text + } + + fn translate_after_edit( + pre_edit_text: &str, + offset: TextUnit, + edits: Vec, + ) -> LineCol { + let text = edit_text(pre_edit_text, edits); + let line_index = LineIndex::new(&text); + line_index.line_col(offset) + } + + proptest! { + #[test] + fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual = translate_offset_with_edit(&line_index, x.offset, &x.edits); + assert_eq!(actual.line, expected.line); + } + } +} diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml index d73ff158f97..3c8c240cd80 100644 --- a/crates/ra_lsp_server/Cargo.toml +++ b/crates/ra_lsp_server/Cargo.toml @@ -34,8 +34,6 @@ ra_analysis = { path = "../ra_analysis" } gen_lsp_server = { path = "../gen_lsp_server" } ra_vfs = { path = "../ra_vfs" } -proptest = "0.8.7" - [dev-dependencies] tempdir = "0.3.7" -test_utils = { path = "../test_utils" } \ No newline at end of file +test_utils = { path = "../test_utils" } diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 6d0ebbcd965..051f1f995b3 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -296,202 +296,6 @@ fn translate_offset_with_edit( } } -#[derive(Debug)] -struct OffsetNewlineIter<'a> { - text: &'a str, - offset: TextUnit, -} - -impl<'a> Iterator for OffsetNewlineIter<'a> { - type Item = TextUnit; - fn next(&mut self) -> Option { - let next_idx = self - .text - .char_indices() - .filter_map(|(i, c)| if c == '\n' { Some(i + 1) } else { None }) - .next()?; - let next = self.offset + TextUnit::from_usize(next_idx); - self.text = &self.text[next_idx..]; - self.offset = next; - Some(next) - } -} - -#[derive(Debug, Clone, Copy, PartialEq)] -enum TranslatedPos { - Before, - After, -} - -/// None means it was deleted -type TranslatedOffset = Option<(TranslatedPos, TextUnit)>; - -fn translate_offset(offset: TextUnit, edit: &TranslatedAtomEdit) -> TranslatedOffset { - if offset <= edit.delete.start() { - Some((TranslatedPos::Before, offset)) - } else if offset <= edit.delete.end() { - None - } else { - let diff = edit.insert.len() as i64 - edit.delete.len().to_usize() as i64; - let after = TextUnit::from((offset.to_usize() as i64 + diff) as u32); - Some((TranslatedPos::After, after)) - } -} - -trait TranslatedNewlineIterator { - fn translate(&self, offset: TextUnit) -> TextUnit; - fn translate_range(&self, range: TextRange) -> TextRange { - TextRange::from_to(self.translate(range.start()), self.translate(range.end())) - } - fn next_translated(&mut self) -> Option; - fn boxed<'a>(self) -> Box - where - Self: 'a + Sized, - { - Box::new(self) - } -} - -struct TranslatedAtomEdit<'a> { - delete: TextRange, - insert: &'a str, -} - -struct TranslatedNewlines<'a, T: TranslatedNewlineIterator> { - inner: T, - next_inner: Option, - edit: TranslatedAtomEdit<'a>, - insert: OffsetNewlineIter<'a>, -} - -impl<'a, T: TranslatedNewlineIterator> TranslatedNewlines<'a, T> { - fn from(inner: T, edit: &'a AtomTextEdit) -> Self { - let delete = inner.translate_range(edit.delete); - let mut res = TranslatedNewlines { - inner, - next_inner: None, - edit: TranslatedAtomEdit { - delete, - insert: &edit.insert, - }, - insert: OffsetNewlineIter { - offset: delete.start(), - text: &edit.insert, - }, - }; - // prepare next_inner - res.advance_inner(); - res - } - - fn advance_inner(&mut self) { - self.next_inner = self - .inner - .next_translated() - .map(|x| translate_offset(x, &self.edit)); - } -} - -impl<'a, T: TranslatedNewlineIterator> TranslatedNewlineIterator for TranslatedNewlines<'a, T> { - fn translate(&self, offset: TextUnit) -> TextUnit { - let offset = self.inner.translate(offset); - let (_, offset) = - translate_offset(offset, &self.edit).expect("translate_unit returned None"); - offset - } - - fn next_translated(&mut self) -> Option { - match self.next_inner { - None => self.insert.next(), - Some(next) => match next { - None => self.insert.next().or_else(|| { - self.advance_inner(); - self.next_translated() - }), - Some((TranslatedPos::Before, next)) => { - self.advance_inner(); - Some(next) - } - Some((TranslatedPos::After, next)) => self.insert.next().or_else(|| { - self.advance_inner(); - Some(next) - }), - }, - } - } -} - -impl<'a> Iterator for Box { - type Item = TextUnit; - fn next(&mut self) -> Option { - self.next_translated() - } -} - -impl TranslatedNewlineIterator for Box { - fn translate(&self, offset: TextUnit) -> TextUnit { - self.as_ref().translate(offset) - } - fn next_translated(&mut self) -> Option { - self.as_mut().next_translated() - } -} - -struct IteratorWrapper>(T); - -impl> TranslatedNewlineIterator for IteratorWrapper { - fn translate(&self, offset: TextUnit) -> TextUnit { - offset - } - fn next_translated(&mut self) -> Option { - self.0.next() - } -} - -impl> Iterator for IteratorWrapper { - type Item = TextUnit; - fn next(&mut self) -> Option { - self.0.next() - } -} - -fn translate_newlines<'a>( - mut newlines: Box, - edits: &'a [AtomTextEdit], -) -> Box { - for edit in edits { - newlines = TranslatedNewlines::from(newlines, edit).boxed(); - } - newlines -} - -#[allow(dead_code)] -fn translate_offset_with_edit_fast( - pre_edit_index: &LineIndex, - offset: TextUnit, - edits: &[AtomTextEdit], -) -> LineCol { - // println!("{:?}", pre_edit_index.newlines()); - let mut newlines: Box = Box::new(IteratorWrapper( - pre_edit_index.newlines().iter().map(|x| *x), - )); - - newlines = translate_newlines(newlines, edits); - - let mut line = 0; - for n in newlines { - if n > offset { - break; - } - line += 1; - } - - LineCol { - line: line, - col_utf16: 0, - } -} - impl TryConvWith for SourceFileEdit { type Ctx = ServerWorld; type Output = TextDocumentEdit; @@ -582,112 +386,3 @@ where self.iter.next().map(|item| item.conv_with(self.ctx)) } } - -#[cfg(test)] -mod test { - use proptest::{prelude::*, proptest, proptest_helper}; - use super::*; - use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; - - #[derive(Debug)] - struct ArbTextWithOffsetAndEdits { - text: String, - offset: TextUnit, - edits: Vec, - } - - fn arb_text_with_offset_and_edits() -> BoxedStrategy { - arb_text() - .prop_flat_map(|text| { - (arb_offset(&text), arb_edits(&text), Just(text)).prop_map( - |(offset, edits, text)| ArbTextWithOffsetAndEdits { - text, - offset, - edits, - }, - ) - }) - .boxed() - } - - fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { - // apply edits ordered from last to first - // since they should not overlap we can just use start() - edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize)); - - let mut text = pre_edit_text.to_owned(); - - for edit in &edits { - let range = edit.delete.start().to_usize()..edit.delete.end().to_usize(); - text.replace_range(range, &edit.insert); - } - - text - } - - fn translate_after_edit( - pre_edit_text: &str, - offset: TextUnit, - edits: Vec, - ) -> LineCol { - let text = edit_text(pre_edit_text, edits); - let line_index = LineIndex::new(&text); - line_index.line_col(offset) - } - - proptest! { - #[test] - fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { - let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual = translate_offset_with_edit_fast(&line_index, x.offset, &x.edits); - assert_eq!(actual.line, expected.line); - } - } - - #[test] - fn test_translate_offset_with_edit_1() { - let x = ArbTextWithOffsetAndEdits { - text: "jbnan".to_owned(), - offset: 3.into(), - edits: vec![ - AtomTextEdit::delete(TextRange::from_to(1.into(), 3.into())), - AtomTextEdit::insert(4.into(), "\n".into()), - ], - }; - let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual = translate_offset_with_edit_fast(&line_index, x.offset, &x.edits); - // assert_eq!(actual, expected); - assert_eq!(actual.line, expected.line); - } - - #[test] - fn test_translate_offset_with_edit_2() { - let x = ArbTextWithOffsetAndEdits { - text: "aa\n".to_owned(), - offset: 1.into(), - edits: vec![AtomTextEdit::delete(TextRange::from_to(0.into(), 2.into()))], - }; - let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual = translate_offset_with_edit_fast(&line_index, x.offset, &x.edits); - // assert_eq!(actual, expected); - assert_eq!(actual.line, expected.line); - } - - #[test] - fn test_translate_offset_with_edit_3() { - let x = ArbTextWithOffsetAndEdits { - text: "".to_owned(), - offset: 0.into(), - edits: vec![AtomTextEdit::insert(0.into(), "\n".into())], - }; - let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual = translate_offset_with_edit_fast(&line_index, x.offset, &x.edits); - // assert_eq!(actual, expected); - assert_eq!(actual.line, expected.line); - } - -} From 7299df8409097de67647b371b81da7bcf49112e6 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Wed, 19 Dec 2018 19:25:42 +0100 Subject: [PATCH 06/18] simplified version --- crates/ra_editor/src/line_index_utils.rs | 139 +++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index e4fad04b2c3..6bd6a397ebf 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,6 +1,7 @@ use ra_text_edit::{AtomTextEdit}; use ra_syntax::{TextUnit, TextRange}; use crate::{LineIndex, LineCol}; +use superslice::Ext; #[derive(Debug)] struct OffsetNewlineIter<'a> { @@ -58,6 +59,136 @@ trait TranslatedNewlineIterator { } } +#[derive(Debug)] +struct AltEdit<'a> { + insert_newlines: OffsetNewlineIter<'a>, + delete: TextRange, + diff: i64, +} + +fn translate_range_by(range: TextRange, diff: i64) -> TextRange { + if diff == 0 { + range + } else { + let start = translate_by(range.start(), diff); + let end = translate_by(range.end(), diff); + TextRange::from_to(start, end) + } +} + +fn translate_by(x: TextUnit, diff: i64) -> TextUnit { + if diff == 0 { + x + } else { + TextUnit::from((x.to_usize() as i64 + diff) as u32) + } +} + +fn to_alt_edits<'a>(offset: TextUnit, edits: &'a [AtomTextEdit]) -> Vec> { + let mut xs: Vec> = Vec::with_capacity(edits.len()); + // collect and sort edits + for edit in edits { + // TODO discard after translating? + // if edit.delete.start() >= offset { + // continue; + // } + let insert_index = xs.upper_bound_by_key(&edit.delete.start(), |x| x.delete.start()); + let diff = edit.insert.len() as i64 - edit.delete.len().to_usize() as i64; + xs.insert( + insert_index, + AltEdit { + insert_newlines: OffsetNewlineIter { + offset: edit.delete.start(), + text: &edit.insert, + }, + delete: edit.delete, + diff: diff, + }, + ); + } + // translate edits by previous edits + for i in 1..xs.len() { + let (x, prevs) = xs[0..=i].split_last_mut().unwrap(); + for prev in prevs { + x.delete = translate_range_by(x.delete, prev.diff); + x.insert_newlines.offset = translate_by(x.insert_newlines.offset, prev.diff); + } + } + xs +} + +#[derive(Debug)] +enum NextNewline { + Use, + Discard, + Replace(TextUnit), + New(TextUnit), +} + +fn next_newline(candidate: Option, edits: &mut [AltEdit]) -> NextNewline { + let mut candidate = match candidate { + None => { + for edit in edits { + if let Some(inserted) = edit.insert_newlines.next() { + return NextNewline::New(inserted); + } + } + return NextNewline::Use; // END + } + Some(x) => x, + }; + + for edit in edits { + if candidate <= edit.delete.start() { + return NextNewline::Replace(candidate); + } else if candidate <= edit.delete.end() { + return match edit.insert_newlines.next() { + Some(x) => NextNewline::Replace(x), + None => NextNewline::Discard, + }; + } else { + if let Some(inserted) = edit.insert_newlines.next() { + return NextNewline::New(inserted); + } + candidate = translate_by(candidate, edit.diff); + } + } + return NextNewline::Replace(candidate); +} + +fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { + let mut edits = to_alt_edits(offset, edits); + let mut orig_newlines = line_index.newlines().iter().map(|x| *x).peekable(); + + let mut count = 0; + + loop { + let res = next_newline(orig_newlines.peek().map(|x| *x), &mut edits); + let next = match res { + NextNewline::Use => orig_newlines.next(), + NextNewline::Discard => { + orig_newlines.next(); + continue; + } + NextNewline::Replace(new) => { + orig_newlines.next(); + Some(new) + } + NextNewline::New(new) => Some(new), + }; + match next { + Some(n) if n <= offset => { + count += 1; + } + _ => { + break; + } + } + } + + count +} + struct TranslatedAtomEdit<'a> { delete: TextRange, insert: &'a str, @@ -256,5 +387,13 @@ mod test { let actual = translate_offset_with_edit(&line_index, x.offset, &x.edits); assert_eq!(actual.line, expected.line); } + + #[test] + fn test_translate_offset_with_edit_alt(x in arb_text_with_offset_and_edits()) { + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual_lines = count_newlines(x.offset, &line_index, &x.edits); + assert_eq!(actual_lines, expected.line); + } } } From a005d2a614031a18c9a5bf6557789a41f1b25c31 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Fri, 21 Dec 2018 18:38:28 +0100 Subject: [PATCH 07/18] final iteration, faster a bit simpler the main thing is we iterate over inserted newlines at once for each edit --- crates/ra_editor/src/line_index_utils.rs | 392 +++++++++++++---------- 1 file changed, 215 insertions(+), 177 deletions(-) diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 6bd6a397ebf..5ce2446c100 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,4 +1,4 @@ -use ra_text_edit::{AtomTextEdit}; +use ra_text_edit::AtomTextEdit; use ra_syntax::{TextUnit, TextRange}; use crate::{LineIndex, LineCol}; use superslice::Ext; @@ -24,41 +24,6 @@ impl<'a> Iterator for OffsetNewlineIter<'a> { } } -#[derive(Debug, Clone, Copy, PartialEq)] -enum TranslatedPos { - Before, - After, -} - -/// None means it was deleted -type TranslatedOffset = Option<(TranslatedPos, TextUnit)>; - -fn translate_offset(offset: TextUnit, edit: &TranslatedAtomEdit) -> TranslatedOffset { - if offset <= edit.delete.start() { - Some((TranslatedPos::Before, offset)) - } else if offset <= edit.delete.end() { - None - } else { - let diff = edit.insert.len() as i64 - edit.delete.len().to_usize() as i64; - let after = TextUnit::from((offset.to_usize() as i64 + diff) as u32); - Some((TranslatedPos::After, after)) - } -} - -trait TranslatedNewlineIterator { - fn translate(&self, offset: TextUnit) -> TextUnit; - fn translate_range(&self, range: TextRange) -> TextRange { - TextRange::from_to(self.translate(range.start()), self.translate(range.end())) - } - fn next_translated(&mut self) -> Option; - fn boxed<'a>(self) -> Box - where - Self: 'a + Sized, - { - Box::new(self) - } -} - #[derive(Debug)] struct AltEdit<'a> { insert_newlines: OffsetNewlineIter<'a>, @@ -156,7 +121,7 @@ fn next_newline(candidate: Option, edits: &mut [AltEdit]) -> NextNewli return NextNewline::Replace(candidate); } -fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { +pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { let mut edits = to_alt_edits(offset, edits); let mut orig_newlines = line_index.newlines().iter().map(|x| *x).peekable(); @@ -189,142 +154,184 @@ fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdi count } -struct TranslatedAtomEdit<'a> { +#[derive(Debug)] +enum NextNewlines<'a> { + Use, + ReplaceMany(OffsetNewlineIter<'a>), + AddMany(OffsetNewlineIter<'a>), +} + +#[derive(Debug)] +struct TranslatedEdit<'a> { delete: TextRange, insert: &'a str, + diff: i64, } -struct TranslatedNewlines<'a, T: TranslatedNewlineIterator> { - inner: T, - next_inner: Option, - edit: TranslatedAtomEdit<'a>, - insert: OffsetNewlineIter<'a>, +struct Edits<'a, 'b> { + edits: &'b [&'a AtomTextEdit], + current: Option>, + acc_diff: i64, } -impl<'a, T: TranslatedNewlineIterator> TranslatedNewlines<'a, T> { - fn from(inner: T, edit: &'a AtomTextEdit) -> Self { - let delete = inner.translate_range(edit.delete); - let mut res = TranslatedNewlines { - inner, - next_inner: None, - edit: TranslatedAtomEdit { - delete, - insert: &edit.insert, - }, - insert: OffsetNewlineIter { - offset: delete.start(), - text: &edit.insert, - }, +impl<'a, 'b> Edits<'a, 'b> { + fn new(sorted_edits: &'b [&'a AtomTextEdit]) -> Edits<'a, 'b> { + let mut x = Edits { + edits: sorted_edits, + current: None, + acc_diff: 0, }; - // prepare next_inner - res.advance_inner(); + x.advance_edit(); + x + } + fn advance_edit(&mut self) { + self.acc_diff += self.current.as_ref().map_or(0, |x| x.diff); + match self.edits.split_first() { + Some((next, rest)) => { + let delete = translate_range_by(next.delete, self.acc_diff); + let diff = next.insert.len() as i64 - next.delete.len().to_usize() as i64; + self.current = Some(TranslatedEdit { + delete, + insert: &next.insert, + diff, + }); + self.edits = rest; + } + None => { + self.current = None; + } + } + } + + fn next_inserted_newlines(&mut self) -> Option> { + let cur = self.current.as_ref()?; + let res = Some(OffsetNewlineIter { + offset: cur.delete.start(), + text: &cur.insert, + }); + self.advance_edit(); res } - fn advance_inner(&mut self) { - self.next_inner = self - .inner - .next_translated() - .map(|x| translate_offset(x, &self.edit)); - } -} - -impl<'a, T: TranslatedNewlineIterator> TranslatedNewlineIterator for TranslatedNewlines<'a, T> { - fn translate(&self, offset: TextUnit) -> TextUnit { - let offset = self.inner.translate(offset); - let (_, offset) = - translate_offset(offset, &self.edit).expect("translate_unit returned None"); - offset - } - - fn next_translated(&mut self) -> Option { - match self.next_inner { - None => self.insert.next(), - Some(next) => match next { - None => self.insert.next().or_else(|| { - self.advance_inner(); - self.next_translated() - }), - Some((TranslatedPos::Before, next)) => { - self.advance_inner(); - Some(next) + fn next_newlines(&mut self, candidate: TextUnit) -> NextNewlines { + let res = match &mut self.current { + Some(edit) => { + if candidate <= edit.delete.start() { + NextNewlines::Use + } else if candidate <= edit.delete.end() { + let iter = OffsetNewlineIter { + offset: edit.delete.start(), + text: &edit.insert, + }; + // empty slice + edit.insert = &edit.insert[edit.insert.len()..]; + NextNewlines::ReplaceMany(iter) + } else { + let iter = OffsetNewlineIter { + offset: edit.delete.start(), + text: &edit.insert, + }; + // empty slice + edit.insert = &edit.insert[edit.insert.len()..]; + self.advance_edit(); + NextNewlines::AddMany(iter) } - Some((TranslatedPos::After, next)) => self.insert.next().or_else(|| { - self.advance_inner(); - Some(next) - }), - }, - } + } + None => NextNewlines::Use, + }; + res } } -impl<'a> Iterator for Box { - type Item = TextUnit; - fn next(&mut self) -> Option { - self.next_translated() - } -} - -impl TranslatedNewlineIterator for Box { - fn translate(&self, offset: TextUnit) -> TextUnit { - self.as_ref().translate(offset) - } - fn next_translated(&mut self) -> Option { - self.as_mut().next_translated() - } -} - -struct IteratorWrapper>(T); - -impl> TranslatedNewlineIterator for IteratorWrapper { - fn translate(&self, offset: TextUnit) -> TextUnit { - offset - } - fn next_translated(&mut self) -> Option { - self.0.next() - } -} - -impl> Iterator for IteratorWrapper { - type Item = TextUnit; - fn next(&mut self) -> Option { - self.0.next() - } -} - -fn translate_newlines<'a>( - mut newlines: Box, - edits: &'a [AtomTextEdit], -) -> Box { +pub fn count_newlines_alt(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { + let mut sorted_edits: Vec<&AtomTextEdit> = Vec::with_capacity(edits.len()); for edit in edits { - newlines = TranslatedNewlines::from(newlines, edit).boxed(); + let insert_index = + sorted_edits.upper_bound_by_key(&edit.delete.start(), |x| x.delete.start()); + sorted_edits.insert(insert_index, &edit); } - newlines + + let mut state = Edits::new(&sorted_edits); + + let mut lines: u32 = 0; + + for &orig_newline in line_index.newlines() { + loop { + let translated_newline = translate_by(orig_newline, state.acc_diff); + match state.next_newlines(translated_newline) { + NextNewlines::Use => { + if offset < translated_newline { + return lines; + } else { + lines += 1; + } + break; + } + NextNewlines::ReplaceMany(ns) => { + for n in ns { + if offset < n { + return lines; + } else { + lines += 1; + } + } + break; + } + NextNewlines::AddMany(ns) => { + for n in ns { + if offset < n { + return lines; + } else { + lines += 1; + } + } + } + } + } + } + + loop { + match state.next_inserted_newlines() { + None => break, + Some(ns) => { + for n in ns { + if offset < n { + return lines; + } else { + lines += 1; + } + } + } + } + } + + lines } -pub fn translate_offset_with_edit( - pre_edit_index: &LineIndex, +// for bench +pub fn translate_after_edit( + pre_edit_text: &str, offset: TextUnit, - edits: &[AtomTextEdit], + edits: Vec, ) -> LineCol { - let mut newlines: Box = Box::new(IteratorWrapper( - pre_edit_index.newlines().iter().map(|x| *x), - )); + let text = edit_text(pre_edit_text, edits); + let line_index = LineIndex::new(&text); + line_index.line_col(offset) +} - newlines = translate_newlines(newlines, edits); +fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { + // apply edits ordered from last to first + // since they should not overlap we can just use start() + edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize)); - let mut line = 0; - for n in newlines { - if n > offset { - break; - } - line += 1; + let mut text = pre_edit_text.to_owned(); + + for edit in &edits { + let range = edit.delete.start().to_usize()..edit.delete.end().to_usize(); + text.replace_range(range, &edit.insert); } - LineCol { - line: line, - col_utf16: 0, // TODO not implemented yet - } + text } #[cfg(test)] @@ -354,46 +361,77 @@ mod test { .boxed() } - fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { - // apply edits ordered from last to first - // since they should not overlap we can just use start() - edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize)); - - let mut text = pre_edit_text.to_owned(); - - for edit in &edits { - let range = edit.delete.start().to_usize()..edit.delete.end().to_usize(); - text.replace_range(range, &edit.insert); - } - - text - } - - fn translate_after_edit( - pre_edit_text: &str, - offset: TextUnit, - edits: Vec, - ) -> LineCol { - let text = edit_text(pre_edit_text, edits); - let line_index = LineIndex::new(&text); - line_index.line_col(offset) - } - proptest! { #[test] fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { let line_index = LineIndex::new(&x.text); let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual = translate_offset_with_edit(&line_index, x.offset, &x.edits); - assert_eq!(actual.line, expected.line); + let actual_lines = count_newlines(x.offset, &line_index, &x.edits); + assert_eq!(actual_lines, expected.line); } #[test] fn test_translate_offset_with_edit_alt(x in arb_text_with_offset_and_edits()) { let line_index = LineIndex::new(&x.text); let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual_lines = count_newlines(x.offset, &line_index, &x.edits); + let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); assert_eq!(actual_lines, expected.line); } } + + #[test] + fn test_translate_offset_with_edit_alt_1() { + let x = ArbTextWithOffsetAndEdits { + text: String::from("aA\n"), + offset: 2.into(), + edits: vec![AtomTextEdit::delete(TextRange::from_to(1.into(), 2.into()))], + }; + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); + assert_eq!(actual_lines, expected.line); + } + + #[test] + fn test_translate_offset_with_edit_alt_2() { + let x = ArbTextWithOffsetAndEdits { + text: String::from("\nqꀸ#"), + offset: 5.into(), + edits: vec![AtomTextEdit::insert(1.into(), "\n".into())], + }; + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); + assert_eq!(actual_lines, expected.line); + } + + #[test] + fn test_translate_offset_with_edit_alt_3() { + let x = ArbTextWithOffsetAndEdits { + text: String::from("\n\n\n"), + offset: 0.into(), + edits: vec![AtomTextEdit::delete(TextRange::from_to(0.into(), 2.into()))], + }; + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); + assert_eq!(actual_lines, expected.line); + } + + #[test] + fn test_translate_offset_with_edit_alt_4() { + let x = ArbTextWithOffsetAndEdits { + text: String::from("☻54翑\"A"), + offset: 5.into(), + edits: vec![ + AtomTextEdit::delete(TextRange::from_to(0.into(), 8.into())), + AtomTextEdit::insert(9.into(), String::from("\n")), + ], + }; + let line_index = LineIndex::new(&x.text); + let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); + let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); + assert_eq!(actual_lines, expected.line); + } + } From d6312085a1ac97030fa768366585b9cfb6c955cd Mon Sep 17 00:00:00 2001 From: Bernardo Date: Fri, 21 Dec 2018 18:51:31 +0100 Subject: [PATCH 08/18] remove slower impl, add benchmarks --- Cargo.lock | 178 ++++++++++++++ crates/ra_editor/Cargo.toml | 8 + .../translate_offset_with_edit_benchmark.rs | 88 +++++++ crates/ra_editor/src/lib.rs | 4 +- crates/ra_editor/src/line_index_utils.rs | 218 ++---------------- crates/ra_text_edit/src/test_utils.rs | 9 +- 6 files changed, 304 insertions(+), 201 deletions(-) create mode 100644 crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs diff --git a/Cargo.lock b/Cargo.lock index 127d7772a71..1ee2df8ddb4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,6 +122,11 @@ dependencies = [ "serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "cast" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "cc" version = "1.0.27" @@ -164,6 +169,51 @@ dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "criterion" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", + "criterion-plot 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "criterion-stats 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "csv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "failure_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "handlebars 0.32.4 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools-num 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)", + "simplelog 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "criterion-plot" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "criterion-stats" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", + "thread-scoped 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "crossbeam-channel" version = "0.2.6" @@ -233,6 +283,23 @@ dependencies = [ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "csv" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "csv-core 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "csv-core" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "derive-new" version = "0.5.6" @@ -368,6 +435,21 @@ name = "glob" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "handlebars" +version = "0.32.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "pest 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "pest_derive 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "heck" version = "0.3.1" @@ -418,6 +500,14 @@ dependencies = [ "either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "itertools-num" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "itoa" version = "0.4.3" @@ -596,6 +686,11 @@ name = "percent-encoding" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "pest" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "pest" version = "2.0.2" @@ -604,6 +699,16 @@ dependencies = [ "ucd-trie 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "pest_derive" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "pest 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "pest_derive" version = "2.0.1" @@ -665,6 +770,11 @@ name = "quick-error" version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "quote" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "quote" version = "0.6.10" @@ -721,11 +831,15 @@ dependencies = [ name = "ra_editor" version = "0.1.0" dependencies = [ + "criterion 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", "join_to_string 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)", "ra_syntax 0.1.0", "ra_text_edit 0.1.0", + "rand 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_xorshift 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "superslice 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "test_utils 0.1.0", @@ -1126,6 +1240,16 @@ dependencies = [ "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "simplelog" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "term 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "slug" version = "0.1.4" @@ -1165,6 +1289,16 @@ name = "superslice" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "syn" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "syn" version = "0.14.9" @@ -1185,6 +1319,14 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "synom" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "synstructure" version = "0.10.1" @@ -1249,6 +1391,15 @@ dependencies = [ "tera 0.11.20 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "term" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "termion" version = "1.5.1" @@ -1285,6 +1436,11 @@ dependencies = [ "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "thread-scoped" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "thread_local" version = "0.3.6" @@ -1413,6 +1569,11 @@ name = "unicode-width" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unicode-xid" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-xid" version = "0.1.0" @@ -1527,11 +1688,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40" "checksum byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94f88df23a25417badc922ab0f5716cc1330e87f71ddd9203b3a3ccd9cedf75d" "checksum cargo_metadata 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e5d1b4d380e1bab994591a24c2bdd1b054f64b60bef483a8c598c7c345bc3bbe" +"checksum cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "926013f2860c46252efceabb19f4a6b308197505082c609025aa6706c011d427" "checksum cc 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "155ed195f7bd722d1dfeb30365b9d0c1f6a078fa7ca4014497e5935d90993d6f" "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" "checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" "checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +"checksum criterion 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c47d2b548c5647e1a436dc0cb78d4ebf51b6bf7ab101ed76662828bdd4d3a24a" +"checksum criterion-plot 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6e649d6aacdbbdb94ec659561a309a71336fc5655ed408f3afd28df2fc0c4f4f" +"checksum criterion-stats 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ff43cac80562f91ead0b617c1be74edf350adfaa195809d355de98dfc8f9237d" "checksum crossbeam-channel 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7b85741761b7f160bc5e7e0c14986ef685b7f8bf9b7ad081c60c604bb4649827" "checksum crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f739f8c5363aca78cfb059edf753d8f0d36908c348f3d8d1503f03d8b75d9cf3" "checksum crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "927121f5407de9956180ff5e936fe3cf4324279280001cd56b669d28ee7e9150" @@ -1539,6 +1704,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2760899e32a1d58d5abb31129f8fae5de75220bc2176e77ff7c627ae45c918d9" "checksum crossbeam-utils 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "677d453a17e8bd2b913fa38e8b9cf04bcdbb5be790aa294f2389661d72036015" "checksum crossbeam-utils 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "41ee4864f4797060e52044376f7d107429ce1fb43460021b126424b7180ee21a" +"checksum csv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "9fd1c44c58078cfbeaf11fbb3eac9ae5534c23004ed770cc4bfb48e658ae4f04" +"checksum csv-core 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa5cdef62f37e6ffe7d1f07a381bc0db32b7a3ff1cac0de56cb0d81e71f53d65" "checksum derive-new 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "6ca414e896ae072546f4d789f452daaecf60ddee4c9df5dc6d5936d769e3d87c" "checksum deunicode 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690" "checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" @@ -1556,6 +1723,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" +"checksum handlebars 0.32.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d89ec99d1594f285d4590fc32bac5f75cdab383f1123d504d27862c644a807dd" "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" "checksum humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" "checksum id-arena 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3a7250033feafee46a1cecd2c2616a64aec1d064f38c9ae2bdd297728542843e" @@ -1563,6 +1731,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum im 12.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ae9c7f9bb8aee47fc16d535a705f7867a9fc83bb822e5e1043bb98e77ffeed3c" "checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d" "checksum itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0d47946d458e94a1b7bcabbf6521ea7c037062c81f534615abcad76e84d4970d" +"checksum itertools-num 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a872a22f9e6f7521ca557660adb96dd830e54f0f490fa115bb55dd69d38b27e7" "checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" "checksum join_to_string 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7bddc885f3fd69dd4b5d747c2efe6dd2c36d795ea9938281ed50910e32c95e31" "checksum languageserver-types 0.53.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a0be4f8f783832a308975035043c0e8ad18e11c3f3d395aa6f1a9f3a30b8e654" @@ -1586,13 +1755,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c" "checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9" "checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" +"checksum pest 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0fce5d8b5cc33983fc74f78ad552b5522ab41442c4ca91606e4236eb4b5ceefc" "checksum pest 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a677051ad923732bb5c70f2d45f8985a96e3eee2e2bff86697e3b11b0c3fcfde" +"checksum pest_derive 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3294f437119209b084c797604295f40227cffa35c57220b1e99a6ff3bf8ee4" "checksum pest_derive 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b76f477146419bc539a63f4ef40e902166cb43b3e51cecc71d9136fd12c567e7" "checksum pest_generator 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ebee4e9680be4fd162e6f3394ae4192a6b60b1e4d17d845e631f0c68d1a3386" "checksum pest_meta 2.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1f6d5f6f0e6082578c86af197d780dc38328e3f768cec06aac9bc46d714e8221" "checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09" "checksum proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)" = "926d0604475349f463fe44130aae73f2294b5309ab2ca0310b998bd334ef191f" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" +"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c" "checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd" "checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c" @@ -1629,22 +1801,27 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde_derive 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)" = "96a7f9496ac65a2db5929afa087b54f8fc5008dcfbe48a8874ed20049b0d6154" "checksum serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)" = "c37ccd6be3ed1fdf419ee848f7c758eb31b054d7cd3ae3600e3bae0adf569811" "checksum sha-1 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "51b9d1f3b5de8a167ab06834a7c883bd197f2191e1dda1a22d9ccfeedbf9aded" +"checksum simplelog 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e95345f185d5adeb8ec93459d2dc99654e294cc6ccf5b75414d8ea262de9a13" "checksum slug 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b3bc762e6a4b6c6fcaade73e77f9ebc6991b676f88bb2358bddb56560f073373" "checksum smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b73ea3738b47563803ef814925e69be00799a8c07420be8b996f8e98fb2336db" "checksum smol_str 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "486a74e9b9fc53373808f7a17e10fc728adcb1fbe272292271d8bea61175e181" "checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum superslice 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b50b13d42370e0f5fc62eafdd5c2d20065eaf5458dab215ff3e20e63eea96b30" +"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)" = "261ae9ecaa397c42b960649561949d69311f08eeaea86a65696e6e46517cf741" "checksum syn 0.15.23 (registry+https://github.com/rust-lang/crates.io-index)" = "9545a6a093a3f0bd59adb472700acc08cad3776f860f16a897dfce8c88721cbc" +"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" "checksum tempfile 3.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "7e91405c14320e5c79b3d148e1c86f40749a36e490642202a31689cb1a3452b2" "checksum tera 0.11.20 (registry+https://github.com/rust-lang/crates.io-index)" = "4b505279e19d8f7d24b1a9dc58327c9c36174b1a2c7ebdeac70792d017cb64f3" "checksum teraron 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0d89ad4617d1dec55331067fadaa041e813479e1779616f3d3ce9308bf46184e" +"checksum term 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5e6b677dd1e8214ea1ef4297f85dbcbed8e8cdddb561040cc998ca2551c37561" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8009d7bdbd896a7e09b595f8f9325a19047fc708653e60d0895202b82135048f" "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" +"checksum thread-scoped 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bcbb6aa301e5d3b0b5ef639c9a9c7e2f1c944f177b460c04dc24c69b1fa2bd99" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f0c90a5f3459330ac8bc0d2f879c693bb7a2f59689c1083fc4ef83834da865" "checksum time 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "847da467bf0db05882a9e2375934a8a55cffdc9db0d128af1518200260ba1f6c" @@ -1661,6 +1838,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum unicode-normalization 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "6a0180bc61fc5a987082bfa111f4cc95c4caff7f9799f3e46df09163a937aa25" "checksum unicode-segmentation 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "aa6024fc12ddfd1c6dbc14a80fa2324d4568849869b779f6bd37e5e4c03344d1" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" +"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" "checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" diff --git a/crates/ra_editor/Cargo.toml b/crates/ra_editor/Cargo.toml index 1ad99af2806..039688d7df9 100644 --- a/crates/ra_editor/Cargo.toml +++ b/crates/ra_editor/Cargo.toml @@ -18,3 +18,11 @@ proptest = "0.8.7" [dev-dependencies] test_utils = { path = "../test_utils" } +criterion = "0.2" +rand = "*" +rand_xorshift = "*" +lazy_static = "*" + +[[bench]] +name = "translate_offset_with_edit_benchmark" +harness = false \ No newline at end of file diff --git a/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs b/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs new file mode 100644 index 00000000000..b345a91aecb --- /dev/null +++ b/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs @@ -0,0 +1,88 @@ +use criterion::{criterion_group, criterion_main}; +use criterion::Criterion; +use criterion::Fun; +use ra_text_edit::AtomTextEdit; +use ra_text_edit::test_utils::{arb_edits_custom, arb_offset}; +use ra_editor::line_index_utils; +use ra_editor::LineIndex; +use ra_syntax::TextUnit; +use proptest::test_runner; +use proptest::string::string_regex; +use proptest::strategy::{Strategy, ValueTree}; +use rand_xorshift::XorShiftRng; +use rand::SeedableRng; +use lazy_static::lazy_static; + +#[derive(Debug)] +struct Data { + text: String, + line_index: LineIndex, + edits: Vec, + offset: TextUnit, +} + +fn setup_data() -> Data { + let mut runner = test_runner::TestRunner::default(); + { + struct TestRng { + rng: XorShiftRng, + } + // HACK to be able to manually seed the TestRunner + let rng: &mut TestRng = unsafe { std::mem::transmute(runner.rng()) }; + rng.rng = XorShiftRng::seed_from_u64(0); + } + + let text = { + let arb = string_regex("([a-zA-Z_0-9]{10,50}.{1,5}\n){100,500}").unwrap(); + let tree = arb.new_tree(&mut runner).unwrap(); + tree.current() + }; + + let edits = { + let arb = arb_edits_custom(&text, 99, 100); + let tree = arb.new_tree(&mut runner).unwrap(); + tree.current() + }; + + let offset = { + let arb = arb_offset(&text); + let tree = arb.new_tree(&mut runner).unwrap(); + tree.current() + }; + + let line_index = LineIndex::new(&text); + + Data { + text, + line_index, + edits, + offset, + } +} + +lazy_static! { + static ref DATA: Data = setup_data(); +} + +fn compare_translates(c: &mut Criterion) { + let f1 = Fun::new("translate_after_edit", |b, _| { + b.iter(|| { + let d = &*DATA; + line_index_utils::translate_after_edit(&d.text, d.offset, d.edits.clone()); + }) + }); + + let f2 = Fun::new("count_newlines", |b, _| { + b.iter(|| { + let d = &*DATA; + line_index_utils::count_newlines(d.offset, &d.line_index, &d.edits); + }) + }); + + let functions = vec![f1, f2]; + + c.bench_functions("translate", functions, ()); +} + +criterion_group!(benches, compare_translates); +criterion_main!(benches); diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 619497f0b4b..7145c6cfbe6 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -2,7 +2,8 @@ mod code_actions; mod extend_selection; mod folding_ranges; mod line_index; -mod line_index_utils; +// public for benchmarkig +pub mod line_index_utils; mod symbols; #[cfg(test)] mod test_utils; @@ -13,7 +14,6 @@ pub use self::{ extend_selection::extend_selection, folding_ranges::{folding_ranges, Fold, FoldKind}, line_index::{LineCol, LineIndex}, - line_index_utils::translate_offset_with_edit, symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, typing::{join_lines, on_enter, on_eq_typed}, }; diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 5ce2446c100..e62c5089d8d 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -24,136 +24,6 @@ impl<'a> Iterator for OffsetNewlineIter<'a> { } } -#[derive(Debug)] -struct AltEdit<'a> { - insert_newlines: OffsetNewlineIter<'a>, - delete: TextRange, - diff: i64, -} - -fn translate_range_by(range: TextRange, diff: i64) -> TextRange { - if diff == 0 { - range - } else { - let start = translate_by(range.start(), diff); - let end = translate_by(range.end(), diff); - TextRange::from_to(start, end) - } -} - -fn translate_by(x: TextUnit, diff: i64) -> TextUnit { - if diff == 0 { - x - } else { - TextUnit::from((x.to_usize() as i64 + diff) as u32) - } -} - -fn to_alt_edits<'a>(offset: TextUnit, edits: &'a [AtomTextEdit]) -> Vec> { - let mut xs: Vec> = Vec::with_capacity(edits.len()); - // collect and sort edits - for edit in edits { - // TODO discard after translating? - // if edit.delete.start() >= offset { - // continue; - // } - let insert_index = xs.upper_bound_by_key(&edit.delete.start(), |x| x.delete.start()); - let diff = edit.insert.len() as i64 - edit.delete.len().to_usize() as i64; - xs.insert( - insert_index, - AltEdit { - insert_newlines: OffsetNewlineIter { - offset: edit.delete.start(), - text: &edit.insert, - }, - delete: edit.delete, - diff: diff, - }, - ); - } - // translate edits by previous edits - for i in 1..xs.len() { - let (x, prevs) = xs[0..=i].split_last_mut().unwrap(); - for prev in prevs { - x.delete = translate_range_by(x.delete, prev.diff); - x.insert_newlines.offset = translate_by(x.insert_newlines.offset, prev.diff); - } - } - xs -} - -#[derive(Debug)] -enum NextNewline { - Use, - Discard, - Replace(TextUnit), - New(TextUnit), -} - -fn next_newline(candidate: Option, edits: &mut [AltEdit]) -> NextNewline { - let mut candidate = match candidate { - None => { - for edit in edits { - if let Some(inserted) = edit.insert_newlines.next() { - return NextNewline::New(inserted); - } - } - return NextNewline::Use; // END - } - Some(x) => x, - }; - - for edit in edits { - if candidate <= edit.delete.start() { - return NextNewline::Replace(candidate); - } else if candidate <= edit.delete.end() { - return match edit.insert_newlines.next() { - Some(x) => NextNewline::Replace(x), - None => NextNewline::Discard, - }; - } else { - if let Some(inserted) = edit.insert_newlines.next() { - return NextNewline::New(inserted); - } - candidate = translate_by(candidate, edit.diff); - } - } - return NextNewline::Replace(candidate); -} - -pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { - let mut edits = to_alt_edits(offset, edits); - let mut orig_newlines = line_index.newlines().iter().map(|x| *x).peekable(); - - let mut count = 0; - - loop { - let res = next_newline(orig_newlines.peek().map(|x| *x), &mut edits); - let next = match res { - NextNewline::Use => orig_newlines.next(), - NextNewline::Discard => { - orig_newlines.next(); - continue; - } - NextNewline::Replace(new) => { - orig_newlines.next(); - Some(new) - } - NextNewline::New(new) => Some(new), - }; - match next { - Some(n) if n <= offset => { - count += 1; - } - _ => { - break; - } - } - } - - count -} - #[derive(Debug)] enum NextNewlines<'a> { Use, @@ -188,7 +58,7 @@ impl<'a, 'b> Edits<'a, 'b> { self.acc_diff += self.current.as_ref().map_or(0, |x| x.diff); match self.edits.split_first() { Some((next, rest)) => { - let delete = translate_range_by(next.delete, self.acc_diff); + let delete = self.translate_range(next.delete); let diff = next.insert.len() as i64 - next.delete.len().to_usize() as i64; self.current = Some(TranslatedEdit { delete, @@ -241,9 +111,27 @@ impl<'a, 'b> Edits<'a, 'b> { }; res } + + fn translate_range(&self, range: TextRange) -> TextRange { + if self.acc_diff == 0 { + range + } else { + let start = self.translate(range.start()); + let end = self.translate(range.end()); + TextRange::from_to(start, end) + } + } + + fn translate(&self, x: TextUnit) -> TextUnit { + if self.acc_diff == 0 { + x + } else { + TextUnit::from((x.to_usize() as i64 + self.acc_diff) as u32) + } + } } -pub fn count_newlines_alt(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { +pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { let mut sorted_edits: Vec<&AtomTextEdit> = Vec::with_capacity(edits.len()); for edit in edits { let insert_index = @@ -257,7 +145,7 @@ pub fn count_newlines_alt(offset: TextUnit, line_index: &LineIndex, edits: &[Ato for &orig_newline in line_index.newlines() { loop { - let translated_newline = translate_by(orig_newline, state.acc_diff); + let translated_newline = state.translate(orig_newline); match state.next_newlines(translated_newline) { NextNewlines::Use => { if offset < translated_newline { @@ -369,69 +257,5 @@ mod test { let actual_lines = count_newlines(x.offset, &line_index, &x.edits); assert_eq!(actual_lines, expected.line); } - - #[test] - fn test_translate_offset_with_edit_alt(x in arb_text_with_offset_and_edits()) { - let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); - assert_eq!(actual_lines, expected.line); - } } - - #[test] - fn test_translate_offset_with_edit_alt_1() { - let x = ArbTextWithOffsetAndEdits { - text: String::from("aA\n"), - offset: 2.into(), - edits: vec![AtomTextEdit::delete(TextRange::from_to(1.into(), 2.into()))], - }; - let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); - assert_eq!(actual_lines, expected.line); - } - - #[test] - fn test_translate_offset_with_edit_alt_2() { - let x = ArbTextWithOffsetAndEdits { - text: String::from("\nqꀸ#"), - offset: 5.into(), - edits: vec![AtomTextEdit::insert(1.into(), "\n".into())], - }; - let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); - assert_eq!(actual_lines, expected.line); - } - - #[test] - fn test_translate_offset_with_edit_alt_3() { - let x = ArbTextWithOffsetAndEdits { - text: String::from("\n\n\n"), - offset: 0.into(), - edits: vec![AtomTextEdit::delete(TextRange::from_to(0.into(), 2.into()))], - }; - let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); - assert_eq!(actual_lines, expected.line); - } - - #[test] - fn test_translate_offset_with_edit_alt_4() { - let x = ArbTextWithOffsetAndEdits { - text: String::from("☻54翑\"A"), - offset: 5.into(), - edits: vec![ - AtomTextEdit::delete(TextRange::from_to(0.into(), 8.into())), - AtomTextEdit::insert(9.into(), String::from("\n")), - ], - }; - let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual_lines = count_newlines_alt(x.offset, &line_index, &x.edits); - assert_eq!(actual_lines, expected.line); - } - } diff --git a/crates/ra_text_edit/src/test_utils.rs b/crates/ra_text_edit/src/test_utils.rs index 4a0ebc08ecf..f150288f6ac 100644 --- a/crates/ra_text_edit/src/test_utils.rs +++ b/crates/ra_text_edit/src/test_utils.rs @@ -24,6 +24,10 @@ pub fn arb_offset(text: &str) -> BoxedStrategy { } pub fn arb_edits(text: &str) -> BoxedStrategy> { + arb_edits_custom(&text, 0, 7) +} + +pub fn arb_edits_custom(text: &str, min: usize, max: usize) -> BoxedStrategy> { if text.is_empty() { // only valid edits return Just(vec![]) @@ -37,9 +41,10 @@ pub fn arb_edits(text: &str) -> BoxedStrategy> { } let offsets = text_offsets(text); - let max_cuts = offsets.len().min(7); + let max_cuts = max.min(offsets.len()); + let min_cuts = min.min(offsets.len() - 1); - proptest::sample::subsequence(offsets, 0..max_cuts) + proptest::sample::subsequence(offsets, min_cuts..max_cuts) .prop_flat_map(|cuts| { let strategies: Vec<_> = cuts .chunks(2) From 1c44ba0f04a0997617d517111d0a08245f0dacac Mon Sep 17 00:00:00 2001 From: Bernardo Date: Fri, 21 Dec 2018 20:11:27 +0100 Subject: [PATCH 09/18] simplify newline check with macro --- crates/ra_editor/src/line_index_utils.rs | 34 ++++++++++-------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index e62c5089d8d..2dd7fd70866 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -143,35 +143,33 @@ pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTex let mut lines: u32 = 0; + macro_rules! test_newline { + ($x:ident) => { + if offset < $x { + return lines; + } else { + lines += 1; + } + }; + } + for &orig_newline in line_index.newlines() { loop { let translated_newline = state.translate(orig_newline); match state.next_newlines(translated_newline) { NextNewlines::Use => { - if offset < translated_newline { - return lines; - } else { - lines += 1; - } + test_newline!(translated_newline); break; } NextNewlines::ReplaceMany(ns) => { for n in ns { - if offset < n { - return lines; - } else { - lines += 1; - } + test_newline!(n); } break; } NextNewlines::AddMany(ns) => { for n in ns { - if offset < n { - return lines; - } else { - lines += 1; - } + test_newline!(n); } } } @@ -183,11 +181,7 @@ pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTex None => break, Some(ns) => { for n in ns { - if offset < n { - return lines; - } else { - lines += 1; - } + test_newline!(n); } } } From 36f2b1f3b9c36ace65852d971f998a090cf9b5d5 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sat, 22 Dec 2018 12:59:48 +0100 Subject: [PATCH 10/18] iterate over `Step`s which are either, newlines or multibyte chars --- crates/ra_editor/src/line_index.rs | 14 ++- crates/ra_editor/src/line_index_utils.rs | 133 +++++++++++++++++++---- 2 files changed, 121 insertions(+), 26 deletions(-) diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index 7d9b8d79fe7..c29e2e49a30 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -15,9 +15,9 @@ pub struct LineCol { } #[derive(Clone, Debug, Hash, PartialEq, Eq)] -struct Utf16Char { - start: TextUnit, - end: TextUnit, +pub(crate) struct Utf16Char { + pub(crate) start: TextUnit, + pub(crate) end: TextUnit, } impl Utf16Char { @@ -122,7 +122,13 @@ impl LineIndex { } pub(crate) fn newlines(&self) -> &[TextUnit] { - &self.newlines[1..] + &self.newlines[..] + } + + pub(crate) fn utf16_chars(&self, newline_idx: usize) -> Option<&[Utf16Char]> { + self.utf16_lines + .get(&(newline_idx as u32)) + .map(|x| x.as_slice()) } } diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 2dd7fd70866..9c8d801e909 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,8 +1,58 @@ use ra_text_edit::AtomTextEdit; use ra_syntax::{TextUnit, TextRange}; -use crate::{LineIndex, LineCol}; +use crate::{LineIndex, LineCol, line_index::Utf16Char}; use superslice::Ext; +#[derive(Debug, Clone)] +enum Step { + Newline(TextUnit), + Utf16Char(TextRange), +} + +struct LineIndexStepIter<'a> { + line_index: &'a LineIndex, + newlines: std::slice::Iter<'a, TextUnit>, + next_newline_idx: usize, + utf16_chars: Option>, +} + +impl<'a> LineIndexStepIter<'a> { + fn from(line_index: &LineIndex) -> LineIndexStepIter { + let mut x = LineIndexStepIter { + line_index, + newlines: line_index.newlines().iter(), + next_newline_idx: 0, + utf16_chars: None, + }; + // skip first newline since it's not real + x.next(); + x + } +} + +impl<'a> Iterator for LineIndexStepIter<'a> { + type Item = Step; + fn next(&mut self) -> Option { + self.utf16_chars + .as_mut() + .and_then(|x| { + None + // TODO Enable + // let x = x.next()?; + // Some(Step::Utf16Char(TextRange::from_to(x.start, x.end))) + }) + .or_else(|| { + self.utf16_chars = self + .line_index + .utf16_chars(self.next_newline_idx) + .map(|x| x.iter()); + self.next_newline_idx += 1; + let x = self.newlines.next()?; + Some(Step::Newline(*x)) + }) + } +} + #[derive(Debug)] struct OffsetNewlineIter<'a> { text: &'a str, @@ -10,16 +60,35 @@ struct OffsetNewlineIter<'a> { } impl<'a> Iterator for OffsetNewlineIter<'a> { - type Item = TextUnit; - fn next(&mut self) -> Option { - let next_idx = self + type Item = Step; + fn next(&mut self) -> Option { + let (next, next_offset) = self .text .char_indices() - .filter_map(|(i, c)| if c == '\n' { Some(i + 1) } else { None }) + .filter_map(|(i, c)| { + if c == '\n' { + let next_offset = self.offset + TextUnit::from_usize(i + 1); + let next = Step::Newline(next_offset); + Some((next, next_offset)) + } else { + None + // TODO enable + // let char_len = TextUnit::of_char(c); + // if char_len.to_usize() > 1 { + // let start = self.offset + TextUnit::from_usize(i); + // let end = start + char_len; + // let next = Step::Utf16Char(TextRange::from_to(start, end)); + // let next_offset = end; + // Some((next, next_offset)) + // } else { + // None + // } + } + }) .next()?; - let next = self.offset + TextUnit::from_usize(next_idx); + let next_idx = (next_offset - self.offset).to_usize(); self.text = &self.text[next_idx..]; - self.offset = next; + self.offset = next_offset; Some(next) } } @@ -83,12 +152,16 @@ impl<'a, 'b> Edits<'a, 'b> { res } - fn next_newlines(&mut self, candidate: TextUnit) -> NextNewlines { + fn next_step(&mut self, step: &Step) -> NextNewlines { + let step_pos = match step { + &Step::Newline(n) => n, + &Step::Utf16Char(r) => unimplemented!(), + }; let res = match &mut self.current { Some(edit) => { - if candidate <= edit.delete.start() { + if step_pos <= edit.delete.start() { NextNewlines::Use - } else if candidate <= edit.delete.end() { + } else if step_pos <= edit.delete.end() { let iter = OffsetNewlineIter { offset: edit.delete.start(), text: &edit.insert, @@ -129,6 +202,17 @@ impl<'a, 'b> Edits<'a, 'b> { TextUnit::from((x.to_usize() as i64 + self.acc_diff) as u32) } } + + fn translate_step(&self, x: &Step) -> Step { + if self.acc_diff == 0 { + x.clone() + } else { + match x { + &Step::Newline(n) => Step::Newline(self.translate(n)), + &Step::Utf16Char(r) => Step::Utf16Char(self.translate_range(r)), + } + } + } } pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { @@ -143,33 +227,38 @@ pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTex let mut lines: u32 = 0; - macro_rules! test_newline { + macro_rules! test_step { ($x:ident) => { - if offset < $x { - return lines; - } else { - lines += 1; + match &$x { + Step::Newline(n) => { + if offset < *n { + return lines; + } else { + lines += 1; + } + } + Step::Utf16Char(x) => unimplemented!(), } }; } - for &orig_newline in line_index.newlines() { + for orig_step in LineIndexStepIter::from(line_index) { loop { - let translated_newline = state.translate(orig_newline); - match state.next_newlines(translated_newline) { + let translated_newline = state.translate_step(&orig_step); + match state.next_step(&translated_newline) { NextNewlines::Use => { - test_newline!(translated_newline); + test_step!(translated_newline); break; } NextNewlines::ReplaceMany(ns) => { for n in ns { - test_newline!(n); + test_step!(n); } break; } NextNewlines::AddMany(ns) => { for n in ns { - test_newline!(n); + test_step!(n); } } } @@ -181,7 +270,7 @@ pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTex None => break, Some(ns) => { for n in ns { - test_newline!(n); + test_step!(n); } } } From 5c8525ce4ae8bb969f2ac263bf14adad1c835c03 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sat, 22 Dec 2018 15:44:27 +0100 Subject: [PATCH 11/18] column translation implemented but not quite working yet --- .../translate_offset_with_edit_benchmark.rs | 4 +- crates/ra_editor/src/line_index.rs | 14 +-- crates/ra_editor/src/line_index_utils.rs | 102 +++++++++++------- 3 files changed, 70 insertions(+), 50 deletions(-) diff --git a/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs b/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs index b345a91aecb..89ed998235a 100644 --- a/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs +++ b/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs @@ -72,10 +72,10 @@ fn compare_translates(c: &mut Criterion) { }) }); - let f2 = Fun::new("count_newlines", |b, _| { + let f2 = Fun::new("translate_offset_with_edit", |b, _| { b.iter(|| { let d = &*DATA; - line_index_utils::count_newlines(d.offset, &d.line_index, &d.edits); + line_index_utils::translate_offset_with_edit(&d.line_index, d.offset, &d.edits); }) }); diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index c29e2e49a30..6dbabd97e00 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -4,8 +4,8 @@ use superslice::Ext; #[derive(Clone, Debug, PartialEq, Eq)] pub struct LineIndex { - newlines: Vec, - utf16_lines: FxHashMap>, + pub(crate) newlines: Vec, + pub(crate) utf16_lines: FxHashMap>, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] @@ -120,16 +120,6 @@ impl LineIndex { col } - - pub(crate) fn newlines(&self) -> &[TextUnit] { - &self.newlines[..] - } - - pub(crate) fn utf16_chars(&self, newline_idx: usize) -> Option<&[Utf16Char]> { - self.utf16_lines - .get(&(newline_idx as u32)) - .map(|x| x.as_slice()) - } } #[test] diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 9c8d801e909..382f3ac7200 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -9,18 +9,17 @@ enum Step { Utf16Char(TextRange), } +#[derive(Debug)] struct LineIndexStepIter<'a> { line_index: &'a LineIndex, - newlines: std::slice::Iter<'a, TextUnit>, next_newline_idx: usize, - utf16_chars: Option>, + utf16_chars: Option<(TextUnit, std::slice::Iter<'a, Utf16Char>)>, } impl<'a> LineIndexStepIter<'a> { fn from(line_index: &LineIndex) -> LineIndexStepIter { let mut x = LineIndexStepIter { line_index, - newlines: line_index.newlines().iter(), next_newline_idx: 0, utf16_chars: None, }; @@ -35,20 +34,22 @@ impl<'a> Iterator for LineIndexStepIter<'a> { fn next(&mut self) -> Option { self.utf16_chars .as_mut() - .and_then(|x| { - None - // TODO Enable - // let x = x.next()?; - // Some(Step::Utf16Char(TextRange::from_to(x.start, x.end))) + .and_then(|(newline, x)| { + let x = x.next()?; + Some(Step::Utf16Char(TextRange::from_to( + *newline + x.start, + *newline + x.end, + ))) }) .or_else(|| { + let next_newline = *self.line_index.newlines.get(self.next_newline_idx)?; self.utf16_chars = self .line_index - .utf16_chars(self.next_newline_idx) - .map(|x| x.iter()); + .utf16_lines + .get(&(self.next_newline_idx as u32)) + .map(|x| (next_newline, x.iter())); self.next_newline_idx += 1; - let x = self.newlines.next()?; - Some(Step::Newline(*x)) + Some(Step::Newline(next_newline)) }) } } @@ -71,18 +72,16 @@ impl<'a> Iterator for OffsetNewlineIter<'a> { let next = Step::Newline(next_offset); Some((next, next_offset)) } else { - None - // TODO enable - // let char_len = TextUnit::of_char(c); - // if char_len.to_usize() > 1 { - // let start = self.offset + TextUnit::from_usize(i); - // let end = start + char_len; - // let next = Step::Utf16Char(TextRange::from_to(start, end)); - // let next_offset = end; - // Some((next, next_offset)) - // } else { - // None - // } + let char_len = TextUnit::of_char(c); + if char_len.to_usize() > 1 { + let start = self.offset + TextUnit::from_usize(i); + let end = start + char_len; + let next = Step::Utf16Char(TextRange::from_to(start, end)); + let next_offset = end; + Some((next, next_offset)) + } else { + None + } } }) .next()?; @@ -155,7 +154,7 @@ impl<'a, 'b> Edits<'a, 'b> { fn next_step(&mut self, step: &Step) -> NextNewlines { let step_pos = match step { &Step::Newline(n) => n, - &Step::Utf16Char(r) => unimplemented!(), + &Step::Utf16Char(r) => r.start(), }; let res = match &mut self.current { Some(edit) => { @@ -215,7 +214,11 @@ impl<'a, 'b> Edits<'a, 'b> { } } -pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTextEdit]) -> u32 { +pub fn translate_offset_with_edit( + line_index: &LineIndex, + offset: TextUnit, + edits: &[AtomTextEdit], +) -> LineCol { let mut sorted_edits: Vec<&AtomTextEdit> = Vec::with_capacity(edits.len()); for edit in edits { let insert_index = @@ -225,29 +228,55 @@ pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTex let mut state = Edits::new(&sorted_edits); - let mut lines: u32 = 0; + let mut pos: LineCol = LineCol { + line: 0, + col_utf16: 0, + }; + + let mut last_newline: TextUnit = TextUnit::from(0); + let mut col_adjust: TextUnit = TextUnit::from(0); macro_rules! test_step { ($x:ident) => { match &$x { Step::Newline(n) => { if offset < *n { - return lines; + return_pos!(); + } else if offset == *n { + pos.line += 1; + pos.col_utf16 = 0; + return pos; } else { - lines += 1; + pos.line += 1; + pos.col_utf16 = 0; + last_newline = *n; + col_adjust = TextUnit::from(0); + } + } + Step::Utf16Char(x) => { + if offset < x.end() { + return_pos!(); + } else { + col_adjust += x.len() - TextUnit::from(1); } } - Step::Utf16Char(x) => unimplemented!(), } }; } + macro_rules! return_pos { + () => { + pos.col_utf16 = ((offset - last_newline) - col_adjust).into(); + return pos; + }; + } + for orig_step in LineIndexStepIter::from(line_index) { loop { - let translated_newline = state.translate_step(&orig_step); - match state.next_step(&translated_newline) { + let translated_step = state.translate_step(&orig_step); + match state.next_step(&translated_step) { NextNewlines::Use => { - test_step!(translated_newline); + test_step!(translated_step); break; } NextNewlines::ReplaceMany(ns) => { @@ -276,7 +305,7 @@ pub fn count_newlines(offset: TextUnit, line_index: &LineIndex, edits: &[AtomTex } } - lines + return_pos!(); } // for bench @@ -337,8 +366,9 @@ mod test { fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { let line_index = LineIndex::new(&x.text); let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); - let actual_lines = count_newlines(x.offset, &line_index, &x.edits); - assert_eq!(actual_lines, expected.line); + let actual = translate_offset_with_edit(&line_index, x.offset, &x.edits); + // assert_eq!(actual, expected); + assert_eq!(actual.line, expected.line); } } } From dc2afae991892719b97b0e4b40d8483b43b08680 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sat, 22 Dec 2018 20:52:43 +0100 Subject: [PATCH 12/18] fix arbitrary offset generation, col translation working --- crates/ra_editor/src/line_index.rs | 28 ++++++ crates/ra_editor/src/line_index_utils.rs | 103 ++++++++++++++--------- 2 files changed, 92 insertions(+), 39 deletions(-) diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index 6dbabd97e00..b01760313cb 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -62,6 +62,12 @@ impl LineIndex { curr_col += char_len; } + + // Save any utf-16 characters seen in the last line + if utf16_chars.len() > 0 { + utf16_lines.insert(line, utf16_chars); + } + LineIndex { newlines, utf16_lines, @@ -122,6 +128,28 @@ impl LineIndex { } } +// for bench and test +pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { + let mut res = LineCol { + line: 0, + col_utf16: 0, + }; + for (i, c) in text.char_indices() { + if i + c.len_utf8() > offset.to_usize() { + // if it's an invalid offset, inside a multibyte char + // return as if it was at the start of the char + break; + } + if c == '\n' { + res.line += 1; + res.col_utf16 = 0; + } else { + res.col_utf16 += 1; + } + } + res +} + #[test] fn test_line_index() { let text = "hello\nworld"; diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 382f3ac7200..913ca5b150f 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,6 +1,6 @@ use ra_text_edit::AtomTextEdit; use ra_syntax::{TextUnit, TextRange}; -use crate::{LineIndex, LineCol, line_index::Utf16Char}; +use crate::{LineIndex, LineCol, line_index::Utf16Char, line_index}; use superslice::Ext; #[derive(Debug, Clone)] @@ -154,7 +154,7 @@ impl<'a, 'b> Edits<'a, 'b> { fn next_step(&mut self, step: &Step) -> NextNewlines { let step_pos = match step { &Step::Newline(n) => n, - &Step::Utf16Char(r) => r.start(), + &Step::Utf16Char(r) => r.end(), }; let res = match &mut self.current { Some(edit) => { @@ -214,6 +214,40 @@ impl<'a, 'b> Edits<'a, 'b> { } } +#[derive(Debug)] +struct RunningLineCol { + line: u32, + last_newline: TextUnit, + col_adjust: TextUnit, +} + +impl RunningLineCol { + fn new() -> RunningLineCol { + RunningLineCol { + line: 0, + last_newline: TextUnit::from(0), + col_adjust: TextUnit::from(0), + } + } + + fn to_line_col(&self, offset: TextUnit) -> LineCol { + LineCol { + line: self.line, + col_utf16: ((offset - self.last_newline) - self.col_adjust).into(), + } + } + + fn add_line(&mut self, newline: TextUnit) { + self.line += 1; + self.last_newline = newline; + self.col_adjust = TextUnit::from(0); + } + + fn adjust_col(&mut self, range: &TextRange) { + self.col_adjust += range.len() - TextUnit::from(1); + } +} + pub fn translate_offset_with_edit( line_index: &LineIndex, offset: TextUnit, @@ -228,49 +262,35 @@ pub fn translate_offset_with_edit( let mut state = Edits::new(&sorted_edits); - let mut pos: LineCol = LineCol { - line: 0, - col_utf16: 0, - }; - - let mut last_newline: TextUnit = TextUnit::from(0); - let mut col_adjust: TextUnit = TextUnit::from(0); + let mut res = RunningLineCol::new(); macro_rules! test_step { ($x:ident) => { match &$x { Step::Newline(n) => { if offset < *n { - return_pos!(); + return res.to_line_col(offset); } else if offset == *n { - pos.line += 1; - pos.col_utf16 = 0; - return pos; + res.add_line(*n); + return res.to_line_col(offset); } else { - pos.line += 1; - pos.col_utf16 = 0; - last_newline = *n; - col_adjust = TextUnit::from(0); + res.add_line(*n); } } Step::Utf16Char(x) => { if offset < x.end() { - return_pos!(); + // if the offset is inside a multibyte char it's invalid + // clamp it to the start of the char + let clamp = offset.min(x.start()); + return res.to_line_col(clamp); } else { - col_adjust += x.len() - TextUnit::from(1); + res.adjust_col(x); } } } }; } - macro_rules! return_pos { - () => { - pos.col_utf16 = ((offset - last_newline) - col_adjust).into(); - return pos; - }; - } - for orig_step in LineIndexStepIter::from(line_index) { loop { let translated_step = state.translate_step(&orig_step); @@ -305,7 +325,7 @@ pub fn translate_offset_with_edit( } } - return_pos!(); + res.to_line_col(offset) } // for bench @@ -315,8 +335,7 @@ pub fn translate_after_edit( edits: Vec, ) -> LineCol { let text = edit_text(pre_edit_text, edits); - let line_index = LineIndex::new(&text); - line_index.line_col(offset) + line_index::to_line_col(&text, offset) } fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { @@ -343,6 +362,7 @@ mod test { #[derive(Debug)] struct ArbTextWithOffsetAndEdits { text: String, + edited_text: String, offset: TextUnit, edits: Vec, } @@ -350,13 +370,18 @@ mod test { fn arb_text_with_offset_and_edits() -> BoxedStrategy { arb_text() .prop_flat_map(|text| { - (arb_offset(&text), arb_edits(&text), Just(text)).prop_map( - |(offset, edits, text)| ArbTextWithOffsetAndEdits { - text, - offset, - edits, - }, - ) + (arb_edits(&text), Just(text)).prop_flat_map(|(edits, text)| { + let edited_text = edit_text(&text, edits.clone()); + let arb_offset = arb_offset(&edited_text); + (Just(text), Just(edited_text), Just(edits), arb_offset).prop_map( + |(text, edited_text, edits, offset)| ArbTextWithOffsetAndEdits { + text, + edits, + edited_text, + offset, + }, + ) + }) }) .boxed() } @@ -364,11 +389,11 @@ mod test { proptest! { #[test] fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { + let expected = line_index::to_line_col(&x.edited_text, x.offset); let line_index = LineIndex::new(&x.text); - let expected = translate_after_edit(&x.text, x.offset, x.edits.clone()); let actual = translate_offset_with_edit(&line_index, x.offset, &x.edits); - // assert_eq!(actual, expected); - assert_eq!(actual.line, expected.line); + + assert_eq!(actual, expected); } } } From c886b72dab918d6f4d3be025135f769bc429cc79 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sat, 22 Dec 2018 22:11:10 +0100 Subject: [PATCH 13/18] make criterion args work, small simplification --- crates/ra_editor/Cargo.toml | 4 +++ .../translate_offset_with_edit_benchmark.rs | 29 +++++++++---------- crates/ra_editor/src/line_index_utils.rs | 3 -- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/crates/ra_editor/Cargo.toml b/crates/ra_editor/Cargo.toml index 039688d7df9..7ed7526ec5b 100644 --- a/crates/ra_editor/Cargo.toml +++ b/crates/ra_editor/Cargo.toml @@ -23,6 +23,10 @@ rand = "*" rand_xorshift = "*" lazy_static = "*" +[lib] +# so that criterion arguments work, see: https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options +bench = false + [[bench]] name = "translate_offset_with_edit_benchmark" harness = false \ No newline at end of file diff --git a/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs b/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs index 89ed998235a..0f550fd39a2 100644 --- a/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs +++ b/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs @@ -65,21 +65,20 @@ lazy_static! { } fn compare_translates(c: &mut Criterion) { - let f1 = Fun::new("translate_after_edit", |b, _| { - b.iter(|| { - let d = &*DATA; - line_index_utils::translate_after_edit(&d.text, d.offset, d.edits.clone()); - }) - }); - - let f2 = Fun::new("translate_offset_with_edit", |b, _| { - b.iter(|| { - let d = &*DATA; - line_index_utils::translate_offset_with_edit(&d.line_index, d.offset, &d.edits); - }) - }); - - let functions = vec![f1, f2]; + let functions = vec![ + Fun::new("translate_after_edit", |b, _| { + b.iter(|| { + let d = &*DATA; + line_index_utils::translate_after_edit(&d.text, d.offset, d.edits.clone()); + }) + }), + Fun::new("translate_offset_with_edit", |b, _| { + b.iter(|| { + let d = &*DATA; + line_index_utils::translate_offset_with_edit(&d.line_index, d.offset, &d.edits); + }) + }), + ]; c.bench_functions("translate", functions, ()); } diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 913ca5b150f..632c962cc2a 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -270,9 +270,6 @@ pub fn translate_offset_with_edit( Step::Newline(n) => { if offset < *n { return res.to_line_col(offset); - } else if offset == *n { - res.add_line(*n); - return res.to_line_col(offset); } else { res.add_line(*n); } From aff0124b37e34910190498ad519deb1aea0d8451 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sun, 23 Dec 2018 14:01:36 +0100 Subject: [PATCH 14/18] add line_index proptest --- crates/ra_editor/src/line_index.rs | 234 +++++++++++++---------- crates/ra_editor/src/line_index_utils.rs | 4 +- 2 files changed, 132 insertions(+), 106 deletions(-) diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index b01760313cb..5304fbcf6f3 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -128,7 +128,8 @@ impl LineIndex { } } -// for bench and test +/// Simple reference implementation to use in proptests +/// and benchmarks as baseline pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { let mut res = LineCol { line: 0, @@ -150,111 +151,135 @@ pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { res } -#[test] -fn test_line_index() { - let text = "hello\nworld"; - let index = LineIndex::new(text); - assert_eq!( - index.line_col(0.into()), - LineCol { - line: 0, - col_utf16: 0 - } - ); - assert_eq!( - index.line_col(1.into()), - LineCol { - line: 0, - col_utf16: 1 - } - ); - assert_eq!( - index.line_col(5.into()), - LineCol { - line: 0, - col_utf16: 5 - } - ); - assert_eq!( - index.line_col(6.into()), - LineCol { - line: 1, - col_utf16: 0 - } - ); - assert_eq!( - index.line_col(7.into()), - LineCol { - line: 1, - col_utf16: 1 - } - ); - assert_eq!( - index.line_col(8.into()), - LineCol { - line: 1, - col_utf16: 2 - } - ); - assert_eq!( - index.line_col(10.into()), - LineCol { - line: 1, - col_utf16: 4 - } - ); - assert_eq!( - index.line_col(11.into()), - LineCol { - line: 1, - col_utf16: 5 - } - ); - assert_eq!( - index.line_col(12.into()), - LineCol { - line: 1, - col_utf16: 6 - } - ); +#[cfg(test)] +mod test_line_index { + use super::*; + use proptest::{prelude::*, proptest, proptest_helper}; + use ra_text_edit::test_utils::{arb_text, arb_offset}; - let text = "\nhello\nworld"; - let index = LineIndex::new(text); - assert_eq!( - index.line_col(0.into()), - LineCol { - line: 0, - col_utf16: 0 + #[test] + fn test_line_index() { + let text = "hello\nworld"; + let index = LineIndex::new(text); + assert_eq!( + index.line_col(0.into()), + LineCol { + line: 0, + col_utf16: 0 + } + ); + assert_eq!( + index.line_col(1.into()), + LineCol { + line: 0, + col_utf16: 1 + } + ); + assert_eq!( + index.line_col(5.into()), + LineCol { + line: 0, + col_utf16: 5 + } + ); + assert_eq!( + index.line_col(6.into()), + LineCol { + line: 1, + col_utf16: 0 + } + ); + assert_eq!( + index.line_col(7.into()), + LineCol { + line: 1, + col_utf16: 1 + } + ); + assert_eq!( + index.line_col(8.into()), + LineCol { + line: 1, + col_utf16: 2 + } + ); + assert_eq!( + index.line_col(10.into()), + LineCol { + line: 1, + col_utf16: 4 + } + ); + assert_eq!( + index.line_col(11.into()), + LineCol { + line: 1, + col_utf16: 5 + } + ); + assert_eq!( + index.line_col(12.into()), + LineCol { + line: 1, + col_utf16: 6 + } + ); + + let text = "\nhello\nworld"; + let index = LineIndex::new(text); + assert_eq!( + index.line_col(0.into()), + LineCol { + line: 0, + col_utf16: 0 + } + ); + assert_eq!( + index.line_col(1.into()), + LineCol { + line: 1, + col_utf16: 0 + } + ); + assert_eq!( + index.line_col(2.into()), + LineCol { + line: 1, + col_utf16: 1 + } + ); + assert_eq!( + index.line_col(6.into()), + LineCol { + line: 1, + col_utf16: 5 + } + ); + assert_eq!( + index.line_col(7.into()), + LineCol { + line: 2, + col_utf16: 0 + } + ); + } + + fn arb_text_with_offset() -> BoxedStrategy<(TextUnit, String)> { + arb_text() + .prop_flat_map(|text| (arb_offset(&text), Just(text))) + .boxed() + } + + proptest! { + #[test] + fn test_line_index_proptest((offset, text) in arb_text_with_offset()) { + let expected = to_line_col(&text, offset); + let line_index = LineIndex::new(&text); + let actual = line_index.line_col(offset); + + assert_eq!(actual, expected); } - ); - assert_eq!( - index.line_col(1.into()), - LineCol { - line: 1, - col_utf16: 0 - } - ); - assert_eq!( - index.line_col(2.into()), - LineCol { - line: 1, - col_utf16: 1 - } - ); - assert_eq!( - index.line_col(6.into()), - LineCol { - line: 1, - col_utf16: 5 - } - ); - assert_eq!( - index.line_col(7.into()), - LineCol { - line: 2, - col_utf16: 0 - } - ); + } } #[cfg(test)] @@ -349,4 +374,5 @@ const C: char = \"メ メ\"; assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15)); } + } diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 632c962cc2a..a0bb9a6ddc9 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -325,7 +325,7 @@ pub fn translate_offset_with_edit( res.to_line_col(offset) } -// for bench +/// Simplest implementation to use as reference in proptest and benchmarks pub fn translate_after_edit( pre_edit_text: &str, offset: TextUnit, @@ -352,8 +352,8 @@ fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { #[cfg(test)] mod test { - use proptest::{prelude::*, proptest, proptest_helper}; use super::*; + use proptest::{prelude::*, proptest, proptest_helper}; use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; #[derive(Debug)] From 6b2da4e5474ec064e44b7cf19523de1bab72ba27 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sun, 23 Dec 2018 15:49:14 +0100 Subject: [PATCH 15/18] use new translate_offset_with_edit for TryConvWith doc comments --- crates/ra_editor/src/lib.rs | 1 + crates/ra_editor/src/line_index_utils.rs | 2 +- crates/ra_lsp_server/src/conv.rs | 37 +----------------------- crates/ra_text_edit/src/lib.rs | 2 ++ 4 files changed, 5 insertions(+), 37 deletions(-) diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 7145c6cfbe6..2e3635ea0ea 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -14,6 +14,7 @@ pub use self::{ extend_selection::extend_selection, folding_ranges::{folding_ranges, Fold, FoldKind}, line_index::{LineCol, LineIndex}, + line_index_utils::translate_offset_with_edit, symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, typing::{join_lines, on_enter, on_eq_typed}, }; diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index a0bb9a6ddc9..ba3ac8aebd5 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,6 +1,6 @@ use ra_text_edit::AtomTextEdit; use ra_syntax::{TextUnit, TextRange}; -use crate::{LineIndex, LineCol, line_index::Utf16Char, line_index}; +use crate::{LineIndex, LineCol, line_index::{self, Utf16Char}}; use superslice::Ext; #[derive(Debug, Clone)] diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 051f1f995b3..63827aeea0c 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -3,7 +3,7 @@ use languageserver_types::{ TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat, }; use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition, CompletionItem, CompletionItemKind, InsertText}; -use ra_editor::{LineCol, LineIndex}; +use ra_editor::{LineCol, LineIndex, translate_offset_with_edit}; use ra_text_edit::{AtomTextEdit, TextEdit}; use ra_syntax::{SyntaxKind, TextRange, TextUnit}; @@ -261,41 +261,6 @@ impl TryConvWith for SourceChange { } } -// HACK: we should translate offset to line/column using linde_index *with edits applied*. -// A naive version of this function would be to apply `edits` to the original text, -// construct a new line index and use that, but it would be slow. -// -// Writing fast & correct version is issue #105, let's use a quick hack in the meantime -fn translate_offset_with_edit( - pre_edit_index: &LineIndex, - offset: TextUnit, - edits: &[AtomTextEdit], -) -> LineCol { - let fallback = pre_edit_index.line_col(offset); - let edit = match edits.first() { - None => return fallback, - Some(edit) => edit, - }; - let end_offset = edit.delete.start() + TextUnit::of_str(&edit.insert); - if !(edit.delete.start() <= offset && offset <= end_offset) { - return fallback; - } - let rel_offset = offset - edit.delete.start(); - let in_edit_line_col = LineIndex::new(&edit.insert).line_col(rel_offset); - let edit_line_col = pre_edit_index.line_col(edit.delete.start()); - if in_edit_line_col.line == 0 { - LineCol { - line: edit_line_col.line, - col_utf16: edit_line_col.col_utf16 + in_edit_line_col.col_utf16, - } - } else { - LineCol { - line: edit_line_col.line + in_edit_line_col.line, - col_utf16: in_edit_line_col.col_utf16, - } - } -} - impl TryConvWith for SourceFileEdit { type Ctx = ServerWorld; type Output = TextDocumentEdit; diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs index 89600413ad2..8acf1044849 100644 --- a/crates/ra_text_edit/src/lib.rs +++ b/crates/ra_text_edit/src/lib.rs @@ -6,8 +6,10 @@ pub use crate::text_edit::{TextEdit, TextEditBuilder}; use text_unit::{TextRange, TextUnit}; +/// Must not overlap with other `AtomTextEdit`s #[derive(Debug, Clone)] pub struct AtomTextEdit { + /// Refers to offsets in the original text pub delete: TextRange, pub insert: String, } From 863ed19946d6f707ce09dd77bf26b26be73e097c Mon Sep 17 00:00:00 2001 From: Bernardo Date: Mon, 24 Dec 2018 17:01:25 +0100 Subject: [PATCH 16/18] remove benchmark and simplify tests --- Cargo.lock | 178 ------------------ crates/ra_editor/Cargo.toml | 12 -- .../translate_offset_with_edit_benchmark.rs | 87 --------- crates/ra_editor/src/line_index.rs | 23 ++- crates/ra_editor/src/line_index_utils.rs | 63 ++----- crates/ra_text_edit/src/test_utils.rs | 74 ++------ crates/ra_text_edit/src/text_edit.rs | 15 +- 7 files changed, 70 insertions(+), 382 deletions(-) delete mode 100644 crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs diff --git a/Cargo.lock b/Cargo.lock index 1ee2df8ddb4..127d7772a71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,11 +122,6 @@ dependencies = [ "serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "cast" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "cc" version = "1.0.27" @@ -169,51 +164,6 @@ dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "criterion" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", - "criterion-plot 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", - "criterion-stats 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", - "csv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", - "failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", - "failure_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", - "handlebars 0.32.4 (registry+https://github.com/rust-lang/crates.io-index)", - "itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", - "itertools-num 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)", - "simplelog 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", - "walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "criterion-plot" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", - "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "criterion-stats" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", - "num_cpus 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", - "thread-scoped 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "crossbeam-channel" version = "0.2.6" @@ -283,23 +233,6 @@ dependencies = [ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "csv" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "csv-core 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "csv-core" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "derive-new" version = "0.5.6" @@ -435,21 +368,6 @@ name = "glob" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "handlebars" -version = "0.32.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "pest 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "pest_derive 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", - "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "heck" version = "0.3.1" @@ -500,14 +418,6 @@ dependencies = [ "either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "itertools-num" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "itoa" version = "0.4.3" @@ -686,11 +596,6 @@ name = "percent-encoding" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "pest" -version = "1.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "pest" version = "2.0.2" @@ -699,16 +604,6 @@ dependencies = [ "ucd-trie 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "pest_derive" -version = "1.0.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "pest 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "pest_derive" version = "2.0.1" @@ -770,11 +665,6 @@ name = "quick-error" version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "quote" -version = "0.3.15" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "quote" version = "0.6.10" @@ -831,15 +721,11 @@ dependencies = [ name = "ra_editor" version = "0.1.0" dependencies = [ - "criterion 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", "join_to_string 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)", "ra_syntax 0.1.0", "ra_text_edit 0.1.0", - "rand 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_xorshift 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "superslice 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "test_utils 0.1.0", @@ -1240,16 +1126,6 @@ dependencies = [ "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "simplelog" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "term 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "slug" version = "0.1.4" @@ -1289,16 +1165,6 @@ name = "superslice" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "syn" -version = "0.11.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", - "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "syn" version = "0.14.9" @@ -1319,14 +1185,6 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "synom" -version = "0.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "synstructure" version = "0.10.1" @@ -1391,15 +1249,6 @@ dependencies = [ "tera 0.11.20 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "term" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "termion" version = "1.5.1" @@ -1436,11 +1285,6 @@ dependencies = [ "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "thread-scoped" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "thread_local" version = "0.3.6" @@ -1569,11 +1413,6 @@ name = "unicode-width" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "unicode-xid" -version = "0.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "unicode-xid" version = "0.1.0" @@ -1688,15 +1527,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40" "checksum byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94f88df23a25417badc922ab0f5716cc1330e87f71ddd9203b3a3ccd9cedf75d" "checksum cargo_metadata 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e5d1b4d380e1bab994591a24c2bdd1b054f64b60bef483a8c598c7c345bc3bbe" -"checksum cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "926013f2860c46252efceabb19f4a6b308197505082c609025aa6706c011d427" "checksum cc 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "155ed195f7bd722d1dfeb30365b9d0c1f6a078fa7ca4014497e5935d90993d6f" "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" "checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" "checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" -"checksum criterion 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c47d2b548c5647e1a436dc0cb78d4ebf51b6bf7ab101ed76662828bdd4d3a24a" -"checksum criterion-plot 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6e649d6aacdbbdb94ec659561a309a71336fc5655ed408f3afd28df2fc0c4f4f" -"checksum criterion-stats 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ff43cac80562f91ead0b617c1be74edf350adfaa195809d355de98dfc8f9237d" "checksum crossbeam-channel 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7b85741761b7f160bc5e7e0c14986ef685b7f8bf9b7ad081c60c604bb4649827" "checksum crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f739f8c5363aca78cfb059edf753d8f0d36908c348f3d8d1503f03d8b75d9cf3" "checksum crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "927121f5407de9956180ff5e936fe3cf4324279280001cd56b669d28ee7e9150" @@ -1704,8 +1539,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2760899e32a1d58d5abb31129f8fae5de75220bc2176e77ff7c627ae45c918d9" "checksum crossbeam-utils 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "677d453a17e8bd2b913fa38e8b9cf04bcdbb5be790aa294f2389661d72036015" "checksum crossbeam-utils 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "41ee4864f4797060e52044376f7d107429ce1fb43460021b126424b7180ee21a" -"checksum csv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "9fd1c44c58078cfbeaf11fbb3eac9ae5534c23004ed770cc4bfb48e658ae4f04" -"checksum csv-core 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa5cdef62f37e6ffe7d1f07a381bc0db32b7a3ff1cac0de56cb0d81e71f53d65" "checksum derive-new 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "6ca414e896ae072546f4d789f452daaecf60ddee4c9df5dc6d5936d769e3d87c" "checksum deunicode 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690" "checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" @@ -1723,7 +1556,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" -"checksum handlebars 0.32.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d89ec99d1594f285d4590fc32bac5f75cdab383f1123d504d27862c644a807dd" "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" "checksum humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" "checksum id-arena 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3a7250033feafee46a1cecd2c2616a64aec1d064f38c9ae2bdd297728542843e" @@ -1731,7 +1563,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum im 12.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ae9c7f9bb8aee47fc16d535a705f7867a9fc83bb822e5e1043bb98e77ffeed3c" "checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d" "checksum itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0d47946d458e94a1b7bcabbf6521ea7c037062c81f534615abcad76e84d4970d" -"checksum itertools-num 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a872a22f9e6f7521ca557660adb96dd830e54f0f490fa115bb55dd69d38b27e7" "checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" "checksum join_to_string 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7bddc885f3fd69dd4b5d747c2efe6dd2c36d795ea9938281ed50910e32c95e31" "checksum languageserver-types 0.53.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a0be4f8f783832a308975035043c0e8ad18e11c3f3d395aa6f1a9f3a30b8e654" @@ -1755,16 +1586,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c" "checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9" "checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" -"checksum pest 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0fce5d8b5cc33983fc74f78ad552b5522ab41442c4ca91606e4236eb4b5ceefc" "checksum pest 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a677051ad923732bb5c70f2d45f8985a96e3eee2e2bff86697e3b11b0c3fcfde" -"checksum pest_derive 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3294f437119209b084c797604295f40227cffa35c57220b1e99a6ff3bf8ee4" "checksum pest_derive 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b76f477146419bc539a63f4ef40e902166cb43b3e51cecc71d9136fd12c567e7" "checksum pest_generator 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ebee4e9680be4fd162e6f3394ae4192a6b60b1e4d17d845e631f0c68d1a3386" "checksum pest_meta 2.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1f6d5f6f0e6082578c86af197d780dc38328e3f768cec06aac9bc46d714e8221" "checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09" "checksum proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)" = "926d0604475349f463fe44130aae73f2294b5309ab2ca0310b998bd334ef191f" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" -"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c" "checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd" "checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c" @@ -1801,27 +1629,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde_derive 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)" = "96a7f9496ac65a2db5929afa087b54f8fc5008dcfbe48a8874ed20049b0d6154" "checksum serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)" = "c37ccd6be3ed1fdf419ee848f7c758eb31b054d7cd3ae3600e3bae0adf569811" "checksum sha-1 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "51b9d1f3b5de8a167ab06834a7c883bd197f2191e1dda1a22d9ccfeedbf9aded" -"checksum simplelog 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e95345f185d5adeb8ec93459d2dc99654e294cc6ccf5b75414d8ea262de9a13" "checksum slug 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b3bc762e6a4b6c6fcaade73e77f9ebc6991b676f88bb2358bddb56560f073373" "checksum smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b73ea3738b47563803ef814925e69be00799a8c07420be8b996f8e98fb2336db" "checksum smol_str 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "486a74e9b9fc53373808f7a17e10fc728adcb1fbe272292271d8bea61175e181" "checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum superslice 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b50b13d42370e0f5fc62eafdd5c2d20065eaf5458dab215ff3e20e63eea96b30" -"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)" = "261ae9ecaa397c42b960649561949d69311f08eeaea86a65696e6e46517cf741" "checksum syn 0.15.23 (registry+https://github.com/rust-lang/crates.io-index)" = "9545a6a093a3f0bd59adb472700acc08cad3776f860f16a897dfce8c88721cbc" -"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" "checksum tempfile 3.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "7e91405c14320e5c79b3d148e1c86f40749a36e490642202a31689cb1a3452b2" "checksum tera 0.11.20 (registry+https://github.com/rust-lang/crates.io-index)" = "4b505279e19d8f7d24b1a9dc58327c9c36174b1a2c7ebdeac70792d017cb64f3" "checksum teraron 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0d89ad4617d1dec55331067fadaa041e813479e1779616f3d3ce9308bf46184e" -"checksum term 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5e6b677dd1e8214ea1ef4297f85dbcbed8e8cdddb561040cc998ca2551c37561" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8009d7bdbd896a7e09b595f8f9325a19047fc708653e60d0895202b82135048f" "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" -"checksum thread-scoped 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bcbb6aa301e5d3b0b5ef639c9a9c7e2f1c944f177b460c04dc24c69b1fa2bd99" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f0c90a5f3459330ac8bc0d2f879c693bb7a2f59689c1083fc4ef83834da865" "checksum time 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "847da467bf0db05882a9e2375934a8a55cffdc9db0d128af1518200260ba1f6c" @@ -1838,7 +1661,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum unicode-normalization 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "6a0180bc61fc5a987082bfa111f4cc95c4caff7f9799f3e46df09163a937aa25" "checksum unicode-segmentation 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "aa6024fc12ddfd1c6dbc14a80fa2324d4568849869b779f6bd37e5e4c03344d1" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" -"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" "checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" diff --git a/crates/ra_editor/Cargo.toml b/crates/ra_editor/Cargo.toml index 7ed7526ec5b..1ad99af2806 100644 --- a/crates/ra_editor/Cargo.toml +++ b/crates/ra_editor/Cargo.toml @@ -18,15 +18,3 @@ proptest = "0.8.7" [dev-dependencies] test_utils = { path = "../test_utils" } -criterion = "0.2" -rand = "*" -rand_xorshift = "*" -lazy_static = "*" - -[lib] -# so that criterion arguments work, see: https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options -bench = false - -[[bench]] -name = "translate_offset_with_edit_benchmark" -harness = false \ No newline at end of file diff --git a/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs b/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs deleted file mode 100644 index 0f550fd39a2..00000000000 --- a/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs +++ /dev/null @@ -1,87 +0,0 @@ -use criterion::{criterion_group, criterion_main}; -use criterion::Criterion; -use criterion::Fun; -use ra_text_edit::AtomTextEdit; -use ra_text_edit::test_utils::{arb_edits_custom, arb_offset}; -use ra_editor::line_index_utils; -use ra_editor::LineIndex; -use ra_syntax::TextUnit; -use proptest::test_runner; -use proptest::string::string_regex; -use proptest::strategy::{Strategy, ValueTree}; -use rand_xorshift::XorShiftRng; -use rand::SeedableRng; -use lazy_static::lazy_static; - -#[derive(Debug)] -struct Data { - text: String, - line_index: LineIndex, - edits: Vec, - offset: TextUnit, -} - -fn setup_data() -> Data { - let mut runner = test_runner::TestRunner::default(); - { - struct TestRng { - rng: XorShiftRng, - } - // HACK to be able to manually seed the TestRunner - let rng: &mut TestRng = unsafe { std::mem::transmute(runner.rng()) }; - rng.rng = XorShiftRng::seed_from_u64(0); - } - - let text = { - let arb = string_regex("([a-zA-Z_0-9]{10,50}.{1,5}\n){100,500}").unwrap(); - let tree = arb.new_tree(&mut runner).unwrap(); - tree.current() - }; - - let edits = { - let arb = arb_edits_custom(&text, 99, 100); - let tree = arb.new_tree(&mut runner).unwrap(); - tree.current() - }; - - let offset = { - let arb = arb_offset(&text); - let tree = arb.new_tree(&mut runner).unwrap(); - tree.current() - }; - - let line_index = LineIndex::new(&text); - - Data { - text, - line_index, - edits, - offset, - } -} - -lazy_static! { - static ref DATA: Data = setup_data(); -} - -fn compare_translates(c: &mut Criterion) { - let functions = vec![ - Fun::new("translate_after_edit", |b, _| { - b.iter(|| { - let d = &*DATA; - line_index_utils::translate_after_edit(&d.text, d.offset, d.edits.clone()); - }) - }), - Fun::new("translate_offset_with_edit", |b, _| { - b.iter(|| { - let d = &*DATA; - line_index_utils::translate_offset_with_edit(&d.line_index, d.offset, &d.edits); - }) - }), - ]; - - c.bench_functions("translate", functions, ()); -} - -criterion_group!(benches, compare_translates); -criterion_main!(benches); diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index 5304fbcf6f3..898fee7e00e 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -128,8 +128,8 @@ impl LineIndex { } } +#[cfg(test)] /// Simple reference implementation to use in proptests -/// and benchmarks as baseline pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { let mut res = LineCol { line: 0, @@ -270,6 +270,27 @@ mod test_line_index { .boxed() } + fn to_line_col(text: &str, offset: TextUnit) -> LineCol { + let mut res = LineCol { + line: 0, + col_utf16: 0, + }; + for (i, c) in text.char_indices() { + if i + c.len_utf8() > offset.to_usize() { + // if it's an invalid offset, inside a multibyte char + // return as if it was at the start of the char + break; + } + if c == '\n' { + res.line += 1; + res.col_utf16 = 0; + } else { + res.col_utf16 += 1; + } + } + res + } + proptest! { #[test] 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 ba3ac8aebd5..b8b149442b0 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,6 +1,6 @@ use ra_text_edit::AtomTextEdit; use ra_syntax::{TextUnit, TextRange}; -use crate::{LineIndex, LineCol, line_index::{self, Utf16Char}}; +use crate::{LineIndex, LineCol, line_index::Utf16Char}; use superslice::Ext; #[derive(Debug, Clone)] @@ -325,59 +325,34 @@ pub fn translate_offset_with_edit( res.to_line_col(offset) } -/// Simplest implementation to use as reference in proptest and benchmarks -pub fn translate_after_edit( - pre_edit_text: &str, - offset: TextUnit, - edits: Vec, -) -> LineCol { - let text = edit_text(pre_edit_text, edits); - line_index::to_line_col(&text, offset) -} - -fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { - // apply edits ordered from last to first - // since they should not overlap we can just use start() - edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize)); - - let mut text = pre_edit_text.to_owned(); - - for edit in &edits { - let range = edit.delete.start().to_usize()..edit.delete.end().to_usize(); - text.replace_range(range, &edit.insert); - } - - text -} - #[cfg(test)] mod test { use super::*; use proptest::{prelude::*, proptest, proptest_helper}; - use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; + use crate::line_index; + use ra_text_edit::test_utils::{arb_offset, arb_text_with_edits}; + use ra_text_edit::TextEdit; #[derive(Debug)] struct ArbTextWithOffsetAndEdits { text: String, + edits: TextEdit, edited_text: String, offset: TextUnit, - edits: Vec, } - fn arb_text_with_offset_and_edits() -> BoxedStrategy { - arb_text() - .prop_flat_map(|text| { - (arb_edits(&text), Just(text)).prop_flat_map(|(edits, text)| { - let edited_text = edit_text(&text, edits.clone()); - let arb_offset = arb_offset(&edited_text); - (Just(text), Just(edited_text), Just(edits), arb_offset).prop_map( - |(text, edited_text, edits, offset)| ArbTextWithOffsetAndEdits { - text, - edits, - edited_text, - offset, - }, - ) + fn arb_text_with_edits_and_offset() -> BoxedStrategy { + arb_text_with_edits() + .prop_flat_map(|x| { + let edited_text = x.edits.apply(&x.text); + let arb_offset = arb_offset(&edited_text); + (Just(x), Just(edited_text), arb_offset).prop_map(|(x, edited_text, offset)| { + ArbTextWithOffsetAndEdits { + text: x.text, + edits: x.edits, + edited_text, + offset, + } }) }) .boxed() @@ -385,10 +360,10 @@ mod test { proptest! { #[test] - fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { + fn test_translate_offset_with_edit(x in arb_text_with_edits_and_offset()) { let expected = line_index::to_line_col(&x.edited_text, x.offset); let line_index = LineIndex::new(&x.text); - let actual = translate_offset_with_edit(&line_index, x.offset, &x.edits); + let actual = translate_offset_with_edit(&line_index, x.offset, x.edits.as_atoms()); assert_eq!(actual, expected); } diff --git a/crates/ra_text_edit/src/test_utils.rs b/crates/ra_text_edit/src/test_utils.rs index f150288f6ac..f0b8dfde125 100644 --- a/crates/ra_text_edit/src/test_utils.rs +++ b/crates/ra_text_edit/src/test_utils.rs @@ -1,6 +1,6 @@ use proptest::prelude::*; use text_unit::{TextUnit, TextRange}; -use crate::AtomTextEdit; +use crate::{AtomTextEdit, TextEdit}; pub fn arb_text() -> proptest::string::RegexGeneratorStrategy { // generate multiple newlines @@ -23,11 +23,7 @@ pub fn arb_offset(text: &str) -> BoxedStrategy { } } -pub fn arb_edits(text: &str) -> BoxedStrategy> { - arb_edits_custom(&text, 0, 7) -} - -pub fn arb_edits_custom(text: &str, min: usize, max: usize) -> BoxedStrategy> { +pub fn arb_text_edit(text: &str) -> BoxedStrategy { if text.is_empty() { // only valid edits return Just(vec![]) @@ -37,14 +33,14 @@ pub fn arb_edits_custom(text: &str, min: usize, max: usize) -> BoxedStrategy = cuts .chunks(2) @@ -68,52 +64,22 @@ pub fn arb_edits_custom(text: &str, min: usize, max: usize) -> BoxedStrategy BoxedStrategy<(String, Vec)> { - let text = arb_text(); - text.prop_flat_map(|s| { - let edits = arb_edits(&s); - (Just(s), edits) - }) - .boxed() - } - - fn intersect(r1: TextRange, r2: TextRange) -> Option { - let start = r1.start().max(r2.start()); - let end = r1.end().min(r2.end()); - if start <= end { - Some(TextRange::from_to(start, end)) - } else { - None - } - } - proptest! { - #[test] - fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) { - proptest_atom_text_edits_are_valid(text, edits) - } - } - - fn proptest_atom_text_edits_are_valid(text: String, edits: Vec) { - // slicing doesn't panic - for e in &edits { - let _ = &text[e.delete]; - } - // ranges do not overlap - for i in 1..edits.len() { - let e1 = &edits[i]; - for e2 in &edits[0..i] { - if intersect(e1.delete, e2.delete).is_some() { - assert!(false, "Overlapping ranges {} {}", e1.delete, e2.delete); - } - } - } - } +#[derive(Debug, Clone)] +pub struct ArbTextWithEdits { + pub text: String, + pub edits: TextEdit, +} + +pub fn arb_text_with_edits() -> BoxedStrategy { + let text = arb_text(); + text.prop_flat_map(|s| { + let edits = arb_text_edit(&s); + (Just(s), edits) + }) + .prop_map(|(text, edits)| ArbTextWithEdits { text, edits }) + .boxed() } diff --git a/crates/ra_text_edit/src/text_edit.rs b/crates/ra_text_edit/src/text_edit.rs index 392968d638f..0881f3e1cea 100644 --- a/crates/ra_text_edit/src/text_edit.rs +++ b/crates/ra_text_edit/src/text_edit.rs @@ -26,12 +26,7 @@ impl TextEditBuilder { self.atoms.push(AtomTextEdit::insert(offset, text)) } pub fn finish(self) -> TextEdit { - let mut atoms = self.atoms; - atoms.sort_by_key(|a| (a.delete.start(), a.delete.end())); - for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) { - assert!(a1.delete.end() <= a2.delete.start()) - } - TextEdit { atoms } + TextEdit::from_atoms(self.atoms) } pub fn invalidates_offset(&self, offset: TextUnit) -> bool { self.atoms @@ -41,6 +36,14 @@ impl TextEditBuilder { } impl TextEdit { + pub(crate) fn from_atoms(mut atoms: Vec) -> TextEdit { + atoms.sort_by_key(|a| (a.delete.start(), a.delete.end())); + for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) { + assert!(a1.delete.end() <= a2.delete.start()) + } + TextEdit { atoms } + } + pub fn as_atoms(&self) -> &[AtomTextEdit] { &self.atoms } From e9c186e48a77b536053c0f75ac9ea5b2fd322cfa Mon Sep 17 00:00:00 2001 From: Bernardo Date: Tue, 25 Dec 2018 20:49:18 +0100 Subject: [PATCH 17/18] change to `TextEdit` to avoid allocation and sort rename newline to step where applicable --- crates/ra_editor/src/line_index_utils.rs | 86 +++++++++++------------- crates/ra_lsp_server/src/conv.rs | 10 +-- crates/ra_text_edit/src/test_utils.rs | 12 ++-- 3 files changed, 51 insertions(+), 57 deletions(-) diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index b8b149442b0..ec3269bbbab 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -1,7 +1,6 @@ -use ra_text_edit::AtomTextEdit; +use ra_text_edit::{AtomTextEdit, TextEdit}; use ra_syntax::{TextUnit, TextRange}; use crate::{LineIndex, LineCol, line_index::Utf16Char}; -use superslice::Ext; #[derive(Debug, Clone)] enum Step { @@ -55,12 +54,12 @@ impl<'a> Iterator for LineIndexStepIter<'a> { } #[derive(Debug)] -struct OffsetNewlineIter<'a> { +struct OffsetStepIter<'a> { text: &'a str, offset: TextUnit, } -impl<'a> Iterator for OffsetNewlineIter<'a> { +impl<'a> Iterator for OffsetStepIter<'a> { type Item = Step; fn next(&mut self) -> Option { let (next, next_offset) = self @@ -93,10 +92,10 @@ impl<'a> Iterator for OffsetNewlineIter<'a> { } #[derive(Debug)] -enum NextNewlines<'a> { +enum NextSteps<'a> { Use, - ReplaceMany(OffsetNewlineIter<'a>), - AddMany(OffsetNewlineIter<'a>), + ReplaceMany(OffsetStepIter<'a>), + AddMany(OffsetStepIter<'a>), } #[derive(Debug)] @@ -106,16 +105,16 @@ struct TranslatedEdit<'a> { diff: i64, } -struct Edits<'a, 'b> { - edits: &'b [&'a AtomTextEdit], +struct Edits<'a> { + edits: &'a [AtomTextEdit], current: Option>, acc_diff: i64, } -impl<'a, 'b> Edits<'a, 'b> { - fn new(sorted_edits: &'b [&'a AtomTextEdit]) -> Edits<'a, 'b> { +impl<'a> Edits<'a> { + fn from_text_edit(text_edit: &'a TextEdit) -> Edits<'a> { let mut x = Edits { - edits: sorted_edits, + edits: text_edit.as_atoms(), current: None, acc_diff: 0, }; @@ -141,9 +140,9 @@ impl<'a, 'b> Edits<'a, 'b> { } } - fn next_inserted_newlines(&mut self) -> Option> { + fn next_inserted_steps(&mut self) -> Option> { let cur = self.current.as_ref()?; - let res = Some(OffsetNewlineIter { + let res = Some(OffsetStepIter { offset: cur.delete.start(), text: &cur.insert, }); @@ -151,7 +150,7 @@ impl<'a, 'b> Edits<'a, 'b> { res } - fn next_step(&mut self, step: &Step) -> NextNewlines { + fn next_steps(&mut self, step: &Step) -> NextSteps { let step_pos = match step { &Step::Newline(n) => n, &Step::Utf16Char(r) => r.end(), @@ -159,27 +158,27 @@ impl<'a, 'b> Edits<'a, 'b> { let res = match &mut self.current { Some(edit) => { if step_pos <= edit.delete.start() { - NextNewlines::Use + NextSteps::Use } else if step_pos <= edit.delete.end() { - let iter = OffsetNewlineIter { + let iter = OffsetStepIter { offset: edit.delete.start(), text: &edit.insert, }; - // empty slice + // empty slice to avoid returning steps again edit.insert = &edit.insert[edit.insert.len()..]; - NextNewlines::ReplaceMany(iter) + NextSteps::ReplaceMany(iter) } else { - let iter = OffsetNewlineIter { + let iter = OffsetStepIter { offset: edit.delete.start(), text: &edit.insert, }; - // empty slice + // empty slice to avoid returning steps again edit.insert = &edit.insert[edit.insert.len()..]; self.advance_edit(); - NextNewlines::AddMany(iter) + NextSteps::AddMany(iter) } } - None => NextNewlines::Use, + None => NextSteps::Use, }; res } @@ -251,16 +250,9 @@ impl RunningLineCol { pub fn translate_offset_with_edit( line_index: &LineIndex, offset: TextUnit, - edits: &[AtomTextEdit], + text_edit: &TextEdit, ) -> LineCol { - let mut sorted_edits: Vec<&AtomTextEdit> = Vec::with_capacity(edits.len()); - for edit in edits { - let insert_index = - sorted_edits.upper_bound_by_key(&edit.delete.start(), |x| x.delete.start()); - sorted_edits.insert(insert_index, &edit); - } - - let mut state = Edits::new(&sorted_edits); + let mut state = Edits::from_text_edit(&text_edit); let mut res = RunningLineCol::new(); @@ -291,18 +283,18 @@ pub fn translate_offset_with_edit( for orig_step in LineIndexStepIter::from(line_index) { loop { let translated_step = state.translate_step(&orig_step); - match state.next_step(&translated_step) { - NextNewlines::Use => { + match state.next_steps(&translated_step) { + NextSteps::Use => { test_step!(translated_step); break; } - NextNewlines::ReplaceMany(ns) => { + NextSteps::ReplaceMany(ns) => { for n in ns { test_step!(n); } break; } - NextNewlines::AddMany(ns) => { + NextSteps::AddMany(ns) => { for n in ns { test_step!(n); } @@ -312,7 +304,7 @@ pub fn translate_offset_with_edit( } loop { - match state.next_inserted_newlines() { + match state.next_inserted_steps() { None => break, Some(ns) => { for n in ns { @@ -330,26 +322,26 @@ mod test { use super::*; use proptest::{prelude::*, proptest, proptest_helper}; use crate::line_index; - use ra_text_edit::test_utils::{arb_offset, arb_text_with_edits}; + use ra_text_edit::test_utils::{arb_offset, arb_text_with_edit}; use ra_text_edit::TextEdit; #[derive(Debug)] - struct ArbTextWithOffsetAndEdits { + struct ArbTextWithEditAndOffset { text: String, - edits: TextEdit, + edit: TextEdit, edited_text: String, offset: TextUnit, } - fn arb_text_with_edits_and_offset() -> BoxedStrategy { - arb_text_with_edits() + fn arb_text_with_edit_and_offset() -> BoxedStrategy { + arb_text_with_edit() .prop_flat_map(|x| { - let edited_text = x.edits.apply(&x.text); + let edited_text = x.edit.apply(&x.text); let arb_offset = arb_offset(&edited_text); (Just(x), Just(edited_text), arb_offset).prop_map(|(x, edited_text, offset)| { - ArbTextWithOffsetAndEdits { + ArbTextWithEditAndOffset { text: x.text, - edits: x.edits, + edit: x.edit, edited_text, offset, } @@ -360,10 +352,10 @@ mod test { proptest! { #[test] - fn test_translate_offset_with_edit(x in arb_text_with_edits_and_offset()) { + fn test_translate_offset_with_edit(x in arb_text_with_edit_and_offset()) { let expected = line_index::to_line_col(&x.edited_text, x.offset); let line_index = LineIndex::new(&x.text); - let actual = translate_offset_with_edit(&line_index, x.offset, x.edits.as_atoms()); + let actual = translate_offset_with_edit(&line_index, x.offset, &x.edit); assert_eq!(actual, expected); } diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 63827aeea0c..5a911d9d266 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -235,13 +235,15 @@ impl TryConvWith for SourceChange { None => None, Some(pos) => { let line_index = world.analysis().file_line_index(pos.file_id); - let edits = self + let edit = self .source_file_edits .iter() .find(|it| it.file_id == pos.file_id) - .map(|it| it.edit.as_atoms()) - .unwrap_or(&[]); - let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits); + .map(|it| &it.edit); + let line_col = match edit { + Some(edit) => translate_offset_with_edit(&*line_index, pos.offset, edit), + None => line_index.line_col(pos.offset), + }; let position = Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16)); Some(TextDocumentPositionParams { diff --git a/crates/ra_text_edit/src/test_utils.rs b/crates/ra_text_edit/src/test_utils.rs index f0b8dfde125..745f21c9318 100644 --- a/crates/ra_text_edit/src/test_utils.rs +++ b/crates/ra_text_edit/src/test_utils.rs @@ -69,17 +69,17 @@ pub fn arb_text_edit(text: &str) -> BoxedStrategy { } #[derive(Debug, Clone)] -pub struct ArbTextWithEdits { +pub struct ArbTextWithEdit { pub text: String, - pub edits: TextEdit, + pub edit: TextEdit, } -pub fn arb_text_with_edits() -> BoxedStrategy { +pub fn arb_text_with_edit() -> BoxedStrategy { let text = arb_text(); text.prop_flat_map(|s| { - let edits = arb_text_edit(&s); - (Just(s), edits) + let edit = arb_text_edit(&s); + (Just(s), edit) }) - .prop_map(|(text, edits)| ArbTextWithEdits { text, edits }) + .prop_map(|(text, edit)| ArbTextWithEdit { text, edit }) .boxed() } From 1cda43aafd623b400f5916b1d3727b56c136081b Mon Sep 17 00:00:00 2001 From: Bernardo Date: Tue, 25 Dec 2018 21:26:36 +0100 Subject: [PATCH 18/18] test code and dependency cleanup --- crates/ra_editor/Cargo.toml | 3 +-- crates/ra_editor/src/lib.rs | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/ra_editor/Cargo.toml b/crates/ra_editor/Cargo.toml index 1ad99af2806..f39fe4af66c 100644 --- a/crates/ra_editor/Cargo.toml +++ b/crates/ra_editor/Cargo.toml @@ -14,7 +14,6 @@ rustc-hash = "1.0" ra_syntax = { path = "../ra_syntax" } ra_text_edit = { path = "../ra_text_edit" } -proptest = "0.8.7" - [dev-dependencies] test_utils = { path = "../test_utils" } +proptest = "0.8.7" diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 2e3635ea0ea..619497f0b4b 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -2,8 +2,7 @@ mod code_actions; mod extend_selection; mod folding_ranges; mod line_index; -// public for benchmarkig -pub mod line_index_utils; +mod line_index_utils; mod symbols; #[cfg(test)] mod test_utils;