diff options
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | crates/ra_lsp_server/Cargo.toml | 4 | ||||
-rw-r--r-- | crates/ra_lsp_server/src/conv.rs | 61 | ||||
-rw-r--r-- | crates/ra_text_edit/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ra_text_edit/src/lib.rs | 1 | ||||
-rw-r--r-- | 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 3a3793581..a63eac57a 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -763,6 +763,7 @@ dependencies = [ | |||
763 | "languageserver-types 0.53.0 (registry+https://github.com/rust-lang/crates.io-index)", | 763 | "languageserver-types 0.53.0 (registry+https://github.com/rust-lang/crates.io-index)", |
764 | "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", | 764 | "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", |
765 | "parking_lot 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", | 765 | "parking_lot 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", |
766 | "proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)", | ||
766 | "ra_analysis 0.1.0", | 767 | "ra_analysis 0.1.0", |
767 | "ra_editor 0.1.0", | 768 | "ra_editor 0.1.0", |
768 | "ra_syntax 0.1.0", | 769 | "ra_syntax 0.1.0", |
diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml index 3c8c240cd..d73ff158f 100644 --- a/crates/ra_lsp_server/Cargo.toml +++ b/crates/ra_lsp_server/Cargo.toml | |||
@@ -34,6 +34,8 @@ ra_analysis = { path = "../ra_analysis" } | |||
34 | gen_lsp_server = { path = "../gen_lsp_server" } | 34 | gen_lsp_server = { path = "../gen_lsp_server" } |
35 | ra_vfs = { path = "../ra_vfs" } | 35 | ra_vfs = { path = "../ra_vfs" } |
36 | 36 | ||
37 | proptest = "0.8.7" | ||
38 | |||
37 | [dev-dependencies] | 39 | [dev-dependencies] |
38 | tempdir = "0.3.7" | 40 | tempdir = "0.3.7" |
39 | test_utils = { path = "../test_utils" } | 41 | 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 051f1f995..1db499175 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs | |||
@@ -386,3 +386,64 @@ where | |||
386 | self.iter.next().map(|item| item.conv_with(self.ctx)) | 386 | self.iter.next().map(|item| item.conv_with(self.ctx)) |
387 | } | 387 | } |
388 | } | 388 | } |
389 | |||
390 | #[cfg(test)] | ||
391 | mod test { | ||
392 | use proptest::{prelude::*, proptest, proptest_helper}; | ||
393 | use super::*; | ||
394 | use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; | ||
395 | |||
396 | #[derive(Debug)] | ||
397 | struct ArbTextWithOffsetAndEdits { | ||
398 | text: String, | ||
399 | offset: TextUnit, | ||
400 | edits: Vec<AtomTextEdit>, | ||
401 | } | ||
402 | |||
403 | fn arb_text_with_offset_and_edits() -> BoxedStrategy<ArbTextWithOffsetAndEdits> { | ||
404 | arb_text() | ||
405 | .prop_flat_map(|text| { | ||
406 | (arb_offset(&text), arb_edits(&text), Just(text)).prop_map( | ||
407 | |(offset, edits, text)| ArbTextWithOffsetAndEdits { | ||
408 | text, | ||
409 | offset, | ||
410 | edits, | ||
411 | }, | ||
412 | ) | ||
413 | }) | ||
414 | .boxed() | ||
415 | } | ||
416 | |||
417 | fn translate_offset_with_edit_naive( | ||
418 | pre_edit_text: String, | ||
419 | offset: TextUnit, | ||
420 | edits: &[AtomTextEdit], | ||
421 | ) -> LineCol { | ||
422 | // apply edits ordered from last to first | ||
423 | // since they should not overlap we can just use start() | ||
424 | let mut edits: Vec<AtomTextEdit> = edits.to_vec(); | ||
425 | edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize)); | ||
426 | |||
427 | let mut text = pre_edit_text; | ||
428 | |||
429 | for edit in &edits { | ||
430 | let range = edit.delete.start().to_usize()..edit.delete.end().to_usize(); | ||
431 | text.replace_range(range, &edit.insert); | ||
432 | } | ||
433 | |||
434 | let line_index = LineIndex::new(&text); | ||
435 | |||
436 | line_index.line_col(offset) | ||
437 | } | ||
438 | |||
439 | proptest! { | ||
440 | #[test] | ||
441 | fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { | ||
442 | if x.edits.len() <= 1 { | ||
443 | let expected = translate_offset_with_edit_naive(x.text.clone(), x.offset, &x.edits); | ||
444 | let actual = translate_offset_with_edit(&LineIndex::new(&x.text), x.offset, &x.edits); | ||
445 | assert_eq!(actual, expected); | ||
446 | } | ||
447 | } | ||
448 | } | ||
449 | } | ||
diff --git a/crates/ra_text_edit/Cargo.toml b/crates/ra_text_edit/Cargo.toml index 67dfd0924..e0db49688 100644 --- a/crates/ra_text_edit/Cargo.toml +++ b/crates/ra_text_edit/Cargo.toml | |||
@@ -7,7 +7,7 @@ publish = false | |||
7 | 7 | ||
8 | [dependencies] | 8 | [dependencies] |
9 | text_unit = "0.1.5" | 9 | text_unit = "0.1.5" |
10 | proptest = "0.8.7" | ||
10 | 11 | ||
11 | [dev-dependencies] | 12 | [dev-dependencies] |
12 | test_utils = { path = "../test_utils" } | 13 | test_utils = { path = "../test_utils" } |
13 | proptest = "0.8.7" | ||
diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs index 0a407795e..89600413a 100644 --- a/crates/ra_text_edit/src/lib.rs +++ b/crates/ra_text_edit/src/lib.rs | |||
@@ -1,6 +1,5 @@ | |||
1 | mod text_edit; | 1 | mod text_edit; |
2 | pub mod text_utils; | 2 | pub mod text_utils; |
3 | #[cfg(test)] | ||
4 | pub mod test_utils; | 3 | pub mod test_utils; |
5 | 4 | ||
6 | pub use crate::text_edit::{TextEdit, TextEditBuilder}; | 5 | 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 29301bff3..4a0ebc08e 100644 --- a/crates/ra_text_edit/src/test_utils.rs +++ b/crates/ra_text_edit/src/test_utils.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use proptest::{prelude::*, proptest, proptest_helper}; | 1 | use proptest::prelude::*; |
2 | use text_unit::{TextUnit, TextRange}; | 2 | use text_unit::{TextUnit, TextRange}; |
3 | use crate::AtomTextEdit; | 3 | use crate::AtomTextEdit; |
4 | 4 | ||
@@ -66,42 +66,48 @@ pub fn arb_edits(text: &str) -> BoxedStrategy<Vec<AtomTextEdit>> { | |||
66 | .boxed() | 66 | .boxed() |
67 | } | 67 | } |
68 | 68 | ||
69 | fn arb_text_with_edits() -> BoxedStrategy<(String, Vec<AtomTextEdit>)> { | 69 | #[cfg(test)] |
70 | let text = arb_text(); | 70 | mod tests { |
71 | text.prop_flat_map(|s| { | 71 | use super::*; |
72 | let edits = arb_edits(&s); | 72 | use proptest::{proptest, proptest_helper}; |
73 | (Just(s), edits) | ||
74 | }) | ||
75 | .boxed() | ||
76 | } | ||
77 | 73 | ||
78 | fn intersect(r1: TextRange, r2: TextRange) -> Option<TextRange> { | 74 | fn arb_text_with_edits() -> BoxedStrategy<(String, Vec<AtomTextEdit>)> { |
79 | let start = r1.start().max(r2.start()); | 75 | let text = arb_text(); |
80 | let end = r1.end().min(r2.end()); | 76 | text.prop_flat_map(|s| { |
81 | if start <= end { | 77 | let edits = arb_edits(&s); |
82 | Some(TextRange::from_to(start, end)) | 78 | (Just(s), edits) |
83 | } else { | 79 | }) |
84 | None | 80 | .boxed() |
85 | } | 81 | } |
86 | } | ||
87 | 82 | ||
88 | proptest! { | 83 | fn intersect(r1: TextRange, r2: TextRange) -> Option<TextRange> { |
89 | #[test] | 84 | let start = r1.start().max(r2.start()); |
90 | fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) { | 85 | let end = r1.end().min(r2.end()); |
91 | proptest_atom_text_edits_are_valid(text, edits) | 86 | if start <= end { |
87 | Some(TextRange::from_to(start, end)) | ||
88 | } else { | ||
89 | None | ||
90 | } | ||
92 | } | 91 | } |
93 | } | 92 | proptest! { |
94 | 93 | #[test] | |
95 | fn proptest_atom_text_edits_are_valid(text: String, edits: Vec<AtomTextEdit>) { | 94 | fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) { |
96 | // slicing doesn't panic | 95 | proptest_atom_text_edits_are_valid(text, edits) |
97 | for e in &edits { | 96 | } |
98 | let _ = &text[e.delete]; | ||
99 | } | 97 | } |
100 | // ranges do not overlap | 98 | |
101 | for (i1, e1) in edits.iter().skip(1).enumerate() { | 99 | fn proptest_atom_text_edits_are_valid(text: String, edits: Vec<AtomTextEdit>) { |
102 | for e2 in &edits[0..i1] { | 100 | // slicing doesn't panic |
103 | if intersect(e1.delete, e2.delete).is_some() { | 101 | for e in &edits { |
104 | assert!(false, "Overlapping ranges {} {}", e1.delete, e2.delete); | 102 | let _ = &text[e.delete]; |
103 | } | ||
104 | // ranges do not overlap | ||
105 | for i in 1..edits.len() { | ||
106 | let e1 = &edits[i]; | ||
107 | for e2 in &edits[0..i] { | ||
108 | if intersect(e1.delete, e2.delete).is_some() { | ||
109 | assert!(false, "Overlapping ranges {} {}", e1.delete, e2.delete); | ||
110 | } | ||
105 | } | 111 | } |
106 | } | 112 | } |
107 | } | 113 | } |