aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-12-27 12:19:19 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-12-27 12:19:19 +0000
commite422c2e2f4117cf977d28a40a9c8e4dc4cfee811 (patch)
tree9ea1dc365e420c286834b40923deb95a0ca291b9
parent55ab0c602e391537f5e1a84a617fdd817e6a4200 (diff)
parent1cda43aafd623b400f5916b1d3727b56c136081b (diff)
Merge #325
325: implement translate_offset_with_edit r=matklad a=vemoo - Implement `translate_offset_with_edit` to resolve #105 - Add proptest impls for text, offsets and edits and use them in tests for `translate_offset_with_edit` and `LineIndex` - Added benchmark for `translate_offset_with_edit` Co-authored-by: Bernardo <[email protected]>
-rw-r--r--Cargo.lock68
-rw-r--r--crates/ra_editor/Cargo.toml1
-rw-r--r--crates/ra_editor/src/lib.rs2
-rw-r--r--crates/ra_editor/src/line_index.rs283
-rw-r--r--crates/ra_editor/src/line_index_utils.rs363
-rw-r--r--crates/ra_lsp_server/src/conv.rs47
-rw-r--r--crates/ra_text_edit/Cargo.toml1
-rw-r--r--crates/ra_text_edit/src/lib.rs3
-rw-r--r--crates/ra_text_edit/src/test_utils.rs85
-rw-r--r--crates/ra_text_edit/src/text_edit.rs15
10 files changed, 718 insertions, 150 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 69134b434..0d417c024 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -74,6 +74,19 @@ dependencies = [
74] 74]
75 75
76[[package]] 76[[package]]
77name = "bit-set"
78version = "0.5.0"
79source = "registry+https://github.com/rust-lang/crates.io-index"
80dependencies = [
81 "bit-vec 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
82]
83
84[[package]]
85name = "bit-vec"
86version = "0.5.0"
87source = "registry+https://github.com/rust-lang/crates.io-index"
88
89[[package]]
77name = "bitflags" 90name = "bitflags"
78version = "1.0.4" 91version = "1.0.4"
79source = "registry+https://github.com/rust-lang/crates.io-index" 92source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -303,6 +316,11 @@ dependencies = [
303] 316]
304 317
305[[package]] 318[[package]]
319name = "fnv"
320version = "1.0.6"
321source = "registry+https://github.com/rust-lang/crates.io-index"
322
323[[package]]
306name = "fst" 324name = "fst"
307version = "0.3.3" 325version = "0.3.3"
308source = "registry+https://github.com/rust-lang/crates.io-index" 326source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -626,6 +644,28 @@ dependencies = [
626] 644]
627 645
628[[package]] 646[[package]]
647name = "proptest"
648version = "0.8.7"
649source = "registry+https://github.com/rust-lang/crates.io-index"
650dependencies = [
651 "bit-set 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
652 "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
653 "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
654 "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
655 "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
656 "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
657 "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
658 "regex-syntax 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
659 "rusty-fork 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
660 "tempfile 3.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
661]
662
663[[package]]
664name = "quick-error"
665version = "1.2.2"
666source = "registry+https://github.com/rust-lang/crates.io-index"
667
668[[package]]
629name = "quote" 669name = "quote"
630version = "0.6.10" 670version = "0.6.10"
631source = "registry+https://github.com/rust-lang/crates.io-index" 671source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -684,6 +724,7 @@ version = "0.1.0"
684dependencies = [ 724dependencies = [
685 "itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", 725 "itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)",
686 "join_to_string 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 726 "join_to_string 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
727 "proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)",
687 "ra_syntax 0.1.0", 728 "ra_syntax 0.1.0",
688 "ra_text_edit 0.1.0", 729 "ra_text_edit 0.1.0",
689 "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 730 "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -764,6 +805,7 @@ dependencies = [
764name = "ra_text_edit" 805name = "ra_text_edit"
765version = "0.1.0" 806version = "0.1.0"
766dependencies = [ 807dependencies = [
808 "proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)",
767 "test_utils 0.1.0", 809 "test_utils 0.1.0",
768 "text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", 810 "text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
769] 811]
@@ -985,6 +1027,17 @@ dependencies = [
985] 1027]
986 1028
987[[package]] 1029[[package]]
1030name = "rusty-fork"
1031version = "0.2.1"
1032source = "registry+https://github.com/rust-lang/crates.io-index"
1033dependencies = [
1034 "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
1035 "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
1036 "tempfile 3.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
1037 "wait-timeout 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
1038]
1039
1040[[package]]
988name = "ryu" 1041name = "ryu"
989version = "0.2.7" 1042version = "0.2.7"
990source = "registry+https://github.com/rust-lang/crates.io-index" 1043source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1414,6 +1467,14 @@ version = "1.0.2"
1414source = "registry+https://github.com/rust-lang/crates.io-index" 1467source = "registry+https://github.com/rust-lang/crates.io-index"
1415 1468
1416[[package]] 1469[[package]]
1470name = "wait-timeout"
1471version = "0.1.5"
1472source = "registry+https://github.com/rust-lang/crates.io-index"
1473dependencies = [
1474 "libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
1475]
1476
1477[[package]]
1417name = "walkdir" 1478name = "walkdir"
1418version = "2.2.7" 1479version = "2.2.7"
1419source = "registry+https://github.com/rust-lang/crates.io-index" 1480source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1460,6 +1521,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1460"checksum backtrace 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)" = "b5b493b66e03090ebc4343eb02f94ff944e0cbc9ac6571491d170ba026741eb5" 1521"checksum backtrace 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)" = "b5b493b66e03090ebc4343eb02f94ff944e0cbc9ac6571491d170ba026741eb5"
1461"checksum backtrace-sys 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)" = "3fcce89e5ad5c8949caa9434501f7b55415b3e7ad5270cb88c75a8d35e8f1279" 1522"checksum backtrace-sys 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)" = "3fcce89e5ad5c8949caa9434501f7b55415b3e7ad5270cb88c75a8d35e8f1279"
1462"checksum base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643" 1523"checksum base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643"
1524"checksum bit-set 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6f1efcc46c18245a69c38fcc5cc650f16d3a59d034f3106e9ed63748f695730a"
1525"checksum bit-vec 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4440d5cb623bb7390ae27fec0bb6c61111969860f8e3ae198bfa0663645e67cf"
1463"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" 1526"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
1464"checksum block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a076c298b9ecdb530ed9d967e74a6027d6a7478924520acddcddc24c1c8ab3ab" 1527"checksum block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a076c298b9ecdb530ed9d967e74a6027d6a7478924520acddcddc24c1c8ab3ab"
1465"checksum byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40" 1528"checksum byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40"
@@ -1488,6 +1551,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1488"checksum failure_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "64c2d913fe8ed3b6c6518eedf4538255b989945c14c2a7d5cbff62a5e2120596" 1551"checksum failure_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "64c2d913fe8ed3b6c6518eedf4538255b989945c14c2a7d5cbff62a5e2120596"
1489"checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" 1552"checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
1490"checksum flexi_logger 0.10.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4dda06444ccc8b0a6da19d939989b4a4e83f328710ada449eedaed48c8b903cd" 1553"checksum flexi_logger 0.10.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4dda06444ccc8b0a6da19d939989b4a4e83f328710ada449eedaed48c8b903cd"
1554"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
1491"checksum fst 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "db72126ca7dff566cdbbdd54af44668c544897d9d3862b198141f176f1238bdf" 1555"checksum fst 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "db72126ca7dff566cdbbdd54af44668c544897d9d3862b198141f176f1238bdf"
1492"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" 1556"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
1493"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" 1557"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
@@ -1528,6 +1592,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1528"checksum pest_generator 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ebee4e9680be4fd162e6f3394ae4192a6b60b1e4d17d845e631f0c68d1a3386" 1592"checksum pest_generator 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ebee4e9680be4fd162e6f3394ae4192a6b60b1e4d17d845e631f0c68d1a3386"
1529"checksum pest_meta 2.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1f6d5f6f0e6082578c86af197d780dc38328e3f768cec06aac9bc46d714e8221" 1593"checksum pest_meta 2.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1f6d5f6f0e6082578c86af197d780dc38328e3f768cec06aac9bc46d714e8221"
1530"checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09" 1594"checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09"
1595"checksum proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)" = "926d0604475349f463fe44130aae73f2294b5309ab2ca0310b998bd334ef191f"
1596"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
1531"checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c" 1597"checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c"
1532"checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd" 1598"checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd"
1533"checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c" 1599"checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c"
@@ -1552,6 +1618,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1552"checksum rustc-demangle 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "01b90379b8664dd83460d59bdc5dd1fd3172b8913788db483ed1325171eab2f7" 1618"checksum rustc-demangle 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "01b90379b8664dd83460d59bdc5dd1fd3172b8913788db483ed1325171eab2f7"
1553"checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8" 1619"checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8"
1554"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" 1620"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
1621"checksum rusty-fork 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9591f190d2852720b679c21f66ad929f9f1d7bb09d1193c26167586029d8489c"
1555"checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7" 1622"checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7"
1556"checksum safemem 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dca453248a96cb0749e36ccdfe2b0b4e54a61bfef89fb97ec621eb8e0a93dd9" 1623"checksum safemem 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dca453248a96cb0749e36ccdfe2b0b4e54a61bfef89fb97ec621eb8e0a93dd9"
1557"checksum salsa 0.8.0 (git+https://github.com/matklad/salsa?branch=no-upgrade)" = "<none>" 1624"checksum salsa 0.8.0 (git+https://github.com/matklad/salsa?branch=no-upgrade)" = "<none>"
@@ -1603,6 +1670,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1603"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" 1670"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
1604"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" 1671"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
1605"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" 1672"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
1673"checksum wait-timeout 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "b9f3bf741a801531993db6478b95682117471f76916f5e690dd8d45395b09349"
1606"checksum walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d9d7ed3431229a144296213105a390676cc49c9b6a72bd19f3176c98e129fa1" 1674"checksum walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d9d7ed3431229a144296213105a390676cc49c9b6a72bd19f3176c98e129fa1"
1607"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" 1675"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0"
1608"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 1676"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
diff --git a/crates/ra_editor/Cargo.toml b/crates/ra_editor/Cargo.toml
index c29be1350..f39fe4af6 100644
--- a/crates/ra_editor/Cargo.toml
+++ b/crates/ra_editor/Cargo.toml
@@ -16,3 +16,4 @@ ra_text_edit = { path = "../ra_text_edit" }
16 16
17[dev-dependencies] 17[dev-dependencies]
18test_utils = { path = "../test_utils" } 18test_utils = { path = "../test_utils" }
19proptest = "0.8.7"
diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs
index a8c68e79e..d9b89155b 100644
--- a/crates/ra_editor/src/lib.rs
+++ b/crates/ra_editor/src/lib.rs
@@ -2,6 +2,7 @@ mod code_actions;
2mod extend_selection; 2mod extend_selection;
3mod folding_ranges; 3mod folding_ranges;
4mod line_index; 4mod line_index;
5mod line_index_utils;
5mod symbols; 6mod symbols;
6#[cfg(test)] 7#[cfg(test)]
7mod test_utils; 8mod test_utils;
@@ -12,6 +13,7 @@ pub use self::{
12 extend_selection::extend_selection, 13 extend_selection::extend_selection,
13 folding_ranges::{folding_ranges, Fold, FoldKind}, 14 folding_ranges::{folding_ranges, Fold, FoldKind},
14 line_index::{LineCol, LineIndex}, 15 line_index::{LineCol, LineIndex},
16 line_index_utils::translate_offset_with_edit,
15 symbols::{file_structure, file_symbols, FileSymbol, StructureNode}, 17 symbols::{file_structure, file_symbols, FileSymbol, StructureNode},
16 typing::{join_lines, on_enter, on_eq_typed}, 18 typing::{join_lines, on_enter, on_eq_typed},
17}; 19};
diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs
index aab7e4081..898fee7e0 100644
--- a/crates/ra_editor/src/line_index.rs
+++ b/crates/ra_editor/src/line_index.rs
@@ -4,8 +4,8 @@ use superslice::Ext;
4 4
5#[derive(Clone, Debug, PartialEq, Eq)] 5#[derive(Clone, Debug, PartialEq, Eq)]
6pub struct LineIndex { 6pub struct LineIndex {
7 newlines: Vec<TextUnit>, 7 pub(crate) newlines: Vec<TextUnit>,
8 utf16_lines: FxHashMap<u32, Vec<Utf16Char>>, 8 pub(crate) utf16_lines: FxHashMap<u32, Vec<Utf16Char>>,
9} 9}
10 10
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -15,9 +15,9 @@ pub struct LineCol {
15} 15}
16 16
17#[derive(Clone, Debug, Hash, PartialEq, Eq)] 17#[derive(Clone, Debug, Hash, PartialEq, Eq)]
18struct Utf16Char { 18pub(crate) struct Utf16Char {
19 start: TextUnit, 19 pub(crate) start: TextUnit,
20 end: TextUnit, 20 pub(crate) end: TextUnit,
21} 21}
22 22
23impl Utf16Char { 23impl Utf16Char {
@@ -62,6 +62,12 @@ impl LineIndex {
62 62
63 curr_col += char_len; 63 curr_col += char_len;
64 } 64 }
65
66 // Save any utf-16 characters seen in the last line
67 if utf16_chars.len() > 0 {
68 utf16_lines.insert(line, utf16_chars);
69 }
70
65 LineIndex { 71 LineIndex {
66 newlines, 72 newlines,
67 utf16_lines, 73 utf16_lines,
@@ -122,111 +128,179 @@ impl LineIndex {
122 } 128 }
123} 129}
124 130
125#[test] 131#[cfg(test)]
126fn test_line_index() { 132/// Simple reference implementation to use in proptests
127 let text = "hello\nworld"; 133pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol {
128 let index = LineIndex::new(text); 134 let mut res = LineCol {
129 assert_eq!( 135 line: 0,
130 index.line_col(0.into()), 136 col_utf16: 0,
131 LineCol { 137 };
132 line: 0, 138 for (i, c) in text.char_indices() {
133 col_utf16: 0 139 if i + c.len_utf8() > offset.to_usize() {
134 } 140 // if it's an invalid offset, inside a multibyte char
135 ); 141 // return as if it was at the start of the char
136 assert_eq!( 142 break;
137 index.line_col(1.into()),
138 LineCol {
139 line: 0,
140 col_utf16: 1
141 }
142 );
143 assert_eq!(
144 index.line_col(5.into()),
145 LineCol {
146 line: 0,
147 col_utf16: 5
148 }
149 );
150 assert_eq!(
151 index.line_col(6.into()),
152 LineCol {
153 line: 1,
154 col_utf16: 0
155 }
156 );
157 assert_eq!(
158 index.line_col(7.into()),
159 LineCol {
160 line: 1,
161 col_utf16: 1
162 }
163 );
164 assert_eq!(
165 index.line_col(8.into()),
166 LineCol {
167 line: 1,
168 col_utf16: 2
169 }
170 );
171 assert_eq!(
172 index.line_col(10.into()),
173 LineCol {
174 line: 1,
175 col_utf16: 4
176 }
177 );
178 assert_eq!(
179 index.line_col(11.into()),
180 LineCol {
181 line: 1,
182 col_utf16: 5
183 } 143 }
184 ); 144 if c == '\n' {
185 assert_eq!( 145 res.line += 1;
186 index.line_col(12.into()), 146 res.col_utf16 = 0;
187 LineCol { 147 } else {
188 line: 1, 148 res.col_utf16 += 1;
189 col_utf16: 6
190 } 149 }
191 ); 150 }
151 res
152}
192 153
193 let text = "\nhello\nworld"; 154#[cfg(test)]
194 let index = LineIndex::new(text); 155mod test_line_index {
195 assert_eq!( 156 use super::*;
196 index.line_col(0.into()), 157 use proptest::{prelude::*, proptest, proptest_helper};
197 LineCol { 158 use ra_text_edit::test_utils::{arb_text, arb_offset};
159
160 #[test]
161 fn test_line_index() {
162 let text = "hello\nworld";
163 let index = LineIndex::new(text);
164 assert_eq!(
165 index.line_col(0.into()),
166 LineCol {
167 line: 0,
168 col_utf16: 0
169 }
170 );
171 assert_eq!(
172 index.line_col(1.into()),
173 LineCol {
174 line: 0,
175 col_utf16: 1
176 }
177 );
178 assert_eq!(
179 index.line_col(5.into()),
180 LineCol {
181 line: 0,
182 col_utf16: 5
183 }
184 );
185 assert_eq!(
186 index.line_col(6.into()),
187 LineCol {
188 line: 1,
189 col_utf16: 0
190 }
191 );
192 assert_eq!(
193 index.line_col(7.into()),
194 LineCol {
195 line: 1,
196 col_utf16: 1
197 }
198 );
199 assert_eq!(
200 index.line_col(8.into()),
201 LineCol {
202 line: 1,
203 col_utf16: 2
204 }
205 );
206 assert_eq!(
207 index.line_col(10.into()),
208 LineCol {
209 line: 1,
210 col_utf16: 4
211 }
212 );
213 assert_eq!(
214 index.line_col(11.into()),
215 LineCol {
216 line: 1,
217 col_utf16: 5
218 }
219 );
220 assert_eq!(
221 index.line_col(12.into()),
222 LineCol {
223 line: 1,
224 col_utf16: 6
225 }
226 );
227
228 let text = "\nhello\nworld";
229 let index = LineIndex::new(text);
230 assert_eq!(
231 index.line_col(0.into()),
232 LineCol {
233 line: 0,
234 col_utf16: 0
235 }
236 );
237 assert_eq!(
238 index.line_col(1.into()),
239 LineCol {
240 line: 1,
241 col_utf16: 0
242 }
243 );
244 assert_eq!(
245 index.line_col(2.into()),
246 LineCol {
247 line: 1,
248 col_utf16: 1
249 }
250 );
251 assert_eq!(
252 index.line_col(6.into()),
253 LineCol {
254 line: 1,
255 col_utf16: 5
256 }
257 );
258 assert_eq!(
259 index.line_col(7.into()),
260 LineCol {
261 line: 2,
262 col_utf16: 0
263 }
264 );
265 }
266
267 fn arb_text_with_offset() -> BoxedStrategy<(TextUnit, String)> {
268 arb_text()
269 .prop_flat_map(|text| (arb_offset(&text), Just(text)))
270 .boxed()
271 }
272
273 fn to_line_col(text: &str, offset: TextUnit) -> LineCol {
274 let mut res = LineCol {
198 line: 0, 275 line: 0,
199 col_utf16: 0 276 col_utf16: 0,
200 } 277 };
201 ); 278 for (i, c) in text.char_indices() {
202 assert_eq!( 279 if i + c.len_utf8() > offset.to_usize() {
203 index.line_col(1.into()), 280 // if it's an invalid offset, inside a multibyte char
204 LineCol { 281 // return as if it was at the start of the char
205 line: 1, 282 break;
206 col_utf16: 0 283 }
207 } 284 if c == '\n' {
208 ); 285 res.line += 1;
209 assert_eq!( 286 res.col_utf16 = 0;
210 index.line_col(2.into()), 287 } else {
211 LineCol { 288 res.col_utf16 += 1;
212 line: 1, 289 }
213 col_utf16: 1
214 }
215 );
216 assert_eq!(
217 index.line_col(6.into()),
218 LineCol {
219 line: 1,
220 col_utf16: 5
221 } 290 }
222 ); 291 res
223 assert_eq!( 292 }
224 index.line_col(7.into()), 293
225 LineCol { 294 proptest! {
226 line: 2, 295 #[test]
227 col_utf16: 0 296 fn test_line_index_proptest((offset, text) in arb_text_with_offset()) {
297 let expected = to_line_col(&text, offset);
298 let line_index = LineIndex::new(&text);
299 let actual = line_index.line_col(offset);
300
301 assert_eq!(actual, expected);
228 } 302 }
229 ); 303 }
230} 304}
231 305
232#[cfg(test)] 306#[cfg(test)]
@@ -321,4 +395,5 @@ const C: char = \"メ メ\";
321 395
322 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15)); 396 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15));
323 } 397 }
398
324} 399}
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 000000000..ec3269bbb
--- /dev/null
+++ b/crates/ra_editor/src/line_index_utils.rs
@@ -0,0 +1,363 @@
1use ra_text_edit::{AtomTextEdit, TextEdit};
2use ra_syntax::{TextUnit, TextRange};
3use crate::{LineIndex, LineCol, line_index::Utf16Char};
4
5#[derive(Debug, Clone)]
6enum Step {
7 Newline(TextUnit),
8 Utf16Char(TextRange),
9}
10
11#[derive(Debug)]
12struct LineIndexStepIter<'a> {
13 line_index: &'a LineIndex,
14 next_newline_idx: usize,
15 utf16_chars: Option<(TextUnit, std::slice::Iter<'a, Utf16Char>)>,
16}
17
18impl<'a> LineIndexStepIter<'a> {
19 fn from(line_index: &LineIndex) -> LineIndexStepIter {
20 let mut x = LineIndexStepIter {
21 line_index,
22 next_newline_idx: 0,
23 utf16_chars: None,
24 };
25 // skip first newline since it's not real
26 x.next();
27 x
28 }
29}
30
31impl<'a> Iterator for LineIndexStepIter<'a> {
32 type Item = Step;
33 fn next(&mut self) -> Option<Step> {
34 self.utf16_chars
35 .as_mut()
36 .and_then(|(newline, x)| {
37 let x = x.next()?;
38 Some(Step::Utf16Char(TextRange::from_to(
39 *newline + x.start,
40 *newline + x.end,
41 )))
42 })
43 .or_else(|| {
44 let next_newline = *self.line_index.newlines.get(self.next_newline_idx)?;
45 self.utf16_chars = self
46 .line_index
47 .utf16_lines
48 .get(&(self.next_newline_idx as u32))
49 .map(|x| (next_newline, x.iter()));
50 self.next_newline_idx += 1;
51 Some(Step::Newline(next_newline))
52 })
53 }
54}
55
56#[derive(Debug)]
57struct OffsetStepIter<'a> {
58 text: &'a str,
59 offset: TextUnit,
60}
61
62impl<'a> Iterator for OffsetStepIter<'a> {
63 type Item = Step;
64 fn next(&mut self) -> Option<Step> {
65 let (next, next_offset) = self
66 .text
67 .char_indices()
68 .filter_map(|(i, c)| {
69 if c == '\n' {
70 let next_offset = self.offset + TextUnit::from_usize(i + 1);
71 let next = Step::Newline(next_offset);
72 Some((next, next_offset))
73 } else {
74 let char_len = TextUnit::of_char(c);
75 if char_len.to_usize() > 1 {
76 let start = self.offset + TextUnit::from_usize(i);
77 let end = start + char_len;
78 let next = Step::Utf16Char(TextRange::from_to(start, end));
79 let next_offset = end;
80 Some((next, next_offset))
81 } else {
82 None
83 }
84 }
85 })
86 .next()?;
87 let next_idx = (next_offset - self.offset).to_usize();
88 self.text = &self.text[next_idx..];
89 self.offset = next_offset;
90 Some(next)
91 }
92}
93
94#[derive(Debug)]
95enum NextSteps<'a> {
96 Use,
97 ReplaceMany(OffsetStepIter<'a>),
98 AddMany(OffsetStepIter<'a>),
99}
100
101#[derive(Debug)]
102struct TranslatedEdit<'a> {
103 delete: TextRange,
104 insert: &'a str,
105 diff: i64,
106}
107
108struct Edits<'a> {
109 edits: &'a [AtomTextEdit],
110 current: Option<TranslatedEdit<'a>>,
111 acc_diff: i64,
112}
113
114impl<'a> Edits<'a> {
115 fn from_text_edit(text_edit: &'a TextEdit) -> Edits<'a> {
116 let mut x = Edits {
117 edits: text_edit.as_atoms(),
118 current: None,
119 acc_diff: 0,
120 };
121 x.advance_edit();
122 x
123 }
124 fn advance_edit(&mut self) {
125 self.acc_diff += self.current.as_ref().map_or(0, |x| x.diff);
126 match self.edits.split_first() {
127 Some((next, rest)) => {
128 let delete = self.translate_range(next.delete);
129 let diff = next.insert.len() as i64 - next.delete.len().to_usize() as i64;
130 self.current = Some(TranslatedEdit {
131 delete,
132 insert: &next.insert,
133 diff,
134 });
135 self.edits = rest;
136 }
137 None => {
138 self.current = None;
139 }
140 }
141 }
142
143 fn next_inserted_steps(&mut self) -> Option<OffsetStepIter<'a>> {
144 let cur = self.current.as_ref()?;
145 let res = Some(OffsetStepIter {
146 offset: cur.delete.start(),
147 text: &cur.insert,
148 });
149 self.advance_edit();
150 res
151 }
152
153 fn next_steps(&mut self, step: &Step) -> NextSteps {
154 let step_pos = match step {
155 &Step::Newline(n) => n,
156 &Step::Utf16Char(r) => r.end(),
157 };
158 let res = match &mut self.current {
159 Some(edit) => {
160 if step_pos <= edit.delete.start() {
161 NextSteps::Use
162 } else if step_pos <= edit.delete.end() {
163 let iter = OffsetStepIter {
164 offset: edit.delete.start(),
165 text: &edit.insert,
166 };
167 // empty slice to avoid returning steps again
168 edit.insert = &edit.insert[edit.insert.len()..];
169 NextSteps::ReplaceMany(iter)
170 } else {
171 let iter = OffsetStepIter {
172 offset: edit.delete.start(),
173 text: &edit.insert,
174 };
175 // empty slice to avoid returning steps again
176 edit.insert = &edit.insert[edit.insert.len()..];
177 self.advance_edit();
178 NextSteps::AddMany(iter)
179 }
180 }
181 None => NextSteps::Use,
182 };
183 res
184 }
185
186 fn translate_range(&self, range: TextRange) -> TextRange {
187 if self.acc_diff == 0 {
188 range
189 } else {
190 let start = self.translate(range.start());
191 let end = self.translate(range.end());
192 TextRange::from_to(start, end)
193 }
194 }
195
196 fn translate(&self, x: TextUnit) -> TextUnit {
197 if self.acc_diff == 0 {
198 x
199 } else {
200 TextUnit::from((x.to_usize() as i64 + self.acc_diff) as u32)
201 }
202 }
203
204 fn translate_step(&self, x: &Step) -> Step {
205 if self.acc_diff == 0 {
206 x.clone()
207 } else {
208 match x {
209 &Step::Newline(n) => Step::Newline(self.translate(n)),
210 &Step::Utf16Char(r) => Step::Utf16Char(self.translate_range(r)),
211 }
212 }
213 }
214}
215
216#[derive(Debug)]
217struct RunningLineCol {
218 line: u32,
219 last_newline: TextUnit,
220 col_adjust: TextUnit,
221}
222
223impl RunningLineCol {
224 fn new() -> RunningLineCol {
225 RunningLineCol {
226 line: 0,
227 last_newline: TextUnit::from(0),
228 col_adjust: TextUnit::from(0),
229 }
230 }
231
232 fn to_line_col(&self, offset: TextUnit) -> LineCol {
233 LineCol {
234 line: self.line,
235 col_utf16: ((offset - self.last_newline) - self.col_adjust).into(),
236 }
237 }
238
239 fn add_line(&mut self, newline: TextUnit) {
240 self.line += 1;
241 self.last_newline = newline;
242 self.col_adjust = TextUnit::from(0);
243 }
244
245 fn adjust_col(&mut self, range: &TextRange) {
246 self.col_adjust += range.len() - TextUnit::from(1);
247 }
248}
249
250pub fn translate_offset_with_edit(
251 line_index: &LineIndex,
252 offset: TextUnit,
253 text_edit: &TextEdit,
254) -> LineCol {
255 let mut state = Edits::from_text_edit(&text_edit);
256
257 let mut res = RunningLineCol::new();
258
259 macro_rules! test_step {
260 ($x:ident) => {
261 match &$x {
262 Step::Newline(n) => {
263 if offset < *n {
264 return res.to_line_col(offset);
265 } else {
266 res.add_line(*n);
267 }
268 }
269 Step::Utf16Char(x) => {
270 if offset < x.end() {
271 // if the offset is inside a multibyte char it's invalid
272 // clamp it to the start of the char
273 let clamp = offset.min(x.start());
274 return res.to_line_col(clamp);
275 } else {
276 res.adjust_col(x);
277 }
278 }
279 }
280 };
281 }
282
283 for orig_step in LineIndexStepIter::from(line_index) {
284 loop {
285 let translated_step = state.translate_step(&orig_step);
286 match state.next_steps(&translated_step) {
287 NextSteps::Use => {
288 test_step!(translated_step);
289 break;
290 }
291 NextSteps::ReplaceMany(ns) => {
292 for n in ns {
293 test_step!(n);
294 }
295 break;
296 }
297 NextSteps::AddMany(ns) => {
298 for n in ns {
299 test_step!(n);
300 }
301 }
302 }
303 }
304 }
305
306 loop {
307 match state.next_inserted_steps() {
308 None => break,
309 Some(ns) => {
310 for n in ns {
311 test_step!(n);
312 }
313 }
314 }
315 }
316
317 res.to_line_col(offset)
318}
319
320#[cfg(test)]
321mod test {
322 use super::*;
323 use proptest::{prelude::*, proptest, proptest_helper};
324 use crate::line_index;
325 use ra_text_edit::test_utils::{arb_offset, arb_text_with_edit};
326 use ra_text_edit::TextEdit;
327
328 #[derive(Debug)]
329 struct ArbTextWithEditAndOffset {
330 text: String,
331 edit: TextEdit,
332 edited_text: String,
333 offset: TextUnit,
334 }
335
336 fn arb_text_with_edit_and_offset() -> BoxedStrategy<ArbTextWithEditAndOffset> {
337 arb_text_with_edit()
338 .prop_flat_map(|x| {
339 let edited_text = x.edit.apply(&x.text);
340 let arb_offset = arb_offset(&edited_text);
341 (Just(x), Just(edited_text), arb_offset).prop_map(|(x, edited_text, offset)| {
342 ArbTextWithEditAndOffset {
343 text: x.text,
344 edit: x.edit,
345 edited_text,
346 offset,
347 }
348 })
349 })
350 .boxed()
351 }
352
353 proptest! {
354 #[test]
355 fn test_translate_offset_with_edit(x in arb_text_with_edit_and_offset()) {
356 let expected = line_index::to_line_col(&x.edited_text, x.offset);
357 let line_index = LineIndex::new(&x.text);
358 let actual = translate_offset_with_edit(&line_index, x.offset, &x.edit);
359
360 assert_eq!(actual, expected);
361 }
362 }
363}
diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs
index c0e4e3a36..d3670104e 100644
--- a/crates/ra_lsp_server/src/conv.rs
+++ b/crates/ra_lsp_server/src/conv.rs
@@ -3,7 +3,7 @@ use languageserver_types::{
3 TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat, 3 TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat,
4}; 4};
5use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition, CompletionItem, CompletionItemKind, InsertText}; 5use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition, CompletionItem, CompletionItemKind, InsertText};
6use ra_editor::{LineCol, LineIndex}; 6use ra_editor::{LineCol, LineIndex, translate_offset_with_edit};
7use ra_text_edit::{AtomTextEdit, TextEdit}; 7use ra_text_edit::{AtomTextEdit, TextEdit};
8use ra_syntax::{SyntaxKind, TextRange, TextUnit}; 8use ra_syntax::{SyntaxKind, TextRange, TextUnit};
9 9
@@ -238,13 +238,15 @@ impl TryConvWith for SourceChange {
238 None => None, 238 None => None,
239 Some(pos) => { 239 Some(pos) => {
240 let line_index = world.analysis().file_line_index(pos.file_id); 240 let line_index = world.analysis().file_line_index(pos.file_id);
241 let edits = self 241 let edit = self
242 .source_file_edits 242 .source_file_edits
243 .iter() 243 .iter()
244 .find(|it| it.file_id == pos.file_id) 244 .find(|it| it.file_id == pos.file_id)
245 .map(|it| it.edit.as_atoms()) 245 .map(|it| &it.edit);
246 .unwrap_or(&[]); 246 let line_col = match edit {
247 let line_col = translate_offset_with_edit(&*line_index, pos.offset, edits); 247 Some(edit) => translate_offset_with_edit(&*line_index, pos.offset, edit),
248 None => line_index.line_col(pos.offset),
249 };
248 let position = 250 let position =
249 Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16)); 251 Position::new(u64::from(line_col.line), u64::from(line_col.col_utf16));
250 Some(TextDocumentPositionParams { 252 Some(TextDocumentPositionParams {
@@ -264,41 +266,6 @@ impl TryConvWith for SourceChange {
264 } 266 }
265} 267}
266 268
267// HACK: we should translate offset to line/column using linde_index *with edits applied*.
268// A naive version of this function would be to apply `edits` to the original text,
269// construct a new line index and use that, but it would be slow.
270//
271// Writing fast & correct version is issue #105, let's use a quick hack in the meantime
272fn translate_offset_with_edit(
273 pre_edit_index: &LineIndex,
274 offset: TextUnit,
275 edits: &[AtomTextEdit],
276) -> LineCol {
277 let fallback = pre_edit_index.line_col(offset);
278 let edit = match edits.first() {
279 None => return fallback,
280 Some(edit) => edit,
281 };
282 let end_offset = edit.delete.start() + TextUnit::of_str(&edit.insert);
283 if !(edit.delete.start() <= offset && offset <= end_offset) {
284 return fallback;
285 }
286 let rel_offset = offset - edit.delete.start();
287 let in_edit_line_col = LineIndex::new(&edit.insert).line_col(rel_offset);
288 let edit_line_col = pre_edit_index.line_col(edit.delete.start());
289 if in_edit_line_col.line == 0 {
290 LineCol {
291 line: edit_line_col.line,
292 col_utf16: edit_line_col.col_utf16 + in_edit_line_col.col_utf16,
293 }
294 } else {
295 LineCol {
296 line: edit_line_col.line + in_edit_line_col.line,
297 col_utf16: in_edit_line_col.col_utf16,
298 }
299 }
300}
301
302impl TryConvWith for SourceFileEdit { 269impl TryConvWith for SourceFileEdit {
303 type Ctx = ServerWorld; 270 type Ctx = ServerWorld;
304 type Output = TextDocumentEdit; 271 type Output = TextDocumentEdit;
diff --git a/crates/ra_text_edit/Cargo.toml b/crates/ra_text_edit/Cargo.toml
index 3c4157a4e..e0db49688 100644
--- a/crates/ra_text_edit/Cargo.toml
+++ b/crates/ra_text_edit/Cargo.toml
@@ -7,6 +7,7 @@ publish = false
7 7
8[dependencies] 8[dependencies]
9text_unit = "0.1.5" 9text_unit = "0.1.5"
10proptest = "0.8.7"
10 11
11[dev-dependencies] 12[dev-dependencies]
12test_utils = { path = "../test_utils" } 13test_utils = { path = "../test_utils" }
diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs
index 13e20f6fb..8acf10448 100644
--- a/crates/ra_text_edit/src/lib.rs
+++ b/crates/ra_text_edit/src/lib.rs
@@ -1,12 +1,15 @@
1mod text_edit; 1mod text_edit;
2pub mod text_utils; 2pub mod text_utils;
3pub mod test_utils;
3 4
4pub use crate::text_edit::{TextEdit, TextEditBuilder}; 5pub use crate::text_edit::{TextEdit, TextEditBuilder};
5 6
6use text_unit::{TextRange, TextUnit}; 7use text_unit::{TextRange, TextUnit};
7 8
9/// Must not overlap with other `AtomTextEdit`s
8#[derive(Debug, Clone)] 10#[derive(Debug, Clone)]
9pub struct AtomTextEdit { 11pub struct AtomTextEdit {
12 /// Refers to offsets in the original text
10 pub delete: TextRange, 13 pub delete: TextRange,
11 pub insert: String, 14 pub insert: String,
12} 15}
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 000000000..745f21c93
--- /dev/null
+++ b/crates/ra_text_edit/src/test_utils.rs
@@ -0,0 +1,85 @@
1use proptest::prelude::*;
2use text_unit::{TextUnit, TextRange};
3use crate::{AtomTextEdit, TextEdit};
4
5pub fn arb_text() -> proptest::string::RegexGeneratorStrategy<String> {
6 // generate multiple newlines
7 proptest::string::string_regex("(.*\n?)*").unwrap()
8}
9
10fn text_offsets(text: &str) -> Vec<TextUnit> {
11 text.char_indices()
12 .map(|(i, _)| TextUnit::from_usize(i))
13 .collect()
14}
15
16pub fn arb_offset(text: &str) -> BoxedStrategy<TextUnit> {
17 let offsets = text_offsets(text);
18 // this is necessary to avoid "Uniform::new called with `low >= high`" panic
19 if offsets.is_empty() {
20 Just(TextUnit::from(0)).boxed()
21 } else {
22 prop::sample::select(offsets).boxed()
23 }
24}
25
26pub fn arb_text_edit(text: &str) -> BoxedStrategy<TextEdit> {
27 if text.is_empty() {
28 // only valid edits
29 return Just(vec![])
30 .boxed()
31 .prop_union(
32 arb_text()
33 .prop_map(|text| vec![AtomTextEdit::insert(TextUnit::from(0), text)])
34 .boxed(),
35 )
36 .prop_map(TextEdit::from_atoms)
37 .boxed();
38 }
39
40 let offsets = text_offsets(text);
41 let max_cuts = 7.min(offsets.len());
42
43 proptest::sample::subsequence(offsets, 0..max_cuts)
44 .prop_flat_map(|cuts| {
45 let strategies: Vec<_> = cuts
46 .chunks(2)
47 .map(|chunk| match chunk {
48 &[from, to] => {
49 let range = TextRange::from_to(from, to);
50 Just(AtomTextEdit::delete(range))
51 .boxed()
52 .prop_union(
53 arb_text()
54 .prop_map(move |text| AtomTextEdit::replace(range, text))
55 .boxed(),
56 )
57 .boxed()
58 }
59 &[x] => arb_text()
60 .prop_map(move |text| AtomTextEdit::insert(x, text))
61 .boxed(),
62 _ => unreachable!(),
63 })
64 .collect();
65 strategies
66 })
67 .prop_map(TextEdit::from_atoms)
68 .boxed()
69}
70
71#[derive(Debug, Clone)]
72pub struct ArbTextWithEdit {
73 pub text: String,
74 pub edit: TextEdit,
75}
76
77pub fn arb_text_with_edit() -> BoxedStrategy<ArbTextWithEdit> {
78 let text = arb_text();
79 text.prop_flat_map(|s| {
80 let edit = arb_text_edit(&s);
81 (Just(s), edit)
82 })
83 .prop_map(|(text, edit)| ArbTextWithEdit { text, edit })
84 .boxed()
85}
diff --git a/crates/ra_text_edit/src/text_edit.rs b/crates/ra_text_edit/src/text_edit.rs
index 392968d63..0881f3e1c 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 {
26 self.atoms.push(AtomTextEdit::insert(offset, text)) 26 self.atoms.push(AtomTextEdit::insert(offset, text))
27 } 27 }
28 pub fn finish(self) -> TextEdit { 28 pub fn finish(self) -> TextEdit {
29 let mut atoms = self.atoms; 29 TextEdit::from_atoms(self.atoms)
30 atoms.sort_by_key(|a| (a.delete.start(), a.delete.end()));
31 for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) {
32 assert!(a1.delete.end() <= a2.delete.start())
33 }
34 TextEdit { atoms }
35 } 30 }
36 pub fn invalidates_offset(&self, offset: TextUnit) -> bool { 31 pub fn invalidates_offset(&self, offset: TextUnit) -> bool {
37 self.atoms 32 self.atoms
@@ -41,6 +36,14 @@ impl TextEditBuilder {
41} 36}
42 37
43impl TextEdit { 38impl TextEdit {
39 pub(crate) fn from_atoms(mut atoms: Vec<AtomTextEdit>) -> TextEdit {
40 atoms.sort_by_key(|a| (a.delete.start(), a.delete.end()));
41 for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) {
42 assert!(a1.delete.end() <= a2.delete.start())
43 }
44 TextEdit { atoms }
45 }
46
44 pub fn as_atoms(&self) -> &[AtomTextEdit] { 47 pub fn as_atoms(&self) -> &[AtomTextEdit] {
45 &self.atoms 48 &self.atoms
46 } 49 }