diff options
208 files changed, 14085 insertions, 8738 deletions
diff --git a/Cargo.lock b/Cargo.lock index 08e9c10d8..bed9acf8f 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -2,9 +2,9 @@ | |||
2 | # It is not intended for manual editing. | 2 | # It is not intended for manual editing. |
3 | [[package]] | 3 | [[package]] |
4 | name = "addr2line" | 4 | name = "addr2line" |
5 | version = "0.12.1" | 5 | version = "0.12.2" |
6 | source = "registry+https://github.com/rust-lang/crates.io-index" | 6 | source = "registry+https://github.com/rust-lang/crates.io-index" |
7 | checksum = "a49806b9dadc843c61e7c97e72490ad7f7220ae249012fbda9ad0609457c0543" | 7 | checksum = "602d785912f476e480434627e8732e6766b760c045bbf897d9dfaa9f4fbd399c" |
8 | dependencies = [ | 8 | dependencies = [ |
9 | "gimli", | 9 | "gimli", |
10 | ] | 10 | ] |
@@ -288,9 +288,9 @@ checksum = "69b26e475fd29098530e709294e94e661974c851aed42512793f120fed4e199f" | |||
288 | 288 | ||
289 | [[package]] | 289 | [[package]] |
290 | name = "dtoa" | 290 | name = "dtoa" |
291 | version = "0.4.5" | 291 | version = "0.4.6" |
292 | source = "registry+https://github.com/rust-lang/crates.io-index" | 292 | source = "registry+https://github.com/rust-lang/crates.io-index" |
293 | checksum = "4358a9e11b9a09cf52383b451b49a169e8d797b68aa02301ff586d70d9661ea3" | 293 | checksum = "134951f4028bdadb9b84baf4232681efbf277da25144b9b0ad65df75946c422b" |
294 | 294 | ||
295 | [[package]] | 295 | [[package]] |
296 | name = "either" | 296 | name = "either" |
@@ -354,9 +354,9 @@ checksum = "5f2a4a2034423744d2cc7ca2068453168dcdb82c438419e639a26bd87839c674" | |||
354 | 354 | ||
355 | [[package]] | 355 | [[package]] |
356 | name = "fsevent" | 356 | name = "fsevent" |
357 | version = "0.4.0" | 357 | version = "2.0.2" |
358 | source = "registry+https://github.com/rust-lang/crates.io-index" | 358 | source = "registry+https://github.com/rust-lang/crates.io-index" |
359 | checksum = "5ab7d1bd1bd33cc98b0889831b72da23c0aa4df9cec7e0702f46ecea04b35db6" | 359 | checksum = "97f347202c95c98805c216f9e1df210e8ebaec9fdb2365700a43c10797a35e63" |
360 | dependencies = [ | 360 | dependencies = [ |
361 | "bitflags", | 361 | "bitflags", |
362 | "fsevent-sys", | 362 | "fsevent-sys", |
@@ -364,9 +364,9 @@ dependencies = [ | |||
364 | 364 | ||
365 | [[package]] | 365 | [[package]] |
366 | name = "fsevent-sys" | 366 | name = "fsevent-sys" |
367 | version = "2.0.1" | 367 | version = "3.0.2" |
368 | source = "registry+https://github.com/rust-lang/crates.io-index" | 368 | source = "registry+https://github.com/rust-lang/crates.io-index" |
369 | checksum = "f41b048a94555da0f42f1d632e2e19510084fb8e303b0daa2816e733fb3644a0" | 369 | checksum = "77a29c77f1ca394c3e73a9a5d24cfcabb734682d9634fc398f2204a63c994120" |
370 | dependencies = [ | 370 | dependencies = [ |
371 | "libc", | 371 | "libc", |
372 | ] | 372 | ] |
@@ -483,9 +483,9 @@ dependencies = [ | |||
483 | 483 | ||
484 | [[package]] | 484 | [[package]] |
485 | name = "inotify" | 485 | name = "inotify" |
486 | version = "0.7.1" | 486 | version = "0.8.3" |
487 | source = "registry+https://github.com/rust-lang/crates.io-index" | 487 | source = "registry+https://github.com/rust-lang/crates.io-index" |
488 | checksum = "4816c66d2c8ae673df83366c18341538f234a26d65a9ecea5c348b453ac1d02f" | 488 | checksum = "46dd0a94b393c730779ccfd2a872b67b1eb67be3fc33082e733bdb38b5fde4d4" |
489 | dependencies = [ | 489 | dependencies = [ |
490 | "bitflags", | 490 | "bitflags", |
491 | "inotify-sys", | 491 | "inotify-sys", |
@@ -535,9 +535,9 @@ dependencies = [ | |||
535 | 535 | ||
536 | [[package]] | 536 | [[package]] |
537 | name = "itoa" | 537 | name = "itoa" |
538 | version = "0.4.5" | 538 | version = "0.4.6" |
539 | source = "registry+https://github.com/rust-lang/crates.io-index" | 539 | source = "registry+https://github.com/rust-lang/crates.io-index" |
540 | checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e" | 540 | checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" |
541 | 541 | ||
542 | [[package]] | 542 | [[package]] |
543 | name = "jemalloc-ctl" | 543 | name = "jemalloc-ctl" |
@@ -640,9 +640,9 @@ dependencies = [ | |||
640 | 640 | ||
641 | [[package]] | 641 | [[package]] |
642 | name = "lsp-server" | 642 | name = "lsp-server" |
643 | version = "0.3.2" | 643 | version = "0.3.3" |
644 | source = "registry+https://github.com/rust-lang/crates.io-index" | 644 | source = "registry+https://github.com/rust-lang/crates.io-index" |
645 | checksum = "dccec31bfd027ac0dd288a78e19005fd89624d9099456e284b5241316a6c3072" | 645 | checksum = "53b4ace8ebe5d2aff3687ce0ed507f6020d6a47a7de2b0d3d664ea237ffb0c62" |
646 | dependencies = [ | 646 | dependencies = [ |
647 | "crossbeam-channel", | 647 | "crossbeam-channel", |
648 | "log", | 648 | "log", |
@@ -766,11 +766,13 @@ dependencies = [ | |||
766 | 766 | ||
767 | [[package]] | 767 | [[package]] |
768 | name = "notify" | 768 | name = "notify" |
769 | version = "4.0.15" | 769 | version = "5.0.0-pre.3" |
770 | source = "registry+https://github.com/rust-lang/crates.io-index" | 770 | source = "registry+https://github.com/rust-lang/crates.io-index" |
771 | checksum = "80ae4a7688d1fab81c5bf19c64fc8db920be8d519ce6336ed4e7efe024724dbd" | 771 | checksum = "77d03607cf88b4b160ba0e9ed425fff3cee3b55ac813f0c685b3a3772da37d0e" |
772 | dependencies = [ | 772 | dependencies = [ |
773 | "anymap", | ||
773 | "bitflags", | 774 | "bitflags", |
775 | "crossbeam-channel", | ||
774 | "filetime", | 776 | "filetime", |
775 | "fsevent", | 777 | "fsevent", |
776 | "fsevent-sys", | 778 | "fsevent-sys", |
@@ -830,9 +832,9 @@ dependencies = [ | |||
830 | 832 | ||
831 | [[package]] | 833 | [[package]] |
832 | name = "paste" | 834 | name = "paste" |
833 | version = "0.1.17" | 835 | version = "0.1.18" |
834 | source = "registry+https://github.com/rust-lang/crates.io-index" | 836 | source = "registry+https://github.com/rust-lang/crates.io-index" |
835 | checksum = "026c63fe245362be0322bfec5a9656d458d13f9cfb1785d1b38458b9968e8080" | 837 | checksum = "45ca20c77d80be666aef2b45486da86238fabe33e38306bd3118fe4af33fa880" |
836 | dependencies = [ | 838 | dependencies = [ |
837 | "paste-impl", | 839 | "paste-impl", |
838 | "proc-macro-hack", | 840 | "proc-macro-hack", |
@@ -840,9 +842,9 @@ dependencies = [ | |||
840 | 842 | ||
841 | [[package]] | 843 | [[package]] |
842 | name = "paste-impl" | 844 | name = "paste-impl" |
843 | version = "0.1.17" | 845 | version = "0.1.18" |
844 | source = "registry+https://github.com/rust-lang/crates.io-index" | 846 | source = "registry+https://github.com/rust-lang/crates.io-index" |
845 | checksum = "7b9281a268ec213237dcd2aa3c3d0f46681b04ced37c1616fd36567a9e6954b0" | 847 | checksum = "d95a7db200b97ef370c8e6de0088252f7e0dfff7d047a28528e47456c0fc98b6" |
846 | dependencies = [ | 848 | dependencies = [ |
847 | "proc-macro-hack", | 849 | "proc-macro-hack", |
848 | ] | 850 | ] |
@@ -952,7 +954,9 @@ dependencies = [ | |||
952 | "relative-path", | 954 | "relative-path", |
953 | "rustc-hash", | 955 | "rustc-hash", |
954 | "salsa", | 956 | "salsa", |
957 | "stdx", | ||
955 | "test_utils", | 958 | "test_utils", |
959 | "vfs", | ||
956 | ] | 960 | ] |
957 | 961 | ||
958 | [[package]] | 962 | [[package]] |
@@ -1015,6 +1019,7 @@ dependencies = [ | |||
1015 | "ra_syntax", | 1019 | "ra_syntax", |
1016 | "ra_tt", | 1020 | "ra_tt", |
1017 | "rustc-hash", | 1021 | "rustc-hash", |
1022 | "smallvec", | ||
1018 | "stdx", | 1023 | "stdx", |
1019 | "test_utils", | 1024 | "test_utils", |
1020 | ] | 1025 | ] |
@@ -1076,7 +1081,11 @@ dependencies = [ | |||
1076 | "ra_hir", | 1081 | "ra_hir", |
1077 | "ra_ide_db", | 1082 | "ra_ide_db", |
1078 | "ra_prof", | 1083 | "ra_prof", |
1084 | <<<<<<< HEAD | ||
1085 | "ra_ssr", | ||
1086 | ======= | ||
1079 | "ra_progress", | 1087 | "ra_progress", |
1088 | >>>>>>> Veetaha-feat/sync-branch | ||
1080 | "ra_syntax", | 1089 | "ra_syntax", |
1081 | "ra_text_edit", | 1090 | "ra_text_edit", |
1082 | "rand", | 1091 | "rand", |
@@ -1178,6 +1187,7 @@ dependencies = [ | |||
1178 | "anyhow", | 1187 | "anyhow", |
1179 | "cargo_metadata", | 1188 | "cargo_metadata", |
1180 | "log", | 1189 | "log", |
1190 | "paths", | ||
1181 | "ra_arena", | 1191 | "ra_arena", |
1182 | "ra_cfg", | 1192 | "ra_cfg", |
1183 | "ra_db", | 1193 | "ra_db", |
@@ -1186,6 +1196,19 @@ dependencies = [ | |||
1186 | "rustc-hash", | 1196 | "rustc-hash", |
1187 | "serde", | 1197 | "serde", |
1188 | "serde_json", | 1198 | "serde_json", |
1199 | "stdx", | ||
1200 | ] | ||
1201 | |||
1202 | [[package]] | ||
1203 | name = "ra_ssr" | ||
1204 | version = "0.1.0" | ||
1205 | dependencies = [ | ||
1206 | "ra_db", | ||
1207 | "ra_hir", | ||
1208 | "ra_ide_db", | ||
1209 | "ra_syntax", | ||
1210 | "ra_text_edit", | ||
1211 | "rustc-hash", | ||
1189 | ] | 1212 | ] |
1190 | 1213 | ||
1191 | [[package]] | 1214 | [[package]] |
@@ -1229,22 +1252,6 @@ dependencies = [ | |||
1229 | ] | 1252 | ] |
1230 | 1253 | ||
1231 | [[package]] | 1254 | [[package]] |
1232 | name = "ra_vfs" | ||
1233 | version = "0.6.1" | ||
1234 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1235 | checksum = "cbf31a173fc77ec59c27cf39af6baa137b40f4dbd45a8b3eccb1b2e4cfc922c1" | ||
1236 | dependencies = [ | ||
1237 | "crossbeam-channel", | ||
1238 | "jod-thread", | ||
1239 | "log", | ||
1240 | "notify", | ||
1241 | "parking_lot", | ||
1242 | "relative-path", | ||
1243 | "rustc-hash", | ||
1244 | "walkdir", | ||
1245 | ] | ||
1246 | |||
1247 | [[package]] | ||
1248 | name = "rand" | 1255 | name = "rand" |
1249 | version = "0.7.3" | 1256 | version = "0.7.3" |
1250 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1257 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1402,9 +1409,7 @@ dependencies = [ | |||
1402 | "ra_syntax", | 1409 | "ra_syntax", |
1403 | "ra_text_edit", | 1410 | "ra_text_edit", |
1404 | "ra_tt", | 1411 | "ra_tt", |
1405 | "ra_vfs", | ||
1406 | "rand", | 1412 | "rand", |
1407 | "relative-path", | ||
1408 | "rustc-hash", | 1413 | "rustc-hash", |
1409 | "serde", | 1414 | "serde", |
1410 | "serde_json", | 1415 | "serde_json", |
@@ -1412,6 +1417,8 @@ dependencies = [ | |||
1412 | "tempfile", | 1417 | "tempfile", |
1413 | "test_utils", | 1418 | "test_utils", |
1414 | "threadpool", | 1419 | "threadpool", |
1420 | "vfs", | ||
1421 | "vfs-notify", | ||
1415 | "winapi 0.3.8", | 1422 | "winapi 0.3.8", |
1416 | ] | 1423 | ] |
1417 | 1424 | ||
@@ -1529,18 +1536,18 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" | |||
1529 | 1536 | ||
1530 | [[package]] | 1537 | [[package]] |
1531 | name = "serde" | 1538 | name = "serde" |
1532 | version = "1.0.112" | 1539 | version = "1.0.113" |
1533 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1540 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1534 | checksum = "736aac72d1eafe8e5962d1d1c3d99b0df526015ba40915cb3c49d042e92ec243" | 1541 | checksum = "6135c78461981c79497158ef777264c51d9d0f4f3fc3a4d22b915900e42dac6a" |
1535 | dependencies = [ | 1542 | dependencies = [ |
1536 | "serde_derive", | 1543 | "serde_derive", |
1537 | ] | 1544 | ] |
1538 | 1545 | ||
1539 | [[package]] | 1546 | [[package]] |
1540 | name = "serde_derive" | 1547 | name = "serde_derive" |
1541 | version = "1.0.112" | 1548 | version = "1.0.113" |
1542 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1549 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1543 | checksum = "bf0343ce212ac0d3d6afd9391ac8e9c9efe06b533c8d33f660f6390cc4093f57" | 1550 | checksum = "93c5eaa17d0954cb481cdcfffe9d84fcfa7a1a9f2349271e678677be4c26ae31" |
1544 | dependencies = [ | 1551 | dependencies = [ |
1545 | "proc-macro2", | 1552 | "proc-macro2", |
1546 | "quote", | 1553 | "quote", |
@@ -1560,9 +1567,9 @@ dependencies = [ | |||
1560 | 1567 | ||
1561 | [[package]] | 1568 | [[package]] |
1562 | name = "serde_repr" | 1569 | name = "serde_repr" |
1563 | version = "0.1.5" | 1570 | version = "0.1.6" |
1564 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1571 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1565 | checksum = "cd02c7587ec314570041b2754829f84d873ced14a96d1fd1823531e11db40573" | 1572 | checksum = "2dc6b7951b17b051f3210b063f12cc17320e2fe30ae05b0fe2a3abb068551c76" |
1566 | dependencies = [ | 1573 | dependencies = [ |
1567 | "proc-macro2", | 1574 | "proc-macro2", |
1568 | "quote", | 1575 | "quote", |
@@ -1614,9 +1621,9 @@ checksum = "ab16ced94dbd8a46c82fd81e3ed9a8727dac2977ea869d217bcc4ea1f122e81f" | |||
1614 | 1621 | ||
1615 | [[package]] | 1622 | [[package]] |
1616 | name = "syn" | 1623 | name = "syn" |
1617 | version = "1.0.31" | 1624 | version = "1.0.32" |
1618 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1625 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1619 | checksum = "b5304cfdf27365b7585c25d4af91b35016ed21ef88f17ced89c7093b43dba8b6" | 1626 | checksum = "a994520748611c17d163e81b6c4a4b13d11b7f63884362ab2efac3aa9cf16d00" |
1620 | dependencies = [ | 1627 | dependencies = [ |
1621 | "proc-macro2", | 1628 | "proc-macro2", |
1622 | "quote", | 1629 | "quote", |
@@ -1673,8 +1680,6 @@ name = "test_utils" | |||
1673 | version = "0.1.0" | 1680 | version = "0.1.0" |
1674 | dependencies = [ | 1681 | dependencies = [ |
1675 | "difference", | 1682 | "difference", |
1676 | "ra_cfg", | ||
1677 | "relative-path", | ||
1678 | "rustc-hash", | 1683 | "rustc-hash", |
1679 | "serde_json", | 1684 | "serde_json", |
1680 | "stdx", | 1685 | "stdx", |
@@ -1712,6 +1717,12 @@ dependencies = [ | |||
1712 | ] | 1717 | ] |
1713 | 1718 | ||
1714 | [[package]] | 1719 | [[package]] |
1720 | name = "tinyvec" | ||
1721 | version = "0.3.3" | ||
1722 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1723 | checksum = "53953d2d3a5ad81d9f844a32f14ebb121f50b650cd59d0ee2a07cf13c617efed" | ||
1724 | |||
1725 | [[package]] | ||
1715 | name = "unicode-bidi" | 1726 | name = "unicode-bidi" |
1716 | version = "0.3.4" | 1727 | version = "0.3.4" |
1717 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1728 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1722,11 +1733,11 @@ dependencies = [ | |||
1722 | 1733 | ||
1723 | [[package]] | 1734 | [[package]] |
1724 | name = "unicode-normalization" | 1735 | name = "unicode-normalization" |
1725 | version = "0.1.12" | 1736 | version = "0.1.13" |
1726 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1737 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1727 | checksum = "5479532badd04e128284890390c1e876ef7a993d0570b3597ae43dfa1d59afa4" | 1738 | checksum = "6fb19cf769fa8c6a80a162df694621ebeb4dafb606470b2b2fce0be40a98a977" |
1728 | dependencies = [ | 1739 | dependencies = [ |
1729 | "smallvec", | 1740 | "tinyvec", |
1730 | ] | 1741 | ] |
1731 | 1742 | ||
1732 | [[package]] | 1743 | [[package]] |
@@ -1757,11 +1768,22 @@ dependencies = [ | |||
1757 | name = "vfs" | 1768 | name = "vfs" |
1758 | version = "0.1.0" | 1769 | version = "0.1.0" |
1759 | dependencies = [ | 1770 | dependencies = [ |
1771 | "paths", | ||
1772 | "rustc-hash", | ||
1773 | ] | ||
1774 | |||
1775 | [[package]] | ||
1776 | name = "vfs-notify" | ||
1777 | version = "0.1.0" | ||
1778 | dependencies = [ | ||
1760 | "crossbeam-channel", | 1779 | "crossbeam-channel", |
1761 | "globset", | 1780 | "globset", |
1762 | "jod-thread", | 1781 | "jod-thread", |
1782 | "log", | ||
1783 | "notify", | ||
1763 | "paths", | 1784 | "paths", |
1764 | "rustc-hash", | 1785 | "rustc-hash", |
1786 | "vfs", | ||
1765 | "walkdir", | 1787 | "walkdir", |
1766 | ] | 1788 | ] |
1767 | 1789 | ||
diff --git a/crates/paths/src/lib.rs b/crates/paths/src/lib.rs index c7ce0c42f..1b259682d 100644 --- a/crates/paths/src/lib.rs +++ b/crates/paths/src/lib.rs | |||
@@ -28,6 +28,12 @@ impl AsRef<Path> for AbsPathBuf { | |||
28 | } | 28 | } |
29 | } | 29 | } |
30 | 30 | ||
31 | impl AsRef<AbsPath> for AbsPathBuf { | ||
32 | fn as_ref(&self) -> &AbsPath { | ||
33 | self.as_path() | ||
34 | } | ||
35 | } | ||
36 | |||
31 | impl TryFrom<PathBuf> for AbsPathBuf { | 37 | impl TryFrom<PathBuf> for AbsPathBuf { |
32 | type Error = PathBuf; | 38 | type Error = PathBuf; |
33 | fn try_from(path_buf: PathBuf) -> Result<AbsPathBuf, PathBuf> { | 39 | fn try_from(path_buf: PathBuf) -> Result<AbsPathBuf, PathBuf> { |
@@ -45,9 +51,19 @@ impl TryFrom<&str> for AbsPathBuf { | |||
45 | } | 51 | } |
46 | } | 52 | } |
47 | 53 | ||
54 | impl PartialEq<AbsPath> for AbsPathBuf { | ||
55 | fn eq(&self, other: &AbsPath) -> bool { | ||
56 | self.as_path() == other | ||
57 | } | ||
58 | } | ||
59 | |||
48 | impl AbsPathBuf { | 60 | impl AbsPathBuf { |
61 | pub fn assert(path: PathBuf) -> AbsPathBuf { | ||
62 | AbsPathBuf::try_from(path) | ||
63 | .unwrap_or_else(|path| panic!("expected absolute path, got {}", path.display())) | ||
64 | } | ||
49 | pub fn as_path(&self) -> &AbsPath { | 65 | pub fn as_path(&self) -> &AbsPath { |
50 | AbsPath::new_unchecked(self.0.as_path()) | 66 | AbsPath::assert(self.0.as_path()) |
51 | } | 67 | } |
52 | pub fn pop(&mut self) -> bool { | 68 | pub fn pop(&mut self) -> bool { |
53 | self.0.pop() | 69 | self.0.pop() |
@@ -77,21 +93,99 @@ impl<'a> TryFrom<&'a Path> for &'a AbsPath { | |||
77 | if !path.is_absolute() { | 93 | if !path.is_absolute() { |
78 | return Err(path); | 94 | return Err(path); |
79 | } | 95 | } |
80 | Ok(AbsPath::new_unchecked(path)) | 96 | Ok(AbsPath::assert(path)) |
81 | } | 97 | } |
82 | } | 98 | } |
83 | 99 | ||
84 | impl AbsPath { | 100 | impl AbsPath { |
85 | fn new_unchecked(path: &Path) -> &AbsPath { | 101 | pub fn assert(path: &Path) -> &AbsPath { |
102 | assert!(path.is_absolute()); | ||
86 | unsafe { &*(path as *const Path as *const AbsPath) } | 103 | unsafe { &*(path as *const Path as *const AbsPath) } |
87 | } | 104 | } |
88 | 105 | ||
106 | pub fn parent(&self) -> Option<&AbsPath> { | ||
107 | self.0.parent().map(AbsPath::assert) | ||
108 | } | ||
89 | pub fn join(&self, path: impl AsRef<Path>) -> AbsPathBuf { | 109 | pub fn join(&self, path: impl AsRef<Path>) -> AbsPathBuf { |
90 | self.as_ref().join(path).try_into().unwrap() | 110 | self.as_ref().join(path).try_into().unwrap() |
91 | } | 111 | } |
92 | pub fn normalize(&self) -> AbsPathBuf { | 112 | pub fn normalize(&self) -> AbsPathBuf { |
93 | AbsPathBuf(normalize_path(&self.0)) | 113 | AbsPathBuf(normalize_path(&self.0)) |
94 | } | 114 | } |
115 | pub fn to_path_buf(&self) -> AbsPathBuf { | ||
116 | AbsPathBuf::try_from(self.0.to_path_buf()).unwrap() | ||
117 | } | ||
118 | pub fn strip_prefix(&self, base: &AbsPath) -> Option<&RelPath> { | ||
119 | self.0.strip_prefix(base).ok().map(RelPath::new_unchecked) | ||
120 | } | ||
121 | } | ||
122 | |||
123 | #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] | ||
124 | pub struct RelPathBuf(PathBuf); | ||
125 | |||
126 | impl From<RelPathBuf> for PathBuf { | ||
127 | fn from(RelPathBuf(path_buf): RelPathBuf) -> PathBuf { | ||
128 | path_buf | ||
129 | } | ||
130 | } | ||
131 | |||
132 | impl ops::Deref for RelPathBuf { | ||
133 | type Target = RelPath; | ||
134 | fn deref(&self) -> &RelPath { | ||
135 | self.as_path() | ||
136 | } | ||
137 | } | ||
138 | |||
139 | impl AsRef<Path> for RelPathBuf { | ||
140 | fn as_ref(&self) -> &Path { | ||
141 | self.0.as_path() | ||
142 | } | ||
143 | } | ||
144 | |||
145 | impl TryFrom<PathBuf> for RelPathBuf { | ||
146 | type Error = PathBuf; | ||
147 | fn try_from(path_buf: PathBuf) -> Result<RelPathBuf, PathBuf> { | ||
148 | if !path_buf.is_relative() { | ||
149 | return Err(path_buf); | ||
150 | } | ||
151 | Ok(RelPathBuf(path_buf)) | ||
152 | } | ||
153 | } | ||
154 | |||
155 | impl TryFrom<&str> for RelPathBuf { | ||
156 | type Error = PathBuf; | ||
157 | fn try_from(path: &str) -> Result<RelPathBuf, PathBuf> { | ||
158 | RelPathBuf::try_from(PathBuf::from(path)) | ||
159 | } | ||
160 | } | ||
161 | |||
162 | impl RelPathBuf { | ||
163 | pub fn as_path(&self) -> &RelPath { | ||
164 | RelPath::new_unchecked(self.0.as_path()) | ||
165 | } | ||
166 | } | ||
167 | |||
168 | #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash)] | ||
169 | #[repr(transparent)] | ||
170 | pub struct RelPath(Path); | ||
171 | |||
172 | impl ops::Deref for RelPath { | ||
173 | type Target = Path; | ||
174 | fn deref(&self) -> &Path { | ||
175 | &self.0 | ||
176 | } | ||
177 | } | ||
178 | |||
179 | impl AsRef<Path> for RelPath { | ||
180 | fn as_ref(&self) -> &Path { | ||
181 | &self.0 | ||
182 | } | ||
183 | } | ||
184 | |||
185 | impl RelPath { | ||
186 | pub fn new_unchecked(path: &Path) -> &RelPath { | ||
187 | unsafe { &*(path as *const Path as *const RelPath) } | ||
188 | } | ||
95 | } | 189 | } |
96 | 190 | ||
97 | // https://github.com/rust-lang/cargo/blob/79c769c3d7b4c2cf6a93781575b7f592ef974255/src/cargo/util/paths.rs#L60-L85 | 191 | // https://github.com/rust-lang/cargo/blob/79c769c3d7b4c2cf6a93781575b7f592ef974255/src/cargo/util/paths.rs#L60-L85 |
diff --git a/crates/ra_arena/src/lib.rs b/crates/ra_arena/src/lib.rs index 441fbb3cb..3169aa5b8 100644 --- a/crates/ra_arena/src/lib.rs +++ b/crates/ra_arena/src/lib.rs | |||
@@ -116,6 +116,9 @@ impl<T> Arena<T> { | |||
116 | ) -> impl Iterator<Item = (Idx<T>, &T)> + ExactSizeIterator + DoubleEndedIterator { | 116 | ) -> impl Iterator<Item = (Idx<T>, &T)> + ExactSizeIterator + DoubleEndedIterator { |
117 | self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawId(idx as u32)), value)) | 117 | self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawId(idx as u32)), value)) |
118 | } | 118 | } |
119 | pub fn shrink_to_fit(&mut self) { | ||
120 | self.data.shrink_to_fit(); | ||
121 | } | ||
119 | } | 122 | } |
120 | 123 | ||
121 | impl<T> Default for Arena<T> { | 124 | impl<T> Default for Arena<T> { |
diff --git a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs index 776bddf91..b0e56e1b5 100644 --- a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs +++ b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs | |||
@@ -128,7 +128,7 @@ impl From<foo::bar::baz::Boo> for A { | |||
128 | 128 | ||
129 | fn check_not_applicable(ra_fixture: &str) { | 129 | fn check_not_applicable(ra_fixture: &str) { |
130 | let fixture = | 130 | let fixture = |
131 | format!("//- main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE); | 131 | format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE); |
132 | check_assist_not_applicable(add_from_impl_for_enum, &fixture) | 132 | check_assist_not_applicable(add_from_impl_for_enum, &fixture) |
133 | } | 133 | } |
134 | 134 | ||
diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs index 5092bf336..d1cafa7d9 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs | |||
@@ -488,16 +488,17 @@ mod tests { | |||
488 | check_assist( | 488 | check_assist( |
489 | auto_import, | 489 | auto_import, |
490 | r" | 490 | r" |
491 | //- /lib.rs crate:crate_with_macro | 491 | //- /lib.rs crate:crate_with_macro |
492 | #[macro_export] | 492 | #[macro_export] |
493 | macro_rules! foo { | 493 | macro_rules! foo { |
494 | () => () | 494 | () => () |
495 | } | 495 | } |
496 | 496 | ||
497 | //- /main.rs crate:main deps:crate_with_macro | 497 | //- /main.rs crate:main deps:crate_with_macro |
498 | fn main() { | 498 | fn main() { |
499 | foo<|> | 499 | foo<|> |
500 | }", | 500 | } |
501 | ", | ||
501 | r"use crate_with_macro::foo; | 502 | r"use crate_with_macro::foo; |
502 | 503 | ||
503 | fn main() { | 504 | fn main() { |
@@ -847,13 +848,14 @@ fn main() { | |||
847 | check_assist( | 848 | check_assist( |
848 | auto_import, | 849 | auto_import, |
849 | r" | 850 | r" |
850 | //- /lib.rs crate:dep | 851 | //- /lib.rs crate:dep |
851 | pub struct Struct; | 852 | pub struct Struct; |
852 | 853 | ||
853 | //- /main.rs crate:main deps:dep | 854 | //- /main.rs crate:main deps:dep |
854 | fn main() { | 855 | fn main() { |
855 | Struct<|> | 856 | Struct<|> |
856 | }", | 857 | } |
858 | ", | ||
857 | r"use dep::Struct; | 859 | r"use dep::Struct; |
858 | 860 | ||
859 | fn main() { | 861 | fn main() { |
@@ -869,20 +871,22 @@ fn main() { | |||
869 | check_assist( | 871 | check_assist( |
870 | auto_import, | 872 | auto_import, |
871 | r" | 873 | r" |
872 | //- /lib.rs crate:dep | 874 | //- /lib.rs crate:dep |
873 | pub mod fmt { | 875 | pub mod fmt { |
874 | pub trait Display {} | 876 | pub trait Display {} |
875 | } | 877 | } |
876 | 878 | ||
877 | pub fn panic_fmt() {} | 879 | pub fn panic_fmt() {} |
878 | 880 | ||
879 | //- /main.rs crate:main deps:dep | 881 | //- /main.rs crate:main deps:dep |
880 | struct S; | 882 | struct S; |
881 | 883 | ||
882 | impl f<|>mt::Display for S {}", | 884 | impl f<|>mt::Display for S {} |
885 | ", | ||
883 | r"use dep::fmt; | 886 | r"use dep::fmt; |
884 | 887 | ||
885 | struct S; | 888 | struct S; |
889 | |||
886 | impl fmt::Display for S {} | 890 | impl fmt::Display for S {} |
887 | ", | 891 | ", |
888 | ); | 892 | ); |
@@ -894,21 +898,20 @@ impl fmt::Display for S {} | |||
894 | check_assist( | 898 | check_assist( |
895 | auto_import, | 899 | auto_import, |
896 | r" | 900 | r" |
897 | //- /lib.rs crate:dep | 901 | //- /lib.rs crate:dep |
898 | 902 | macro_rules! mac { | |
899 | macro_rules! mac { | 903 | () => { |
900 | () => { | 904 | pub struct Cheese; |
901 | pub struct Cheese; | 905 | }; |
902 | }; | 906 | } |
903 | } | ||
904 | |||
905 | mac!(); | ||
906 | 907 | ||
907 | //- /main.rs crate:main deps:dep | 908 | mac!(); |
908 | 909 | ||
909 | fn main() { | 910 | //- /main.rs crate:main deps:dep |
910 | Cheese<|>; | 911 | fn main() { |
911 | }", | 912 | Cheese<|>; |
913 | } | ||
914 | ", | ||
912 | r"use dep::Cheese; | 915 | r"use dep::Cheese; |
913 | 916 | ||
914 | fn main() { | 917 | fn main() { |
@@ -924,16 +927,15 @@ fn main() { | |||
924 | check_assist( | 927 | check_assist( |
925 | auto_import, | 928 | auto_import, |
926 | r" | 929 | r" |
927 | //- /lib.rs crate:dep | 930 | //- /lib.rs crate:dep |
928 | 931 | pub struct FMT; | |
929 | pub struct FMT; | 932 | pub struct fmt; |
930 | pub struct fmt; | ||
931 | |||
932 | //- /main.rs crate:main deps:dep | ||
933 | 933 | ||
934 | fn main() { | 934 | //- /main.rs crate:main deps:dep |
935 | FMT<|>; | 935 | fn main() { |
936 | }", | 936 | FMT<|>; |
937 | } | ||
938 | ", | ||
937 | r"use dep::FMT; | 939 | r"use dep::FMT; |
938 | 940 | ||
939 | fn main() { | 941 | fn main() { |
diff --git a/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs index 44db7917a..43b4584b4 100644 --- a/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs | |||
@@ -301,7 +301,7 @@ fn another_fn() { | |||
301 | 301 | ||
302 | fn check_not_applicable(ra_fixture: &str) { | 302 | fn check_not_applicable(ra_fixture: &str) { |
303 | let fixture = | 303 | let fixture = |
304 | format!("//- main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE); | 304 | format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE); |
305 | check_assist_not_applicable(extract_struct_from_enum_variant, &fixture) | 305 | check_assist_not_applicable(extract_struct_from_enum_variant, &fixture) |
306 | } | 306 | } |
307 | 307 | ||
diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs index 569efb768..64270c86f 100644 --- a/crates/ra_assists/src/handlers/fill_match_arms.rs +++ b/crates/ra_assists/src/handlers/fill_match_arms.rs | |||
@@ -763,9 +763,9 @@ mod tests { | |||
763 | fn foo(opt: Option<i32>) { | 763 | fn foo(opt: Option<i32>) { |
764 | match opt<|> { | 764 | match opt<|> { |
765 | } | 765 | } |
766 | }"#; | 766 | } |
767 | let before = | 767 | "#; |
768 | &format!("//- main.rs crate:main deps:core\n{}{}", before, FamousDefs::FIXTURE); | 768 | let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE); |
769 | 769 | ||
770 | check_assist( | 770 | check_assist( |
771 | fill_match_arms, | 771 | fill_match_arms, |
diff --git a/crates/ra_assists/src/handlers/fix_visibility.rs b/crates/ra_assists/src/handlers/fix_visibility.rs index 531b3560f..54601d1f3 100644 --- a/crates/ra_assists/src/handlers/fix_visibility.rs +++ b/crates/ra_assists/src/handlers/fix_visibility.rs | |||
@@ -255,15 +255,14 @@ mod tests { | |||
255 | check_assist( | 255 | check_assist( |
256 | fix_visibility, | 256 | fix_visibility, |
257 | r" | 257 | r" |
258 | //- /main.rs | 258 | //- /main.rs |
259 | mod foo; | 259 | mod foo; |
260 | fn main() { foo::Foo<|> } | 260 | fn main() { foo::Foo<|> } |
261 | 261 | ||
262 | //- /foo.rs | 262 | //- /foo.rs |
263 | struct Foo; | 263 | struct Foo; |
264 | ", | 264 | ", |
265 | r"$0pub(crate) struct Foo; | 265 | r"$0pub(crate) struct Foo; |
266 | |||
267 | ", | 266 | ", |
268 | ); | 267 | ); |
269 | } | 268 | } |
@@ -279,14 +278,14 @@ mod tests { | |||
279 | ); | 278 | ); |
280 | check_assist( | 279 | check_assist( |
281 | fix_visibility, | 280 | fix_visibility, |
282 | r"//- /lib.rs | 281 | r" |
283 | mod foo; | 282 | //- /lib.rs |
284 | fn main() { foo::Foo { <|>bar: () }; } | 283 | mod foo; |
285 | //- /foo.rs | 284 | fn main() { foo::Foo { <|>bar: () }; } |
286 | pub struct Foo { bar: () } | 285 | //- /foo.rs |
287 | ", | 286 | pub struct Foo { bar: () } |
287 | ", | ||
288 | r"pub struct Foo { $0pub(crate) bar: () } | 288 | r"pub struct Foo { $0pub(crate) bar: () } |
289 | |||
290 | ", | 289 | ", |
291 | ); | 290 | ); |
292 | check_assist_not_applicable( | 291 | check_assist_not_applicable( |
@@ -296,12 +295,13 @@ mod tests { | |||
296 | ); | 295 | ); |
297 | check_assist_not_applicable( | 296 | check_assist_not_applicable( |
298 | fix_visibility, | 297 | fix_visibility, |
299 | r"//- /lib.rs | 298 | r" |
300 | mod foo; | 299 | //- /lib.rs |
301 | fn main() { foo::Foo { <|>bar: () }; } | 300 | mod foo; |
302 | //- /foo.rs | 301 | fn main() { foo::Foo { <|>bar: () }; } |
303 | pub struct Foo { pub bar: () } | 302 | //- /foo.rs |
304 | ", | 303 | pub struct Foo { pub bar: () } |
304 | ", | ||
305 | ); | 305 | ); |
306 | } | 306 | } |
307 | 307 | ||
@@ -316,14 +316,14 @@ mod tests { | |||
316 | ); | 316 | ); |
317 | check_assist( | 317 | check_assist( |
318 | fix_visibility, | 318 | fix_visibility, |
319 | r"//- /lib.rs | 319 | r" |
320 | mod foo; | 320 | //- /lib.rs |
321 | fn main() { foo::Foo::Bar { <|>bar: () }; } | 321 | mod foo; |
322 | //- /foo.rs | 322 | fn main() { foo::Foo::Bar { <|>bar: () }; } |
323 | pub enum Foo { Bar { bar: () } } | 323 | //- /foo.rs |
324 | ", | 324 | pub enum Foo { Bar { bar: () } } |
325 | ", | ||
325 | r"pub enum Foo { Bar { $0pub(crate) bar: () } } | 326 | r"pub enum Foo { Bar { $0pub(crate) bar: () } } |
326 | |||
327 | ", | 327 | ", |
328 | ); | 328 | ); |
329 | check_assist_not_applicable( | 329 | check_assist_not_applicable( |
@@ -333,12 +333,13 @@ mod tests { | |||
333 | ); | 333 | ); |
334 | check_assist_not_applicable( | 334 | check_assist_not_applicable( |
335 | fix_visibility, | 335 | fix_visibility, |
336 | r"//- /lib.rs | 336 | r" |
337 | mod foo; | 337 | //- /lib.rs |
338 | fn main() { foo::Foo { <|>bar: () }; } | 338 | mod foo; |
339 | //- /foo.rs | 339 | fn main() { foo::Foo { <|>bar: () }; } |
340 | pub struct Foo { pub bar: () } | 340 | //- /foo.rs |
341 | ", | 341 | pub struct Foo { pub bar: () } |
342 | ", | ||
342 | ); | 343 | ); |
343 | } | 344 | } |
344 | 345 | ||
@@ -355,14 +356,14 @@ mod tests { | |||
355 | ); | 356 | ); |
356 | check_assist( | 357 | check_assist( |
357 | fix_visibility, | 358 | fix_visibility, |
358 | r"//- /lib.rs | 359 | r" |
359 | mod foo; | 360 | //- /lib.rs |
360 | fn main() { foo::Foo { <|>bar: () }; } | 361 | mod foo; |
361 | //- /foo.rs | 362 | fn main() { foo::Foo { <|>bar: () }; } |
362 | pub union Foo { bar: () } | 363 | //- /foo.rs |
363 | ", | 364 | pub union Foo { bar: () } |
365 | ", | ||
364 | r"pub union Foo { $0pub(crate) bar: () } | 366 | r"pub union Foo { $0pub(crate) bar: () } |
365 | |||
366 | ", | 367 | ", |
367 | ); | 368 | ); |
368 | check_assist_not_applicable( | 369 | check_assist_not_applicable( |
@@ -372,12 +373,13 @@ mod tests { | |||
372 | ); | 373 | ); |
373 | check_assist_not_applicable( | 374 | check_assist_not_applicable( |
374 | fix_visibility, | 375 | fix_visibility, |
375 | r"//- /lib.rs | 376 | r" |
376 | mod foo; | 377 | //- /lib.rs |
377 | fn main() { foo::Foo { <|>bar: () }; } | 378 | mod foo; |
378 | //- /foo.rs | 379 | fn main() { foo::Foo { <|>bar: () }; } |
379 | pub union Foo { pub bar: () } | 380 | //- /foo.rs |
380 | ", | 381 | pub union Foo { pub bar: () } |
382 | ", | ||
381 | ); | 383 | ); |
382 | } | 384 | } |
383 | 385 | ||
@@ -458,19 +460,18 @@ mod tests { | |||
458 | check_assist( | 460 | check_assist( |
459 | fix_visibility, | 461 | fix_visibility, |
460 | r" | 462 | r" |
461 | //- /main.rs | 463 | //- /main.rs |
462 | mod foo; | 464 | mod foo; |
463 | fn main() { foo::bar<|>::baz(); } | 465 | fn main() { foo::bar<|>::baz(); } |
464 | 466 | ||
465 | //- /foo.rs | 467 | //- /foo.rs |
466 | mod bar { | 468 | mod bar { |
467 | pub fn baz() {} | 469 | pub fn baz() {} |
468 | } | 470 | } |
469 | ", | 471 | ", |
470 | r"$0pub(crate) mod bar { | 472 | r"$0pub(crate) mod bar { |
471 | pub fn baz() {} | 473 | pub fn baz() {} |
472 | } | 474 | } |
473 | |||
474 | ", | 475 | ", |
475 | ); | 476 | ); |
476 | 477 | ||
@@ -486,17 +487,15 @@ mod tests { | |||
486 | check_assist( | 487 | check_assist( |
487 | fix_visibility, | 488 | fix_visibility, |
488 | r" | 489 | r" |
489 | //- /main.rs | 490 | //- /main.rs |
490 | mod foo; | 491 | mod foo; |
491 | fn main() { foo::bar<|>::baz(); } | 492 | fn main() { foo::bar<|>::baz(); } |
492 | 493 | ||
493 | //- /foo.rs | 494 | //- /foo.rs |
494 | mod bar; | 495 | mod bar; |
495 | 496 | //- /foo/bar.rs | |
496 | //- /foo/bar.rs | 497 | pub fn baz() {} |
497 | pub fn baz() {} | 498 | ", |
498 | } | ||
499 | ", | ||
500 | r"$0pub(crate) mod bar; | 499 | r"$0pub(crate) mod bar; |
501 | ", | 500 | ", |
502 | ); | 501 | ); |
@@ -506,14 +505,16 @@ mod tests { | |||
506 | fn fix_visibility_of_module_declaration_in_other_file() { | 505 | fn fix_visibility_of_module_declaration_in_other_file() { |
507 | check_assist( | 506 | check_assist( |
508 | fix_visibility, | 507 | fix_visibility, |
509 | r"//- /main.rs | 508 | r" |
510 | mod foo; | 509 | //- /main.rs |
511 | fn main() { foo::bar<|>>::baz(); } | 510 | mod foo; |
511 | fn main() { foo::bar<|>>::baz(); } | ||
512 | 512 | ||
513 | //- /foo.rs | 513 | //- /foo.rs |
514 | mod bar { | 514 | mod bar { |
515 | pub fn baz() {} | 515 | pub fn baz() {} |
516 | }", | 516 | } |
517 | ", | ||
517 | r"$0pub(crate) mod bar { | 518 | r"$0pub(crate) mod bar { |
518 | pub fn baz() {} | 519 | pub fn baz() {} |
519 | } | 520 | } |
@@ -525,10 +526,12 @@ mod tests { | |||
525 | fn adds_pub_when_target_is_in_another_crate() { | 526 | fn adds_pub_when_target_is_in_another_crate() { |
526 | check_assist( | 527 | check_assist( |
527 | fix_visibility, | 528 | fix_visibility, |
528 | r"//- /main.rs crate:a deps:foo | 529 | r" |
529 | foo::Bar<|> | 530 | //- /main.rs crate:a deps:foo |
530 | //- /lib.rs crate:foo | 531 | foo::Bar<|> |
531 | struct Bar;", | 532 | //- /lib.rs crate:foo |
533 | struct Bar; | ||
534 | ", | ||
532 | r"$0pub struct Bar; | 535 | r"$0pub struct Bar; |
533 | ", | 536 | ", |
534 | ) | 537 | ) |
diff --git a/crates/ra_assists/src/handlers/introduce_variable.rs b/crates/ra_assists/src/handlers/introduce_variable.rs index 31d6539f7..88b62278f 100644 --- a/crates/ra_assists/src/handlers/introduce_variable.rs +++ b/crates/ra_assists/src/handlers/introduce_variable.rs | |||
@@ -226,27 +226,31 @@ fn foo() { | |||
226 | mark::check!(test_introduce_var_last_expr); | 226 | mark::check!(test_introduce_var_last_expr); |
227 | check_assist( | 227 | check_assist( |
228 | introduce_variable, | 228 | introduce_variable, |
229 | " | 229 | r#" |
230 | fn foo() { | 230 | fn foo() { |
231 | bar(<|>1 + 1<|>) | 231 | bar(<|>1 + 1<|>) |
232 | }", | 232 | } |
233 | " | 233 | "#, |
234 | r#" | ||
234 | fn foo() { | 235 | fn foo() { |
235 | let $0var_name = 1 + 1; | 236 | let $0var_name = 1 + 1; |
236 | bar(var_name) | 237 | bar(var_name) |
237 | }", | 238 | } |
239 | "#, | ||
238 | ); | 240 | ); |
239 | check_assist( | 241 | check_assist( |
240 | introduce_variable, | 242 | introduce_variable, |
241 | " | 243 | r#" |
242 | fn foo() { | 244 | fn foo() { |
243 | <|>bar(1 + 1)<|> | 245 | <|>bar(1 + 1)<|> |
244 | }", | 246 | } |
245 | " | 247 | "#, |
248 | r#" | ||
246 | fn foo() { | 249 | fn foo() { |
247 | let $0var_name = bar(1 + 1); | 250 | let $0var_name = bar(1 + 1); |
248 | var_name | 251 | var_name |
249 | }", | 252 | } |
253 | "#, | ||
250 | ) | 254 | ) |
251 | } | 255 | } |
252 | 256 | ||
diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index 972d16241..ac0b3035c 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs | |||
@@ -127,7 +127,7 @@ fn first_path(path: &ast::Path) -> ast::Path { | |||
127 | 127 | ||
128 | #[cfg(test)] | 128 | #[cfg(test)] |
129 | mod tests { | 129 | mod tests { |
130 | use crate::tests::check_assist; | 130 | use crate::tests::{check_assist, check_assist_not_applicable}; |
131 | 131 | ||
132 | use super::*; | 132 | use super::*; |
133 | 133 | ||
@@ -276,4 +276,14 @@ bar::baz}; | |||
276 | ", | 276 | ", |
277 | ) | 277 | ) |
278 | } | 278 | } |
279 | |||
280 | #[test] | ||
281 | fn test_empty_use() { | ||
282 | check_assist_not_applicable( | ||
283 | merge_imports, | ||
284 | r" | ||
285 | use std::<|> | ||
286 | fn main() {}", | ||
287 | ); | ||
288 | } | ||
279 | } | 289 | } |
diff --git a/crates/ra_assists/src/handlers/raw_string.rs b/crates/ra_assists/src/handlers/raw_string.rs index 16002d2ac..d22d0aa55 100644 --- a/crates/ra_assists/src/handlers/raw_string.rs +++ b/crates/ra_assists/src/handlers/raw_string.rs | |||
@@ -158,16 +158,16 @@ mod test { | |||
158 | check_assist( | 158 | check_assist( |
159 | make_raw_string, | 159 | make_raw_string, |
160 | r#" | 160 | r#" |
161 | fn f() { | 161 | fn f() { |
162 | let s = <|>"random\nstring"; | 162 | let s = <|>"random\nstring"; |
163 | } | 163 | } |
164 | "#, | 164 | "#, |
165 | r##" | 165 | r##" |
166 | fn f() { | 166 | fn f() { |
167 | let s = r#"random | 167 | let s = r#"random |
168 | string"#; | 168 | string"#; |
169 | } | 169 | } |
170 | "##, | 170 | "##, |
171 | ) | 171 | ) |
172 | } | 172 | } |
173 | 173 | ||
@@ -193,16 +193,16 @@ string"#; | |||
193 | check_assist( | 193 | check_assist( |
194 | make_raw_string, | 194 | make_raw_string, |
195 | r###" | 195 | r###" |
196 | fn f() { | 196 | fn f() { |
197 | let s = <|>"#random##\nstring"; | 197 | let s = <|>"#random##\nstring"; |
198 | } | 198 | } |
199 | "###, | 199 | "###, |
200 | r####" | 200 | r####" |
201 | fn f() { | 201 | fn f() { |
202 | let s = r#"#random## | 202 | let s = r#"#random## |
203 | string"#; | 203 | string"#; |
204 | } | 204 | } |
205 | "####, | 205 | "####, |
206 | ) | 206 | ) |
207 | } | 207 | } |
208 | 208 | ||
@@ -211,16 +211,16 @@ string"#; | |||
211 | check_assist( | 211 | check_assist( |
212 | make_raw_string, | 212 | make_raw_string, |
213 | r###" | 213 | r###" |
214 | fn f() { | 214 | fn f() { |
215 | let s = <|>"#random\"##\nstring"; | 215 | let s = <|>"#random\"##\nstring"; |
216 | } | 216 | } |
217 | "###, | 217 | "###, |
218 | r####" | 218 | r####" |
219 | fn f() { | 219 | fn f() { |
220 | let s = r###"#random"## | 220 | let s = r###"#random"## |
221 | string"###; | 221 | string"###; |
222 | } | 222 | } |
223 | "####, | 223 | "####, |
224 | ) | 224 | ) |
225 | } | 225 | } |
226 | 226 | ||
diff --git a/crates/ra_assists/src/handlers/split_import.rs b/crates/ra_assists/src/handlers/split_import.rs index c7a874480..38aa199a0 100644 --- a/crates/ra_assists/src/handlers/split_import.rs +++ b/crates/ra_assists/src/handlers/split_import.rs | |||
@@ -66,4 +66,14 @@ mod tests { | |||
66 | fn issue4044() { | 66 | fn issue4044() { |
67 | check_assist_not_applicable(split_import, "use crate::<|>:::self;") | 67 | check_assist_not_applicable(split_import, "use crate::<|>:::self;") |
68 | } | 68 | } |
69 | |||
70 | #[test] | ||
71 | fn test_empty_use() { | ||
72 | check_assist_not_applicable( | ||
73 | split_import, | ||
74 | r" | ||
75 | use std::<|> | ||
76 | fn main() {}", | ||
77 | ); | ||
78 | } | ||
69 | } | 79 | } |
diff --git a/crates/ra_assists/src/tests.rs b/crates/ra_assists/src/tests.rs index 62dd3547f..858f5ca80 100644 --- a/crates/ra_assists/src/tests.rs +++ b/crates/ra_assists/src/tests.rs | |||
@@ -1,27 +1,21 @@ | |||
1 | mod generated; | 1 | mod generated; |
2 | 2 | ||
3 | use std::sync::Arc; | ||
4 | |||
5 | use hir::Semantics; | 3 | use hir::Semantics; |
6 | use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; | 4 | use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; |
7 | use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase}; | 5 | use ra_ide_db::RootDatabase; |
8 | use ra_syntax::TextRange; | 6 | use ra_syntax::TextRange; |
9 | use test_utils::{ | 7 | use test_utils::{assert_eq_text, extract_offset, extract_range}; |
10 | assert_eq_text, extract_offset, extract_range, extract_range_or_offset, RangeOrOffset, | ||
11 | }; | ||
12 | 8 | ||
13 | use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, Assists}; | 9 | use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, Assists}; |
10 | use stdx::trim_indent; | ||
14 | 11 | ||
15 | pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { | 12 | pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { |
16 | let (mut db, file_id) = RootDatabase::with_single_file(text); | 13 | RootDatabase::with_single_file(text) |
17 | // FIXME: ideally, this should be done by the above `RootDatabase::with_single_file`, | ||
18 | // but it looks like this might need specialization? :( | ||
19 | db.set_local_roots(Arc::new(vec![db.file_source_root(file_id)])); | ||
20 | (db, file_id) | ||
21 | } | 14 | } |
22 | 15 | ||
23 | pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { | 16 | pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { |
24 | check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after)); | 17 | let ra_fixture_after = trim_indent(ra_fixture_after); |
18 | check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after)); | ||
25 | } | 19 | } |
26 | 20 | ||
27 | // FIXME: instead of having a separate function here, maybe use | 21 | // FIXME: instead of having a separate function here, maybe use |
@@ -36,8 +30,9 @@ pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) { | |||
36 | } | 30 | } |
37 | 31 | ||
38 | fn check_doc_test(assist_id: &str, before: &str, after: &str) { | 32 | fn check_doc_test(assist_id: &str, before: &str, after: &str) { |
39 | let (selection, before) = extract_range_or_offset(before); | 33 | let after = trim_indent(after); |
40 | let (db, file_id) = crate::tests::with_single_file(&before); | 34 | let (db, file_id, selection) = RootDatabase::with_range_or_offset(&before); |
35 | let before = db.file_text(file_id).to_string(); | ||
41 | let frange = FileRange { file_id, range: selection.into() }; | 36 | let frange = FileRange { file_id, range: selection.into() }; |
42 | 37 | ||
43 | let mut assist = Assist::resolved(&db, &AssistConfig::default(), frange) | 38 | let mut assist = Assist::resolved(&db, &AssistConfig::default(), frange) |
@@ -57,11 +52,11 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) { | |||
57 | 52 | ||
58 | let actual = { | 53 | let actual = { |
59 | let change = assist.source_change.source_file_edits.pop().unwrap(); | 54 | let change = assist.source_change.source_file_edits.pop().unwrap(); |
60 | let mut actual = before.clone(); | 55 | let mut actual = before; |
61 | change.edit.apply(&mut actual); | 56 | change.edit.apply(&mut actual); |
62 | actual | 57 | actual |
63 | }; | 58 | }; |
64 | assert_eq_text!(after, &actual); | 59 | assert_eq_text!(&after, &actual); |
65 | } | 60 | } |
66 | 61 | ||
67 | enum ExpectedResult<'a> { | 62 | enum ExpectedResult<'a> { |
@@ -71,20 +66,8 @@ enum ExpectedResult<'a> { | |||
71 | } | 66 | } |
72 | 67 | ||
73 | fn check(handler: Handler, before: &str, expected: ExpectedResult) { | 68 | fn check(handler: Handler, before: &str, expected: ExpectedResult) { |
74 | let (text_without_caret, file_with_caret_id, range_or_offset, db) = if before.contains("//-") { | 69 | let (db, file_with_caret_id, range_or_offset) = RootDatabase::with_range_or_offset(before); |
75 | let (mut db, position) = RootDatabase::with_position(before); | 70 | let text_without_caret = db.file_text(file_with_caret_id).to_string(); |
76 | db.set_local_roots(Arc::new(vec![db.file_source_root(position.file_id)])); | ||
77 | ( | ||
78 | db.file_text(position.file_id).as_ref().to_owned(), | ||
79 | position.file_id, | ||
80 | RangeOrOffset::Offset(position.offset), | ||
81 | db, | ||
82 | ) | ||
83 | } else { | ||
84 | let (range_or_offset, text_without_caret) = extract_range_or_offset(before); | ||
85 | let (db, file_id) = with_single_file(&text_without_caret); | ||
86 | (text_without_caret, file_id, range_or_offset, db) | ||
87 | }; | ||
88 | 71 | ||
89 | let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; | 72 | let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() }; |
90 | 73 | ||
diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index c1ff0de7b..b7c45a619 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs | |||
@@ -198,8 +198,7 @@ pub(crate) struct FamousDefs<'a, 'b>(pub(crate) &'a Semantics<'b, RootDatabase>, | |||
198 | #[allow(non_snake_case)] | 198 | #[allow(non_snake_case)] |
199 | impl FamousDefs<'_, '_> { | 199 | impl FamousDefs<'_, '_> { |
200 | #[cfg(test)] | 200 | #[cfg(test)] |
201 | pub(crate) const FIXTURE: &'static str = r#" | 201 | pub(crate) const FIXTURE: &'static str = r#"//- /libcore.rs crate:core |
202 | //- /libcore.rs crate:core | ||
203 | pub mod convert { | 202 | pub mod convert { |
204 | pub trait From<T> { | 203 | pub trait From<T> { |
205 | fn from(T) -> Self; | 204 | fn from(T) -> Self; |
diff --git a/crates/ra_db/Cargo.toml b/crates/ra_db/Cargo.toml index 8ab409158..372fb242b 100644 --- a/crates/ra_db/Cargo.toml +++ b/crates/ra_db/Cargo.toml | |||
@@ -17,3 +17,5 @@ ra_cfg = { path = "../ra_cfg" } | |||
17 | ra_prof = { path = "../ra_prof" } | 17 | ra_prof = { path = "../ra_prof" } |
18 | ra_tt = { path = "../ra_tt" } | 18 | ra_tt = { path = "../ra_tt" } |
19 | test_utils = { path = "../test_utils" } | 19 | test_utils = { path = "../test_utils" } |
20 | vfs = { path = "../vfs" } | ||
21 | stdx = { path = "../stdx" } | ||
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 482a2f3e6..4f4fb4494 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs | |||
@@ -57,17 +57,16 @@ | |||
57 | //! fn insert_source_code_here() {} | 57 | //! fn insert_source_code_here() {} |
58 | //! " | 58 | //! " |
59 | //! ``` | 59 | //! ``` |
60 | 60 | use std::{str::FromStr, sync::Arc}; | |
61 | use std::str::FromStr; | ||
62 | use std::sync::Arc; | ||
63 | 61 | ||
64 | use ra_cfg::CfgOptions; | 62 | use ra_cfg::CfgOptions; |
65 | use rustc_hash::FxHashMap; | 63 | use rustc_hash::FxHashMap; |
66 | use test_utils::{extract_offset, parse_fixture, parse_single_fixture, FixtureMeta, CURSOR_MARKER}; | 64 | use test_utils::{extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER}; |
65 | use vfs::{file_set::FileSet, VfsPath}; | ||
67 | 66 | ||
68 | use crate::{ | 67 | use crate::{ |
69 | input::CrateName, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, RelativePathBuf, | 68 | input::CrateName, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, SourceDatabaseExt, |
70 | SourceDatabaseExt, SourceRoot, SourceRootId, | 69 | SourceRoot, SourceRootId, |
71 | }; | 70 | }; |
72 | 71 | ||
73 | pub const WORKSPACE: SourceRootId = SourceRootId(0); | 72 | pub const WORKSPACE: SourceRootId = SourceRootId(0); |
@@ -75,21 +74,32 @@ pub const WORKSPACE: SourceRootId = SourceRootId(0); | |||
75 | pub trait WithFixture: Default + SourceDatabaseExt + 'static { | 74 | pub trait WithFixture: Default + SourceDatabaseExt + 'static { |
76 | fn with_single_file(text: &str) -> (Self, FileId) { | 75 | fn with_single_file(text: &str) -> (Self, FileId) { |
77 | let mut db = Self::default(); | 76 | let mut db = Self::default(); |
78 | let file_id = with_single_file(&mut db, text); | 77 | let (_, files) = with_files(&mut db, text); |
79 | (db, file_id) | 78 | assert_eq!(files.len(), 1); |
79 | (db, files[0]) | ||
80 | } | 80 | } |
81 | 81 | ||
82 | fn with_files(ra_fixture: &str) -> Self { | 82 | fn with_files(ra_fixture: &str) -> Self { |
83 | let mut db = Self::default(); | 83 | let mut db = Self::default(); |
84 | let pos = with_files(&mut db, ra_fixture); | 84 | let (pos, _) = with_files(&mut db, ra_fixture); |
85 | assert!(pos.is_none()); | 85 | assert!(pos.is_none()); |
86 | db | 86 | db |
87 | } | 87 | } |
88 | 88 | ||
89 | fn with_position(ra_fixture: &str) -> (Self, FilePosition) { | 89 | fn with_position(ra_fixture: &str) -> (Self, FilePosition) { |
90 | let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture); | ||
91 | let offset = match range_or_offset { | ||
92 | RangeOrOffset::Range(_) => panic!(), | ||
93 | RangeOrOffset::Offset(it) => it, | ||
94 | }; | ||
95 | (db, FilePosition { file_id, offset }) | ||
96 | } | ||
97 | |||
98 | fn with_range_or_offset(ra_fixture: &str) -> (Self, FileId, RangeOrOffset) { | ||
90 | let mut db = Self::default(); | 99 | let mut db = Self::default(); |
91 | let pos = with_files(&mut db, ra_fixture); | 100 | let (pos, _) = with_files(&mut db, ra_fixture); |
92 | (db, pos.unwrap()) | 101 | let (file_id, range_or_offset) = pos.unwrap(); |
102 | (db, file_id, range_or_offset) | ||
93 | } | 103 | } |
94 | 104 | ||
95 | fn test_crate(&self) -> CrateId { | 105 | fn test_crate(&self) -> CrateId { |
@@ -103,83 +113,36 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static { | |||
103 | 113 | ||
104 | impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {} | 114 | impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {} |
105 | 115 | ||
106 | fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId { | 116 | fn with_files( |
107 | let file_id = FileId(0); | 117 | db: &mut dyn SourceDatabaseExt, |
108 | let rel_path: RelativePathBuf = "/main.rs".into(); | 118 | fixture: &str, |
109 | 119 | ) -> (Option<(FileId, RangeOrOffset)>, Vec<FileId>) { | |
110 | let mut source_root = SourceRoot::new_local(); | 120 | let fixture = Fixture::parse(fixture); |
111 | source_root.insert_file(rel_path.clone(), file_id); | ||
112 | |||
113 | let fixture = parse_single_fixture(ra_fixture); | ||
114 | |||
115 | let crate_graph = if let Some(entry) = fixture { | ||
116 | let meta = match ParsedMeta::from(&entry.meta) { | ||
117 | ParsedMeta::File(it) => it, | ||
118 | _ => panic!("with_single_file only support file meta"), | ||
119 | }; | ||
120 | |||
121 | let mut crate_graph = CrateGraph::default(); | ||
122 | crate_graph.add_crate_root( | ||
123 | file_id, | ||
124 | meta.edition, | ||
125 | meta.krate.map(|name| { | ||
126 | CrateName::new(&name).expect("Fixture crate name should not contain dashes") | ||
127 | }), | ||
128 | meta.cfg, | ||
129 | meta.env, | ||
130 | Default::default(), | ||
131 | Default::default(), | ||
132 | ); | ||
133 | crate_graph | ||
134 | } else { | ||
135 | let mut crate_graph = CrateGraph::default(); | ||
136 | crate_graph.add_crate_root( | ||
137 | file_id, | ||
138 | Edition::Edition2018, | ||
139 | None, | ||
140 | CfgOptions::default(), | ||
141 | Env::default(), | ||
142 | Default::default(), | ||
143 | Default::default(), | ||
144 | ); | ||
145 | crate_graph | ||
146 | }; | ||
147 | |||
148 | db.set_file_text(file_id, Arc::new(ra_fixture.to_string())); | ||
149 | db.set_file_relative_path(file_id, rel_path); | ||
150 | db.set_file_source_root(file_id, WORKSPACE); | ||
151 | db.set_source_root(WORKSPACE, Arc::new(source_root)); | ||
152 | db.set_crate_graph(Arc::new(crate_graph)); | ||
153 | |||
154 | file_id | ||
155 | } | ||
156 | |||
157 | fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosition> { | ||
158 | let fixture = parse_fixture(fixture); | ||
159 | 121 | ||
122 | let mut files = Vec::new(); | ||
160 | let mut crate_graph = CrateGraph::default(); | 123 | let mut crate_graph = CrateGraph::default(); |
161 | let mut crates = FxHashMap::default(); | 124 | let mut crates = FxHashMap::default(); |
162 | let mut crate_deps = Vec::new(); | 125 | let mut crate_deps = Vec::new(); |
163 | let mut default_crate_root: Option<FileId> = None; | 126 | let mut default_crate_root: Option<FileId> = None; |
164 | 127 | ||
165 | let mut source_root = SourceRoot::new_local(); | 128 | let mut file_set = FileSet::default(); |
166 | let mut source_root_id = WORKSPACE; | 129 | let source_root_id = WORKSPACE; |
167 | let mut source_root_prefix: RelativePathBuf = "/".into(); | 130 | let source_root_prefix = "/".to_string(); |
168 | let mut file_id = FileId(0); | 131 | let mut file_id = FileId(0); |
169 | 132 | ||
170 | let mut file_position = None; | 133 | let mut file_position = None; |
171 | 134 | ||
172 | for entry in fixture.iter() { | 135 | for entry in fixture { |
173 | let meta = match ParsedMeta::from(&entry.meta) { | 136 | let text = if entry.text.contains(CURSOR_MARKER) { |
174 | ParsedMeta::Root { path } => { | 137 | let (range_or_offset, text) = extract_range_or_offset(&entry.text); |
175 | let source_root = std::mem::replace(&mut source_root, SourceRoot::new_local()); | 138 | assert!(file_position.is_none()); |
176 | db.set_source_root(source_root_id, Arc::new(source_root)); | 139 | file_position = Some((file_id, range_or_offset)); |
177 | source_root_id.0 += 1; | 140 | text.to_string() |
178 | source_root_prefix = path; | 141 | } else { |
179 | continue; | 142 | entry.text.clone() |
180 | } | ||
181 | ParsedMeta::File(it) => it, | ||
182 | }; | 143 | }; |
144 | |||
145 | let meta = FileMeta::from(entry); | ||
183 | assert!(meta.path.starts_with(&source_root_prefix)); | 146 | assert!(meta.path.starts_with(&source_root_prefix)); |
184 | 147 | ||
185 | if let Some(krate) = meta.krate { | 148 | if let Some(krate) = meta.krate { |
@@ -190,7 +153,6 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
190 | meta.cfg, | 153 | meta.cfg, |
191 | meta.env, | 154 | meta.env, |
192 | Default::default(), | 155 | Default::default(), |
193 | Default::default(), | ||
194 | ); | 156 | ); |
195 | let prev = crates.insert(krate.clone(), crate_id); | 157 | let prev = crates.insert(krate.clone(), crate_id); |
196 | assert!(prev.is_none()); | 158 | assert!(prev.is_none()); |
@@ -202,20 +164,11 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
202 | default_crate_root = Some(file_id); | 164 | default_crate_root = Some(file_id); |
203 | } | 165 | } |
204 | 166 | ||
205 | let text = if entry.text.contains(CURSOR_MARKER) { | ||
206 | let (offset, text) = extract_offset(&entry.text); | ||
207 | assert!(file_position.is_none()); | ||
208 | file_position = Some(FilePosition { file_id, offset }); | ||
209 | text.to_string() | ||
210 | } else { | ||
211 | entry.text.to_string() | ||
212 | }; | ||
213 | |||
214 | db.set_file_text(file_id, Arc::new(text)); | 167 | db.set_file_text(file_id, Arc::new(text)); |
215 | db.set_file_relative_path(file_id, meta.path.clone()); | ||
216 | db.set_file_source_root(file_id, source_root_id); | 168 | db.set_file_source_root(file_id, source_root_id); |
217 | source_root.insert_file(meta.path, file_id); | 169 | let path = VfsPath::new_virtual_path(meta.path); |
218 | 170 | file_set.insert(file_id, path.into()); | |
171 | files.push(file_id); | ||
219 | file_id.0 += 1; | 172 | file_id.0 += 1; |
220 | } | 173 | } |
221 | 174 | ||
@@ -228,7 +181,6 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
228 | CfgOptions::default(), | 181 | CfgOptions::default(), |
229 | Env::default(), | 182 | Env::default(), |
230 | Default::default(), | 183 | Default::default(), |
231 | Default::default(), | ||
232 | ); | 184 | ); |
233 | } else { | 185 | } else { |
234 | for (from, to) in crate_deps { | 186 | for (from, to) in crate_deps { |
@@ -238,19 +190,14 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
238 | } | 190 | } |
239 | } | 191 | } |
240 | 192 | ||
241 | db.set_source_root(source_root_id, Arc::new(source_root)); | 193 | db.set_source_root(source_root_id, Arc::new(SourceRoot::new_local(file_set))); |
242 | db.set_crate_graph(Arc::new(crate_graph)); | 194 | db.set_crate_graph(Arc::new(crate_graph)); |
243 | 195 | ||
244 | file_position | 196 | (file_position, files) |
245 | } | ||
246 | |||
247 | enum ParsedMeta { | ||
248 | Root { path: RelativePathBuf }, | ||
249 | File(FileMeta), | ||
250 | } | 197 | } |
251 | 198 | ||
252 | struct FileMeta { | 199 | struct FileMeta { |
253 | path: RelativePathBuf, | 200 | path: String, |
254 | krate: Option<String>, | 201 | krate: Option<String>, |
255 | deps: Vec<String>, | 202 | deps: Vec<String>, |
256 | cfg: CfgOptions, | 203 | cfg: CfgOptions, |
@@ -258,25 +205,22 @@ struct FileMeta { | |||
258 | env: Env, | 205 | env: Env, |
259 | } | 206 | } |
260 | 207 | ||
261 | impl From<&FixtureMeta> for ParsedMeta { | 208 | impl From<Fixture> for FileMeta { |
262 | fn from(meta: &FixtureMeta) -> Self { | 209 | fn from(f: Fixture) -> FileMeta { |
263 | match meta { | 210 | let mut cfg = CfgOptions::default(); |
264 | FixtureMeta::Root { path } => { | 211 | f.cfg_atoms.iter().for_each(|it| cfg.insert_atom(it.into())); |
265 | // `Self::Root` causes a false warning: 'variant is never constructed: `Root` ' | 212 | f.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into())); |
266 | // see https://github.com/rust-lang/rust/issues/69018 | 213 | |
267 | ParsedMeta::Root { path: path.to_owned() } | 214 | FileMeta { |
268 | } | 215 | path: f.path, |
269 | FixtureMeta::File(f) => Self::File(FileMeta { | 216 | krate: f.krate, |
270 | path: f.path.to_owned(), | 217 | deps: f.deps, |
271 | krate: f.crate_name.to_owned(), | 218 | cfg, |
272 | deps: f.deps.to_owned(), | 219 | edition: f |
273 | cfg: f.cfg.to_owned(), | 220 | .edition |
274 | edition: f | 221 | .as_ref() |
275 | .edition | 222 | .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()), |
276 | .as_ref() | 223 | env: Env::from(f.env.iter()), |
277 | .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()), | ||
278 | env: Env::from(f.env.iter()), | ||
279 | }), | ||
280 | } | 224 | } |
281 | } | 225 | } |
282 | } | 226 | } |
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index bf26048f2..7f3660118 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs | |||
@@ -6,27 +6,15 @@ | |||
6 | //! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how | 6 | //! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how |
7 | //! actual IO is done and lowered to input. | 7 | //! actual IO is done and lowered to input. |
8 | 8 | ||
9 | use std::{ | 9 | use std::{fmt, ops, str::FromStr, sync::Arc}; |
10 | fmt, ops, | ||
11 | path::{Path, PathBuf}, | ||
12 | str::FromStr, | ||
13 | sync::Arc, | ||
14 | }; | ||
15 | 10 | ||
16 | use ra_cfg::CfgOptions; | 11 | use ra_cfg::CfgOptions; |
17 | use ra_syntax::SmolStr; | 12 | use ra_syntax::SmolStr; |
18 | use ra_tt::TokenExpander; | 13 | use ra_tt::TokenExpander; |
19 | use rustc_hash::{FxHashMap, FxHashSet}; | 14 | use rustc_hash::{FxHashMap, FxHashSet}; |
15 | use vfs::file_set::FileSet; | ||
20 | 16 | ||
21 | use crate::{RelativePath, RelativePathBuf}; | 17 | pub use vfs::FileId; |
22 | |||
23 | /// `FileId` is an integer which uniquely identifies a file. File paths are | ||
24 | /// messy and system-dependent, so most of the code should work directly with | ||
25 | /// `FileId`, without inspecting the path. The mapping between `FileId` and path | ||
26 | /// and `SourceRoot` is constant. A file rename is represented as a pair of | ||
27 | /// deletion/creation. | ||
28 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
29 | pub struct FileId(pub u32); | ||
30 | 18 | ||
31 | /// Files are grouped into source roots. A source root is a directory on the | 19 | /// Files are grouped into source roots. A source root is a directory on the |
32 | /// file systems which is watched for changes. Typically it corresponds to a | 20 | /// file systems which is watched for changes. Typically it corresponds to a |
@@ -45,27 +33,18 @@ pub struct SourceRoot { | |||
45 | /// Libraries are considered mostly immutable, this assumption is used to | 33 | /// Libraries are considered mostly immutable, this assumption is used to |
46 | /// optimize salsa's query structure | 34 | /// optimize salsa's query structure |
47 | pub is_library: bool, | 35 | pub is_library: bool, |
48 | files: FxHashMap<RelativePathBuf, FileId>, | 36 | pub(crate) file_set: FileSet, |
49 | } | 37 | } |
50 | 38 | ||
51 | impl SourceRoot { | 39 | impl SourceRoot { |
52 | pub fn new_local() -> SourceRoot { | 40 | pub fn new_local(file_set: FileSet) -> SourceRoot { |
53 | SourceRoot { is_library: false, files: Default::default() } | 41 | SourceRoot { is_library: false, file_set } |
54 | } | ||
55 | pub fn new_library() -> SourceRoot { | ||
56 | SourceRoot { is_library: true, files: Default::default() } | ||
57 | } | ||
58 | pub fn insert_file(&mut self, path: RelativePathBuf, file_id: FileId) { | ||
59 | self.files.insert(path, file_id); | ||
60 | } | 42 | } |
61 | pub fn remove_file(&mut self, path: &RelativePath) { | 43 | pub fn new_library(file_set: FileSet) -> SourceRoot { |
62 | self.files.remove(path); | 44 | SourceRoot { is_library: true, file_set } |
63 | } | 45 | } |
64 | pub fn walk(&self) -> impl Iterator<Item = FileId> + '_ { | 46 | pub fn iter(&self) -> impl Iterator<Item = FileId> + '_ { |
65 | self.files.values().copied() | 47 | self.file_set.iter() |
66 | } | ||
67 | pub fn file_by_relative_path(&self, path: &RelativePath) -> Option<FileId> { | ||
68 | self.files.get(path).copied() | ||
69 | } | 48 | } |
70 | } | 49 | } |
71 | 50 | ||
@@ -141,7 +120,6 @@ pub struct CrateData { | |||
141 | pub display_name: Option<CrateName>, | 120 | pub display_name: Option<CrateName>, |
142 | pub cfg_options: CfgOptions, | 121 | pub cfg_options: CfgOptions, |
143 | pub env: Env, | 122 | pub env: Env, |
144 | pub extern_source: ExternSource, | ||
145 | pub dependencies: Vec<Dependency>, | 123 | pub dependencies: Vec<Dependency>, |
146 | pub proc_macro: Vec<ProcMacro>, | 124 | pub proc_macro: Vec<ProcMacro>, |
147 | } | 125 | } |
@@ -152,22 +130,11 @@ pub enum Edition { | |||
152 | Edition2015, | 130 | Edition2015, |
153 | } | 131 | } |
154 | 132 | ||
155 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
156 | pub struct ExternSourceId(pub u32); | ||
157 | |||
158 | #[derive(Default, Debug, Clone, PartialEq, Eq)] | 133 | #[derive(Default, Debug, Clone, PartialEq, Eq)] |
159 | pub struct Env { | 134 | pub struct Env { |
160 | entries: FxHashMap<String, String>, | 135 | entries: FxHashMap<String, String>, |
161 | } | 136 | } |
162 | 137 | ||
163 | // FIXME: Redesign vfs for solve the following limitation ? | ||
164 | // Note: Some env variables (e.g. OUT_DIR) are located outside of the | ||
165 | // crate. We store a map to allow remap it to ExternSourceId | ||
166 | #[derive(Default, Debug, Clone, PartialEq, Eq)] | ||
167 | pub struct ExternSource { | ||
168 | extern_paths: FxHashMap<PathBuf, ExternSourceId>, | ||
169 | } | ||
170 | |||
171 | #[derive(Debug, Clone, PartialEq, Eq)] | 138 | #[derive(Debug, Clone, PartialEq, Eq)] |
172 | pub struct Dependency { | 139 | pub struct Dependency { |
173 | pub crate_id: CrateId, | 140 | pub crate_id: CrateId, |
@@ -182,7 +149,6 @@ impl CrateGraph { | |||
182 | display_name: Option<CrateName>, | 149 | display_name: Option<CrateName>, |
183 | cfg_options: CfgOptions, | 150 | cfg_options: CfgOptions, |
184 | env: Env, | 151 | env: Env, |
185 | extern_source: ExternSource, | ||
186 | proc_macro: Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)>, | 152 | proc_macro: Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)>, |
187 | ) -> CrateId { | 153 | ) -> CrateId { |
188 | let proc_macro = | 154 | let proc_macro = |
@@ -194,7 +160,6 @@ impl CrateGraph { | |||
194 | display_name, | 160 | display_name, |
195 | cfg_options, | 161 | cfg_options, |
196 | env, | 162 | env, |
197 | extern_source, | ||
198 | proc_macro, | 163 | proc_macro, |
199 | dependencies: Vec::new(), | 164 | dependencies: Vec::new(), |
200 | }; | 165 | }; |
@@ -254,12 +219,12 @@ impl CrateGraph { | |||
254 | return false; | 219 | return false; |
255 | } | 220 | } |
256 | 221 | ||
222 | if target == from { | ||
223 | return true; | ||
224 | } | ||
225 | |||
257 | for dep in &self[from].dependencies { | 226 | for dep in &self[from].dependencies { |
258 | let crate_id = dep.crate_id; | 227 | let crate_id = dep.crate_id; |
259 | if crate_id == target { | ||
260 | return true; | ||
261 | } | ||
262 | |||
263 | if self.dfs_find(target, crate_id, visited) { | 228 | if self.dfs_find(target, crate_id, visited) { |
264 | return true; | 229 | return true; |
265 | } | 230 | } |
@@ -334,20 +299,6 @@ impl Env { | |||
334 | } | 299 | } |
335 | } | 300 | } |
336 | 301 | ||
337 | impl ExternSource { | ||
338 | pub fn extern_path(&self, path: &Path) -> Option<(ExternSourceId, RelativePathBuf)> { | ||
339 | self.extern_paths.iter().find_map(|(root_path, id)| { | ||
340 | let rel_path = path.strip_prefix(root_path).ok()?; | ||
341 | let rel_path = RelativePathBuf::from_path(rel_path).ok()?; | ||
342 | Some((*id, rel_path)) | ||
343 | }) | ||
344 | } | ||
345 | |||
346 | pub fn set_extern_path(&mut self, root_path: &Path, root: ExternSourceId) { | ||
347 | self.extern_paths.insert(root_path.to_path_buf(), root); | ||
348 | } | ||
349 | } | ||
350 | |||
351 | #[derive(Debug)] | 302 | #[derive(Debug)] |
352 | pub struct ParseEditionError { | 303 | pub struct ParseEditionError { |
353 | invalid_input: String, | 304 | invalid_input: String, |
@@ -369,7 +320,7 @@ mod tests { | |||
369 | use super::{CfgOptions, CrateGraph, CrateName, Dependency, Edition::Edition2018, Env, FileId}; | 320 | use super::{CfgOptions, CrateGraph, CrateName, Dependency, Edition::Edition2018, Env, FileId}; |
370 | 321 | ||
371 | #[test] | 322 | #[test] |
372 | fn it_should_panic_because_of_cycle_dependencies() { | 323 | fn detect_cyclic_dependency_indirect() { |
373 | let mut graph = CrateGraph::default(); | 324 | let mut graph = CrateGraph::default(); |
374 | let crate1 = graph.add_crate_root( | 325 | let crate1 = graph.add_crate_root( |
375 | FileId(1u32), | 326 | FileId(1u32), |
@@ -378,7 +329,6 @@ mod tests { | |||
378 | CfgOptions::default(), | 329 | CfgOptions::default(), |
379 | Env::default(), | 330 | Env::default(), |
380 | Default::default(), | 331 | Default::default(), |
381 | Default::default(), | ||
382 | ); | 332 | ); |
383 | let crate2 = graph.add_crate_root( | 333 | let crate2 = graph.add_crate_root( |
384 | FileId(2u32), | 334 | FileId(2u32), |
@@ -387,7 +337,6 @@ mod tests { | |||
387 | CfgOptions::default(), | 337 | CfgOptions::default(), |
388 | Env::default(), | 338 | Env::default(), |
389 | Default::default(), | 339 | Default::default(), |
390 | Default::default(), | ||
391 | ); | 340 | ); |
392 | let crate3 = graph.add_crate_root( | 341 | let crate3 = graph.add_crate_root( |
393 | FileId(3u32), | 342 | FileId(3u32), |
@@ -396,7 +345,6 @@ mod tests { | |||
396 | CfgOptions::default(), | 345 | CfgOptions::default(), |
397 | Env::default(), | 346 | Env::default(), |
398 | Default::default(), | 347 | Default::default(), |
399 | Default::default(), | ||
400 | ); | 348 | ); |
401 | assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); | 349 | assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); |
402 | assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); | 350 | assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); |
@@ -404,7 +352,7 @@ mod tests { | |||
404 | } | 352 | } |
405 | 353 | ||
406 | #[test] | 354 | #[test] |
407 | fn it_works() { | 355 | fn detect_cyclic_dependency_direct() { |
408 | let mut graph = CrateGraph::default(); | 356 | let mut graph = CrateGraph::default(); |
409 | let crate1 = graph.add_crate_root( | 357 | let crate1 = graph.add_crate_root( |
410 | FileId(1u32), | 358 | FileId(1u32), |
@@ -413,7 +361,6 @@ mod tests { | |||
413 | CfgOptions::default(), | 361 | CfgOptions::default(), |
414 | Env::default(), | 362 | Env::default(), |
415 | Default::default(), | 363 | Default::default(), |
416 | Default::default(), | ||
417 | ); | 364 | ); |
418 | let crate2 = graph.add_crate_root( | 365 | let crate2 = graph.add_crate_root( |
419 | FileId(2u32), | 366 | FileId(2u32), |
@@ -422,6 +369,28 @@ mod tests { | |||
422 | CfgOptions::default(), | 369 | CfgOptions::default(), |
423 | Env::default(), | 370 | Env::default(), |
424 | Default::default(), | 371 | Default::default(), |
372 | ); | ||
373 | assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); | ||
374 | assert!(graph.add_dep(crate2, CrateName::new("crate2").unwrap(), crate2).is_err()); | ||
375 | } | ||
376 | |||
377 | #[test] | ||
378 | fn it_works() { | ||
379 | let mut graph = CrateGraph::default(); | ||
380 | let crate1 = graph.add_crate_root( | ||
381 | FileId(1u32), | ||
382 | Edition2018, | ||
383 | None, | ||
384 | CfgOptions::default(), | ||
385 | Env::default(), | ||
386 | Default::default(), | ||
387 | ); | ||
388 | let crate2 = graph.add_crate_root( | ||
389 | FileId(2u32), | ||
390 | Edition2018, | ||
391 | None, | ||
392 | CfgOptions::default(), | ||
393 | Env::default(), | ||
425 | Default::default(), | 394 | Default::default(), |
426 | ); | 395 | ); |
427 | let crate3 = graph.add_crate_root( | 396 | let crate3 = graph.add_crate_root( |
@@ -431,7 +400,6 @@ mod tests { | |||
431 | CfgOptions::default(), | 400 | CfgOptions::default(), |
432 | Env::default(), | 401 | Env::default(), |
433 | Default::default(), | 402 | Default::default(), |
434 | Default::default(), | ||
435 | ); | 403 | ); |
436 | assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); | 404 | assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); |
437 | assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); | 405 | assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); |
@@ -447,7 +415,6 @@ mod tests { | |||
447 | CfgOptions::default(), | 415 | CfgOptions::default(), |
448 | Env::default(), | 416 | Env::default(), |
449 | Default::default(), | 417 | Default::default(), |
450 | Default::default(), | ||
451 | ); | 418 | ); |
452 | let crate2 = graph.add_crate_root( | 419 | let crate2 = graph.add_crate_root( |
453 | FileId(2u32), | 420 | FileId(2u32), |
@@ -456,7 +423,6 @@ mod tests { | |||
456 | CfgOptions::default(), | 423 | CfgOptions::default(), |
457 | Env::default(), | 424 | Env::default(), |
458 | Default::default(), | 425 | Default::default(), |
459 | Default::default(), | ||
460 | ); | 426 | ); |
461 | assert!(graph | 427 | assert!(graph |
462 | .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) | 428 | .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) |
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 80ddb6058..4a3ba57da 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs | |||
@@ -12,12 +12,13 @@ use rustc_hash::FxHashSet; | |||
12 | pub use crate::{ | 12 | pub use crate::{ |
13 | cancellation::Canceled, | 13 | cancellation::Canceled, |
14 | input::{ | 14 | input::{ |
15 | CrateData, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, ExternSource, | 15 | CrateData, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, FileId, ProcMacroId, |
16 | ExternSourceId, FileId, ProcMacroId, SourceRoot, SourceRootId, | 16 | SourceRoot, SourceRootId, |
17 | }, | 17 | }, |
18 | }; | 18 | }; |
19 | pub use relative_path::{RelativePath, RelativePathBuf}; | 19 | pub use relative_path::{RelativePath, RelativePathBuf}; |
20 | pub use salsa; | 20 | pub use salsa; |
21 | pub use vfs::{file_set::FileSet, AbsPathBuf, VfsPath}; | ||
21 | 22 | ||
22 | #[macro_export] | 23 | #[macro_export] |
23 | macro_rules! impl_intern_key { | 24 | macro_rules! impl_intern_key { |
@@ -113,7 +114,7 @@ pub trait SourceDatabase: CheckCanceled + FileLoader + std::fmt::Debug { | |||
113 | } | 114 | } |
114 | 115 | ||
115 | fn parse_query(db: &impl SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> { | 116 | fn parse_query(db: &impl SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> { |
116 | let _p = profile("parse_query"); | 117 | let _p = profile("parse_query").detail(|| format!("{:?}", file_id)); |
117 | let text = db.file_text(file_id); | 118 | let text = db.file_text(file_id); |
118 | SourceFile::parse(&*text) | 119 | SourceFile::parse(&*text) |
119 | } | 120 | } |
@@ -125,8 +126,6 @@ pub trait SourceDatabaseExt: SourceDatabase { | |||
125 | #[salsa::input] | 126 | #[salsa::input] |
126 | fn file_text(&self, file_id: FileId) -> Arc<String>; | 127 | fn file_text(&self, file_id: FileId) -> Arc<String>; |
127 | /// Path to a file, relative to the root of its source root. | 128 | /// Path to a file, relative to the root of its source root. |
128 | #[salsa::input] | ||
129 | fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf; | ||
130 | /// Source root of the file. | 129 | /// Source root of the file. |
131 | #[salsa::input] | 130 | #[salsa::input] |
132 | fn file_source_root(&self, file_id: FileId) -> SourceRootId; | 131 | fn file_source_root(&self, file_id: FileId) -> SourceRootId; |
@@ -161,24 +160,9 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> { | |||
161 | } | 160 | } |
162 | fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> { | 161 | fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> { |
163 | // FIXME: this *somehow* should be platform agnostic... | 162 | // FIXME: this *somehow* should be platform agnostic... |
164 | if std::path::Path::new(path).is_absolute() { | 163 | let source_root = self.0.file_source_root(anchor); |
165 | let krate = *self.relevant_crates(anchor).iter().next()?; | 164 | let source_root = self.0.source_root(source_root); |
166 | let (extern_source_id, relative_file) = | 165 | source_root.file_set.resolve_path(anchor, path) |
167 | self.0.crate_graph()[krate].extern_source.extern_path(path.as_ref())?; | ||
168 | |||
169 | let source_root = self.0.source_root(SourceRootId(extern_source_id.0)); | ||
170 | source_root.file_by_relative_path(&relative_file) | ||
171 | } else { | ||
172 | let rel_path = { | ||
173 | let mut rel_path = self.0.file_relative_path(anchor); | ||
174 | assert!(rel_path.pop()); | ||
175 | rel_path.push(path); | ||
176 | rel_path.normalize() | ||
177 | }; | ||
178 | let source_root = self.0.file_source_root(anchor); | ||
179 | let source_root = self.0.source_root(source_root); | ||
180 | source_root.file_by_relative_path(&rel_path) | ||
181 | } | ||
182 | } | 166 | } |
183 | 167 | ||
184 | fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> { | 168 | fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> { |
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 1a9f6cc76..a379b9f49 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -26,15 +26,12 @@ use hir_ty::{ | |||
26 | autoderef, | 26 | autoderef, |
27 | display::{HirDisplayError, HirFormatter}, | 27 | display::{HirDisplayError, HirFormatter}, |
28 | expr::ExprValidator, | 28 | expr::ExprValidator, |
29 | method_resolution, ApplicationTy, Canonical, InEnvironment, Substs, TraitEnvironment, Ty, | 29 | method_resolution, ApplicationTy, Canonical, GenericPredicate, InEnvironment, Substs, |
30 | TyDefId, TypeCtor, | 30 | TraitEnvironment, Ty, TyDefId, TypeCtor, |
31 | }; | 31 | }; |
32 | use ra_db::{CrateId, CrateName, Edition, FileId}; | 32 | use ra_db::{CrateId, CrateName, Edition, FileId}; |
33 | use ra_prof::profile; | 33 | use ra_prof::profile; |
34 | use ra_syntax::{ | 34 | use ra_syntax::ast::{self, AttrsOwner, NameOwner}; |
35 | ast::{self, AttrsOwner, NameOwner}, | ||
36 | AstNode, | ||
37 | }; | ||
38 | use rustc_hash::FxHashSet; | 35 | use rustc_hash::FxHashSet; |
39 | 36 | ||
40 | use crate::{ | 37 | use crate::{ |
@@ -186,10 +183,27 @@ impl ModuleDef { | |||
186 | 183 | ||
187 | module.visibility_of(db, self) | 184 | module.visibility_of(db, self) |
188 | } | 185 | } |
186 | |||
187 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | ||
188 | match self { | ||
189 | ModuleDef::Adt(it) => Some(it.name(db)), | ||
190 | ModuleDef::Trait(it) => Some(it.name(db)), | ||
191 | ModuleDef::Function(it) => Some(it.name(db)), | ||
192 | ModuleDef::EnumVariant(it) => Some(it.name(db)), | ||
193 | ModuleDef::TypeAlias(it) => Some(it.name(db)), | ||
194 | |||
195 | ModuleDef::Module(it) => it.name(db), | ||
196 | ModuleDef::Const(it) => it.name(db), | ||
197 | ModuleDef::Static(it) => it.name(db), | ||
198 | |||
199 | ModuleDef::BuiltinType(it) => Some(it.as_name()), | ||
200 | } | ||
201 | } | ||
189 | } | 202 | } |
190 | 203 | ||
191 | pub use hir_def::{ | 204 | pub use hir_def::{ |
192 | attr::Attrs, item_scope::ItemInNs, visibility::Visibility, AssocItemId, AssocItemLoc, | 205 | attr::Attrs, item_scope::ItemInNs, item_tree::ItemTreeNode, visibility::Visibility, |
206 | AssocItemId, AssocItemLoc, | ||
193 | }; | 207 | }; |
194 | 208 | ||
195 | impl Module { | 209 | impl Module { |
@@ -856,7 +870,7 @@ where | |||
856 | ID: Lookup<Data = AssocItemLoc<AST>>, | 870 | ID: Lookup<Data = AssocItemLoc<AST>>, |
857 | DEF: From<ID>, | 871 | DEF: From<ID>, |
858 | CTOR: FnOnce(DEF) -> AssocItem, | 872 | CTOR: FnOnce(DEF) -> AssocItem, |
859 | AST: AstNode, | 873 | AST: ItemTreeNode, |
860 | { | 874 | { |
861 | match id.lookup(db.upcast()).container { | 875 | match id.lookup(db.upcast()).container { |
862 | AssocContainerId::TraitId(_) | AssocContainerId::ImplId(_) => Some(ctor(DEF::from(id))), | 876 | AssocContainerId::TraitId(_) | AssocContainerId::ImplId(_) => Some(ctor(DEF::from(id))), |
@@ -1359,6 +1373,27 @@ impl Type { | |||
1359 | Some(adt.into()) | 1373 | Some(adt.into()) |
1360 | } | 1374 | } |
1361 | 1375 | ||
1376 | pub fn as_dyn_trait(&self) -> Option<Trait> { | ||
1377 | self.ty.value.dyn_trait().map(Into::into) | ||
1378 | } | ||
1379 | |||
1380 | pub fn as_impl_traits(&self, db: &dyn HirDatabase) -> Option<Vec<Trait>> { | ||
1381 | self.ty.value.impl_trait_bounds(db).map(|it| { | ||
1382 | it.into_iter() | ||
1383 | .filter_map(|pred| match pred { | ||
1384 | hir_ty::GenericPredicate::Implemented(trait_ref) => { | ||
1385 | Some(Trait::from(trait_ref.trait_)) | ||
1386 | } | ||
1387 | _ => None, | ||
1388 | }) | ||
1389 | .collect() | ||
1390 | }) | ||
1391 | } | ||
1392 | |||
1393 | pub fn as_associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<Trait> { | ||
1394 | self.ty.value.associated_type_parent_trait(db).map(Into::into) | ||
1395 | } | ||
1396 | |||
1362 | // FIXME: provide required accessors such that it becomes implementable from outside. | 1397 | // FIXME: provide required accessors such that it becomes implementable from outside. |
1363 | pub fn is_equal_for_find_impls(&self, other: &Type) -> bool { | 1398 | pub fn is_equal_for_find_impls(&self, other: &Type) -> bool { |
1364 | match (&self.ty.value, &other.ty.value) { | 1399 | match (&self.ty.value, &other.ty.value) { |
@@ -1380,6 +1415,80 @@ impl Type { | |||
1380 | ty: InEnvironment { value: ty, environment: self.ty.environment.clone() }, | 1415 | ty: InEnvironment { value: ty, environment: self.ty.environment.clone() }, |
1381 | } | 1416 | } |
1382 | } | 1417 | } |
1418 | |||
1419 | pub fn walk(&self, db: &dyn HirDatabase, mut cb: impl FnMut(Type)) { | ||
1420 | // TypeWalk::walk for a Ty at first visits parameters and only after that the Ty itself. | ||
1421 | // We need a different order here. | ||
1422 | |||
1423 | fn walk_substs( | ||
1424 | db: &dyn HirDatabase, | ||
1425 | type_: &Type, | ||
1426 | substs: &Substs, | ||
1427 | cb: &mut impl FnMut(Type), | ||
1428 | ) { | ||
1429 | for ty in substs.iter() { | ||
1430 | walk_type(db, &type_.derived(ty.clone()), cb); | ||
1431 | } | ||
1432 | } | ||
1433 | |||
1434 | fn walk_bounds( | ||
1435 | db: &dyn HirDatabase, | ||
1436 | type_: &Type, | ||
1437 | bounds: &[GenericPredicate], | ||
1438 | cb: &mut impl FnMut(Type), | ||
1439 | ) { | ||
1440 | for pred in bounds { | ||
1441 | match pred { | ||
1442 | GenericPredicate::Implemented(trait_ref) => { | ||
1443 | cb(type_.clone()); | ||
1444 | walk_substs(db, type_, &trait_ref.substs, cb); | ||
1445 | } | ||
1446 | _ => (), | ||
1447 | } | ||
1448 | } | ||
1449 | } | ||
1450 | |||
1451 | fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { | ||
1452 | let ty = type_.ty.value.strip_references(); | ||
1453 | match ty { | ||
1454 | Ty::Apply(ApplicationTy { ctor, parameters }) => { | ||
1455 | match ctor { | ||
1456 | TypeCtor::Adt(_) => { | ||
1457 | cb(type_.derived(ty.clone())); | ||
1458 | } | ||
1459 | TypeCtor::AssociatedType(_) => { | ||
1460 | if let Some(_) = ty.associated_type_parent_trait(db) { | ||
1461 | cb(type_.derived(ty.clone())); | ||
1462 | } | ||
1463 | } | ||
1464 | _ => (), | ||
1465 | } | ||
1466 | |||
1467 | // adt params, tuples, etc... | ||
1468 | walk_substs(db, type_, parameters, cb); | ||
1469 | } | ||
1470 | Ty::Opaque(opaque_ty) => { | ||
1471 | if let Some(bounds) = ty.impl_trait_bounds(db) { | ||
1472 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | ||
1473 | } | ||
1474 | |||
1475 | walk_substs(db, type_, &opaque_ty.parameters, cb); | ||
1476 | } | ||
1477 | Ty::Placeholder(_) => { | ||
1478 | if let Some(bounds) = ty.impl_trait_bounds(db) { | ||
1479 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | ||
1480 | } | ||
1481 | } | ||
1482 | Ty::Dyn(bounds) => { | ||
1483 | walk_bounds(db, &type_.derived(ty.clone()), bounds.as_ref(), cb); | ||
1484 | } | ||
1485 | |||
1486 | _ => (), | ||
1487 | } | ||
1488 | } | ||
1489 | |||
1490 | walk_type(db, self, &mut cb); | ||
1491 | } | ||
1383 | } | 1492 | } |
1384 | 1493 | ||
1385 | impl HirDisplay for Type { | 1494 | impl HirDisplay for Type { |
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index b6b665de1..bb67952de 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs | |||
@@ -6,7 +6,7 @@ pub use hir_def::db::{ | |||
6 | ExprScopesQuery, FunctionDataQuery, GenericParamsQuery, ImplDataQuery, ImportMapQuery, | 6 | ExprScopesQuery, FunctionDataQuery, GenericParamsQuery, ImplDataQuery, ImportMapQuery, |
7 | InternConstQuery, InternDatabase, InternDatabaseStorage, InternEnumQuery, InternFunctionQuery, | 7 | InternConstQuery, InternDatabase, InternDatabaseStorage, InternEnumQuery, InternFunctionQuery, |
8 | InternImplQuery, InternStaticQuery, InternStructQuery, InternTraitQuery, InternTypeAliasQuery, | 8 | InternImplQuery, InternStaticQuery, InternStructQuery, InternTraitQuery, InternTypeAliasQuery, |
9 | InternUnionQuery, LangItemQuery, ModuleLangItemsQuery, RawItemsQuery, StaticDataQuery, | 9 | InternUnionQuery, ItemTreeQuery, LangItemQuery, ModuleLangItemsQuery, StaticDataQuery, |
10 | StructDataQuery, TraitDataQuery, TypeAliasDataQuery, UnionDataQuery, | 10 | StructDataQuery, TraitDataQuery, TypeAliasDataQuery, UnionDataQuery, |
11 | }; | 11 | }; |
12 | pub use hir_expand::db::{ | 12 | pub use hir_expand::db::{ |
@@ -16,7 +16,7 @@ pub use hir_expand::db::{ | |||
16 | pub use hir_ty::db::{ | 16 | pub use hir_ty::db::{ |
17 | AssociatedTyDataQuery, AssociatedTyValueQuery, CallableItemSignatureQuery, FieldTypesQuery, | 17 | AssociatedTyDataQuery, AssociatedTyValueQuery, CallableItemSignatureQuery, FieldTypesQuery, |
18 | GenericDefaultsQuery, GenericPredicatesForParamQuery, GenericPredicatesQuery, HirDatabase, | 18 | GenericDefaultsQuery, GenericPredicatesForParamQuery, GenericPredicatesQuery, HirDatabase, |
19 | HirDatabaseStorage, ImplDatumQuery, ImplSelfTyQuery, ImplTraitQuery, ImplsForTraitQuery, | 19 | HirDatabaseStorage, ImplDatumQuery, ImplSelfTyQuery, ImplTraitQuery, ImplsFromDepsQuery, |
20 | ImplsInCrateQuery, InferQueryQuery, InternAssocTyValueQuery, InternChalkImplQuery, | 20 | ImplsInCrateQuery, InferQueryQuery, InternAssocTyValueQuery, InternChalkImplQuery, |
21 | InternTypeCtorQuery, InternTypeParamIdQuery, ReturnTypeImplTraitsQuery, StructDatumQuery, | 21 | InternTypeCtorQuery, InternTypeParamIdQuery, ReturnTypeImplTraitsQuery, StructDatumQuery, |
22 | TraitDatumQuery, TraitSolveQuery, TyQuery, ValueTyQuery, | 22 | TraitDatumQuery, TraitSolveQuery, TyQuery, ValueTyQuery, |
diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index 63b8fd369..76c32fc17 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | use either::Either; | 3 | use either::Either; |
4 | use hir_def::{ | 4 | use hir_def::{ |
5 | nameres::ModuleSource, | 5 | nameres::{ModuleOrigin, ModuleSource}, |
6 | src::{HasChildSource, HasSource as _}, | 6 | src::{HasChildSource, HasSource as _}, |
7 | Lookup, VariantId, | 7 | Lookup, VariantId, |
8 | }; | 8 | }; |
@@ -29,6 +29,14 @@ impl Module { | |||
29 | def_map[self.id.local_id].definition_source(db.upcast()) | 29 | def_map[self.id.local_id].definition_source(db.upcast()) |
30 | } | 30 | } |
31 | 31 | ||
32 | pub fn is_mod_rs(self, db: &dyn HirDatabase) -> bool { | ||
33 | let def_map = db.crate_def_map(self.id.krate); | ||
34 | match def_map[self.id.local_id].origin { | ||
35 | ModuleOrigin::File { is_mod_rs, .. } => is_mod_rs, | ||
36 | _ => false, | ||
37 | } | ||
38 | } | ||
39 | |||
32 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. | 40 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. |
33 | /// `None` for the crate root. | 41 | /// `None` for the crate root. |
34 | pub fn declaration_source(self, db: &dyn HirDatabase) -> Option<InFile<ast::Module>> { | 42 | pub fn declaration_source(self, db: &dyn HirDatabase) -> Option<InFile<ast::Module>> { |
diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index a232a5856..6a49c424a 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs | |||
@@ -6,9 +6,9 @@ use std::{cell::RefCell, fmt, iter::successors}; | |||
6 | 6 | ||
7 | use hir_def::{ | 7 | use hir_def::{ |
8 | resolver::{self, HasResolver, Resolver}, | 8 | resolver::{self, HasResolver, Resolver}, |
9 | AsMacroCall, TraitId, | 9 | AsMacroCall, TraitId, VariantId, |
10 | }; | 10 | }; |
11 | use hir_expand::{hygiene::Hygiene, ExpansionInfo}; | 11 | use hir_expand::{diagnostics::AstDiagnostic, hygiene::Hygiene, ExpansionInfo}; |
12 | use hir_ty::associated_type_shorthand_candidates; | 12 | use hir_ty::associated_type_shorthand_candidates; |
13 | use itertools::Itertools; | 13 | use itertools::Itertools; |
14 | use ra_db::{FileId, FileRange}; | 14 | use ra_db::{FileId, FileRange}; |
@@ -104,6 +104,13 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
104 | tree | 104 | tree |
105 | } | 105 | } |
106 | 106 | ||
107 | pub fn ast<T: AstDiagnostic + Diagnostic>(&self, d: &T) -> <T as AstDiagnostic>::AST { | ||
108 | let file_id = d.source().file_id; | ||
109 | let root = self.db.parse_or_expand(file_id).unwrap(); | ||
110 | self.cache(root, file_id); | ||
111 | d.ast(self.db) | ||
112 | } | ||
113 | |||
107 | pub fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> { | 114 | pub fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> { |
108 | let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call); | 115 | let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call); |
109 | let sa = self.analyze2(macro_call.map(|it| it.syntax()), None); | 116 | let sa = self.analyze2(macro_call.map(|it| it.syntax()), None); |
@@ -247,6 +254,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
247 | self.analyze(path.syntax()).resolve_path(self.db, path) | 254 | self.analyze(path.syntax()).resolve_path(self.db, path) |
248 | } | 255 | } |
249 | 256 | ||
257 | pub fn resolve_variant(&self, record_lit: ast::RecordLit) -> Option<VariantId> { | ||
258 | self.analyze(record_lit.syntax()).resolve_variant(self.db, record_lit) | ||
259 | } | ||
260 | |||
250 | pub fn lower_path(&self, path: &ast::Path) -> Option<Path> { | 261 | pub fn lower_path(&self, path: &ast::Path) -> Option<Path> { |
251 | let src = self.find_file(path.syntax().clone()); | 262 | let src = self.find_file(path.syntax().clone()); |
252 | Path::from_src(path.clone(), &Hygiene::new(self.db.upcast(), src.file_id.into())) | 263 | Path::from_src(path.clone(), &Hygiene::new(self.db.upcast(), src.file_id.into())) |
diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index 7c6bbea13..1d6c47103 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs | |||
@@ -216,13 +216,43 @@ impl SourceAnalyzer { | |||
216 | if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) { | 216 | if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) { |
217 | return Some(PathResolution::AssocItem(assoc.into())); | 217 | return Some(PathResolution::AssocItem(assoc.into())); |
218 | } | 218 | } |
219 | if let Some(VariantId::EnumVariantId(variant)) = | ||
220 | self.infer.as_ref()?.variant_resolution_for_expr(expr_id) | ||
221 | { | ||
222 | return Some(PathResolution::Def(ModuleDef::EnumVariant(variant.into()))); | ||
223 | } | ||
219 | } | 224 | } |
225 | |||
220 | if let Some(path_pat) = path.syntax().parent().and_then(ast::PathPat::cast) { | 226 | if let Some(path_pat) = path.syntax().parent().and_then(ast::PathPat::cast) { |
221 | let pat_id = self.pat_id(&path_pat.into())?; | 227 | let pat_id = self.pat_id(&path_pat.into())?; |
222 | if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_pat(pat_id) { | 228 | if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_pat(pat_id) { |
223 | return Some(PathResolution::AssocItem(assoc.into())); | 229 | return Some(PathResolution::AssocItem(assoc.into())); |
224 | } | 230 | } |
231 | if let Some(VariantId::EnumVariantId(variant)) = | ||
232 | self.infer.as_ref()?.variant_resolution_for_pat(pat_id) | ||
233 | { | ||
234 | return Some(PathResolution::Def(ModuleDef::EnumVariant(variant.into()))); | ||
235 | } | ||
236 | } | ||
237 | |||
238 | if let Some(rec_lit) = path.syntax().parent().and_then(ast::RecordLit::cast) { | ||
239 | let expr_id = self.expr_id(db, &rec_lit.into())?; | ||
240 | if let Some(VariantId::EnumVariantId(variant)) = | ||
241 | self.infer.as_ref()?.variant_resolution_for_expr(expr_id) | ||
242 | { | ||
243 | return Some(PathResolution::Def(ModuleDef::EnumVariant(variant.into()))); | ||
244 | } | ||
225 | } | 245 | } |
246 | |||
247 | if let Some(rec_pat) = path.syntax().parent().and_then(ast::RecordPat::cast) { | ||
248 | let pat_id = self.pat_id(&rec_pat.into())?; | ||
249 | if let Some(VariantId::EnumVariantId(variant)) = | ||
250 | self.infer.as_ref()?.variant_resolution_for_pat(pat_id) | ||
251 | { | ||
252 | return Some(PathResolution::Def(ModuleDef::EnumVariant(variant.into()))); | ||
253 | } | ||
254 | } | ||
255 | |||
226 | // This must be a normal source file rather than macro file. | 256 | // This must be a normal source file rather than macro file. |
227 | let hir_path = | 257 | let hir_path = |
228 | crate::Path::from_src(path.clone(), &Hygiene::new(db.upcast(), self.file_id))?; | 258 | crate::Path::from_src(path.clone(), &Hygiene::new(db.upcast(), self.file_id))?; |
@@ -313,6 +343,16 @@ impl SourceAnalyzer { | |||
313 | })?; | 343 | })?; |
314 | Some(macro_call_id.as_file()) | 344 | Some(macro_call_id.as_file()) |
315 | } | 345 | } |
346 | |||
347 | pub(crate) fn resolve_variant( | ||
348 | &self, | ||
349 | db: &dyn HirDatabase, | ||
350 | record_lit: ast::RecordLit, | ||
351 | ) -> Option<VariantId> { | ||
352 | let infer = self.infer.as_ref()?; | ||
353 | let expr_id = self.expr_id(db, &record_lit.into())?; | ||
354 | infer.variant_resolution_for_expr(expr_id) | ||
355 | } | ||
316 | } | 356 | } |
317 | 357 | ||
318 | fn scope_for( | 358 | fn scope_for( |
diff --git a/crates/ra_hir_def/Cargo.toml b/crates/ra_hir_def/Cargo.toml index ef1f65ee0..6d43924e3 100644 --- a/crates/ra_hir_def/Cargo.toml +++ b/crates/ra_hir_def/Cargo.toml | |||
@@ -17,6 +17,7 @@ drop_bomb = "0.1.4" | |||
17 | fst = { version = "0.4", default-features = false } | 17 | fst = { version = "0.4", default-features = false } |
18 | itertools = "0.9.0" | 18 | itertools = "0.9.0" |
19 | indexmap = "1.4.0" | 19 | indexmap = "1.4.0" |
20 | smallvec = "1.4.0" | ||
20 | 21 | ||
21 | stdx = { path = "../stdx" } | 22 | stdx = { path = "../stdx" } |
22 | 23 | ||
diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 2eeba0572..197737ffc 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs | |||
@@ -13,7 +13,11 @@ use ra_syntax::{ | |||
13 | use tt::Subtree; | 13 | use tt::Subtree; |
14 | 14 | ||
15 | use crate::{ | 15 | use crate::{ |
16 | db::DefDatabase, nameres::ModuleSource, path::ModPath, src::HasChildSource, src::HasSource, | 16 | db::DefDatabase, |
17 | item_tree::{ItemTreeId, ItemTreeNode}, | ||
18 | nameres::ModuleSource, | ||
19 | path::ModPath, | ||
20 | src::HasChildSource, | ||
17 | AdtId, AttrDefId, Lookup, | 21 | AdtId, AttrDefId, Lookup, |
18 | }; | 22 | }; |
19 | 23 | ||
@@ -34,6 +38,8 @@ impl ops::Deref for Attrs { | |||
34 | } | 38 | } |
35 | 39 | ||
36 | impl Attrs { | 40 | impl Attrs { |
41 | pub const EMPTY: Attrs = Attrs { entries: None }; | ||
42 | |||
37 | pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Attrs { | 43 | pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Attrs { |
38 | match def { | 44 | match def { |
39 | AttrDefId::ModuleId(module) => { | 45 | AttrDefId::ModuleId(module) => { |
@@ -65,19 +71,19 @@ impl Attrs { | |||
65 | Attrs::from_attrs_owner(db, src.map(|it| it as &dyn AttrsOwner)) | 71 | Attrs::from_attrs_owner(db, src.map(|it| it as &dyn AttrsOwner)) |
66 | } | 72 | } |
67 | AttrDefId::AdtId(it) => match it { | 73 | AttrDefId::AdtId(it) => match it { |
68 | AdtId::StructId(it) => attrs_from_loc(it.lookup(db), db), | 74 | AdtId::StructId(it) => attrs_from_item_tree(it.lookup(db).id, db), |
69 | AdtId::EnumId(it) => attrs_from_loc(it.lookup(db), db), | 75 | AdtId::EnumId(it) => attrs_from_item_tree(it.lookup(db).id, db), |
70 | AdtId::UnionId(it) => attrs_from_loc(it.lookup(db), db), | 76 | AdtId::UnionId(it) => attrs_from_item_tree(it.lookup(db).id, db), |
71 | }, | 77 | }, |
72 | AttrDefId::TraitId(it) => attrs_from_loc(it.lookup(db), db), | 78 | AttrDefId::TraitId(it) => attrs_from_item_tree(it.lookup(db).id, db), |
73 | AttrDefId::MacroDefId(it) => { | 79 | AttrDefId::MacroDefId(it) => { |
74 | it.ast_id.map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db)) | 80 | it.ast_id.map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db)) |
75 | } | 81 | } |
76 | AttrDefId::ImplId(it) => attrs_from_loc(it.lookup(db), db), | 82 | AttrDefId::ImplId(it) => attrs_from_item_tree(it.lookup(db).id, db), |
77 | AttrDefId::ConstId(it) => attrs_from_loc(it.lookup(db), db), | 83 | AttrDefId::ConstId(it) => attrs_from_item_tree(it.lookup(db).id, db), |
78 | AttrDefId::StaticId(it) => attrs_from_loc(it.lookup(db), db), | 84 | AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db), |
79 | AttrDefId::FunctionId(it) => attrs_from_loc(it.lookup(db), db), | 85 | AttrDefId::FunctionId(it) => attrs_from_item_tree(it.lookup(db).id, db), |
80 | AttrDefId::TypeAliasId(it) => attrs_from_loc(it.lookup(db), db), | 86 | AttrDefId::TypeAliasId(it) => attrs_from_item_tree(it.lookup(db).id, db), |
81 | } | 87 | } |
82 | } | 88 | } |
83 | 89 | ||
@@ -103,6 +109,18 @@ impl Attrs { | |||
103 | Attrs { entries } | 109 | Attrs { entries } |
104 | } | 110 | } |
105 | 111 | ||
112 | pub fn merge(&self, other: Attrs) -> Attrs { | ||
113 | match (&self.entries, &other.entries) { | ||
114 | (None, None) => Attrs { entries: None }, | ||
115 | (Some(entries), None) | (None, Some(entries)) => { | ||
116 | Attrs { entries: Some(entries.clone()) } | ||
117 | } | ||
118 | (Some(a), Some(b)) => { | ||
119 | Attrs { entries: Some(a.iter().chain(b.iter()).cloned().collect()) } | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | |||
106 | pub fn by_key(&self, key: &'static str) -> AttrQuery<'_> { | 124 | pub fn by_key(&self, key: &'static str) -> AttrQuery<'_> { |
107 | AttrQuery { attrs: self, key } | 125 | AttrQuery { attrs: self, key } |
108 | } | 126 | } |
@@ -187,11 +205,8 @@ where | |||
187 | Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner)) | 205 | Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner)) |
188 | } | 206 | } |
189 | 207 | ||
190 | fn attrs_from_loc<T>(node: T, db: &dyn DefDatabase) -> Attrs | 208 | fn attrs_from_item_tree<N: ItemTreeNode>(id: ItemTreeId<N>, db: &dyn DefDatabase) -> Attrs { |
191 | where | 209 | let tree = db.item_tree(id.file_id); |
192 | T: HasSource, | 210 | let mod_item = N::id_to_mod_item(id.value); |
193 | T::Value: ast::AttrsOwner, | 211 | tree.attrs(mod_item).clone() |
194 | { | ||
195 | let src = node.source(db); | ||
196 | Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner)) | ||
197 | } | 212 | } |
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index f159f80af..3ced648e5 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs | |||
@@ -5,7 +5,7 @@ use either::Either; | |||
5 | use hir_expand::{ | 5 | use hir_expand::{ |
6 | hygiene::Hygiene, | 6 | hygiene::Hygiene, |
7 | name::{name, AsName, Name}, | 7 | name::{name, AsName, Name}, |
8 | HirFileId, MacroDefId, MacroDefKind, | 8 | AstId, HirFileId, MacroDefId, MacroDefKind, |
9 | }; | 9 | }; |
10 | use ra_arena::Arena; | 10 | use ra_arena::Arena; |
11 | use ra_syntax::{ | 11 | use ra_syntax::{ |
@@ -27,6 +27,7 @@ use crate::{ | |||
27 | LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, | 27 | LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, |
28 | }, | 28 | }, |
29 | item_scope::BuiltinShadowMode, | 29 | item_scope::BuiltinShadowMode, |
30 | item_tree::{FileItemTreeId, ItemTree, ItemTreeNode}, | ||
30 | path::{GenericArgs, Path}, | 31 | path::{GenericArgs, Path}, |
31 | type_ref::{Mutability, Rawness, TypeRef}, | 32 | type_ref::{Mutability, Rawness, TypeRef}, |
32 | AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, | 33 | AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, |
@@ -35,6 +36,8 @@ use crate::{ | |||
35 | 36 | ||
36 | use super::{ExprSource, PatSource}; | 37 | use super::{ExprSource, PatSource}; |
37 | use ast::AstChildren; | 38 | use ast::AstChildren; |
39 | use rustc_hash::FxHashMap; | ||
40 | use std::sync::Arc; | ||
38 | 41 | ||
39 | pub(crate) struct LowerCtx { | 42 | pub(crate) struct LowerCtx { |
40 | hygiene: Hygiene, | 43 | hygiene: Hygiene, |
@@ -60,10 +63,10 @@ pub(super) fn lower( | |||
60 | params: Option<ast::ParamList>, | 63 | params: Option<ast::ParamList>, |
61 | body: Option<ast::Expr>, | 64 | body: Option<ast::Expr>, |
62 | ) -> (Body, BodySourceMap) { | 65 | ) -> (Body, BodySourceMap) { |
66 | let item_tree = db.item_tree(expander.current_file_id); | ||
63 | ExprCollector { | 67 | ExprCollector { |
64 | db, | 68 | db, |
65 | def, | 69 | def, |
66 | expander, | ||
67 | source_map: BodySourceMap::default(), | 70 | source_map: BodySourceMap::default(), |
68 | body: Body { | 71 | body: Body { |
69 | exprs: Arena::default(), | 72 | exprs: Arena::default(), |
@@ -72,6 +75,12 @@ pub(super) fn lower( | |||
72 | body_expr: dummy_expr_id(), | 75 | body_expr: dummy_expr_id(), |
73 | item_scope: Default::default(), | 76 | item_scope: Default::default(), |
74 | }, | 77 | }, |
78 | item_trees: { | ||
79 | let mut map = FxHashMap::default(); | ||
80 | map.insert(expander.current_file_id, item_tree); | ||
81 | map | ||
82 | }, | ||
83 | expander, | ||
75 | } | 84 | } |
76 | .collect(params, body) | 85 | .collect(params, body) |
77 | } | 86 | } |
@@ -82,6 +91,8 @@ struct ExprCollector<'a> { | |||
82 | expander: Expander, | 91 | expander: Expander, |
83 | body: Body, | 92 | body: Body, |
84 | source_map: BodySourceMap, | 93 | source_map: BodySourceMap, |
94 | |||
95 | item_trees: FxHashMap<HirFileId, Arc<ItemTree>>, | ||
85 | } | 96 | } |
86 | 97 | ||
87 | impl ExprCollector<'_> { | 98 | impl ExprCollector<'_> { |
@@ -533,6 +544,9 @@ impl ExprCollector<'_> { | |||
533 | self.source_map | 544 | self.source_map |
534 | .expansions | 545 | .expansions |
535 | .insert(macro_call, self.expander.current_file_id); | 546 | .insert(macro_call, self.expander.current_file_id); |
547 | |||
548 | let item_tree = self.db.item_tree(self.expander.current_file_id); | ||
549 | self.item_trees.insert(self.expander.current_file_id, item_tree); | ||
536 | let id = self.collect_expr(expansion); | 550 | let id = self.collect_expr(expansion); |
537 | self.expander.exit(self.db, mark); | 551 | self.expander.exit(self.db, mark); |
538 | id | 552 | id |
@@ -547,6 +561,19 @@ impl ExprCollector<'_> { | |||
547 | } | 561 | } |
548 | } | 562 | } |
549 | 563 | ||
564 | fn find_inner_item<S: ItemTreeNode>(&self, id: AstId<ast::ModuleItem>) -> FileItemTreeId<S> { | ||
565 | let tree = &self.item_trees[&id.file_id]; | ||
566 | |||
567 | // FIXME: This probably breaks with `use` items, since they produce multiple item tree nodes | ||
568 | |||
569 | // Root file (non-macro). | ||
570 | tree.all_inner_items() | ||
571 | .chain(tree.top_level_items().iter().copied()) | ||
572 | .filter_map(|mod_item| mod_item.downcast::<S>()) | ||
573 | .find(|tree_id| tree[*tree_id].ast_id().upcast() == id.value) | ||
574 | .unwrap_or_else(|| panic!("couldn't find inner item for {:?}", id)) | ||
575 | } | ||
576 | |||
550 | fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId { | 577 | fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId { |
551 | if let Some(expr) = expr { | 578 | if let Some(expr) = expr { |
552 | self.collect_expr(expr) | 579 | self.collect_expr(expr) |
@@ -578,56 +605,102 @@ impl ExprCollector<'_> { | |||
578 | 605 | ||
579 | fn collect_block_items(&mut self, block: &ast::BlockExpr) { | 606 | fn collect_block_items(&mut self, block: &ast::BlockExpr) { |
580 | let container = ContainerId::DefWithBodyId(self.def); | 607 | let container = ContainerId::DefWithBodyId(self.def); |
581 | for item in block.items() { | 608 | |
582 | let (def, name): (ModuleDefId, Option<ast::Name>) = match item { | 609 | let items = block |
583 | ast::ModuleItem::FnDef(def) => { | 610 | .items() |
584 | let ast_id = self.expander.ast_id(&def); | 611 | .filter_map(|item| { |
585 | ( | 612 | let (def, name): (ModuleDefId, Option<ast::Name>) = match item { |
586 | FunctionLoc { container: container.into(), ast_id }.intern(self.db).into(), | 613 | ast::ModuleItem::FnDef(def) => { |
587 | def.name(), | 614 | let ast_id = self.expander.ast_id(&def); |
588 | ) | 615 | let id = self.find_inner_item(ast_id.map(|id| id.upcast())); |
589 | } | 616 | ( |
590 | ast::ModuleItem::TypeAliasDef(def) => { | 617 | FunctionLoc { container: container.into(), id: ast_id.with_value(id) } |
591 | let ast_id = self.expander.ast_id(&def); | 618 | .intern(self.db) |
592 | ( | 619 | .into(), |
593 | TypeAliasLoc { container: container.into(), ast_id }.intern(self.db).into(), | 620 | def.name(), |
594 | def.name(), | 621 | ) |
595 | ) | 622 | } |
596 | } | 623 | ast::ModuleItem::TypeAliasDef(def) => { |
597 | ast::ModuleItem::ConstDef(def) => { | 624 | let ast_id = self.expander.ast_id(&def); |
598 | let ast_id = self.expander.ast_id(&def); | 625 | let id = self.find_inner_item(ast_id.map(|id| id.upcast())); |
599 | ( | 626 | ( |
600 | ConstLoc { container: container.into(), ast_id }.intern(self.db).into(), | 627 | TypeAliasLoc { container: container.into(), id: ast_id.with_value(id) } |
601 | def.name(), | 628 | .intern(self.db) |
602 | ) | 629 | .into(), |
603 | } | 630 | def.name(), |
604 | ast::ModuleItem::StaticDef(def) => { | 631 | ) |
605 | let ast_id = self.expander.ast_id(&def); | 632 | } |
606 | (StaticLoc { container, ast_id }.intern(self.db).into(), def.name()) | 633 | ast::ModuleItem::ConstDef(def) => { |
607 | } | 634 | let ast_id = self.expander.ast_id(&def); |
608 | ast::ModuleItem::StructDef(def) => { | 635 | let id = self.find_inner_item(ast_id.map(|id| id.upcast())); |
609 | let ast_id = self.expander.ast_id(&def); | 636 | ( |
610 | (StructLoc { container, ast_id }.intern(self.db).into(), def.name()) | 637 | ConstLoc { container: container.into(), id: ast_id.with_value(id) } |
611 | } | 638 | .intern(self.db) |
612 | ast::ModuleItem::EnumDef(def) => { | 639 | .into(), |
613 | let ast_id = self.expander.ast_id(&def); | 640 | def.name(), |
614 | (EnumLoc { container, ast_id }.intern(self.db).into(), def.name()) | 641 | ) |
615 | } | 642 | } |
616 | ast::ModuleItem::UnionDef(def) => { | 643 | ast::ModuleItem::StaticDef(def) => { |
617 | let ast_id = self.expander.ast_id(&def); | 644 | let ast_id = self.expander.ast_id(&def); |
618 | (UnionLoc { container, ast_id }.intern(self.db).into(), def.name()) | 645 | let id = self.find_inner_item(ast_id.map(|id| id.upcast())); |
619 | } | 646 | ( |
620 | ast::ModuleItem::TraitDef(def) => { | 647 | StaticLoc { container, id: ast_id.with_value(id) } |
621 | let ast_id = self.expander.ast_id(&def); | 648 | .intern(self.db) |
622 | (TraitLoc { container, ast_id }.intern(self.db).into(), def.name()) | 649 | .into(), |
623 | } | 650 | def.name(), |
624 | ast::ModuleItem::ExternBlock(_) => continue, // FIXME: collect from extern blocks | 651 | ) |
625 | ast::ModuleItem::ImplDef(_) | 652 | } |
626 | | ast::ModuleItem::UseItem(_) | 653 | ast::ModuleItem::StructDef(def) => { |
627 | | ast::ModuleItem::ExternCrateItem(_) | 654 | let ast_id = self.expander.ast_id(&def); |
628 | | ast::ModuleItem::Module(_) | 655 | let id = self.find_inner_item(ast_id.map(|id| id.upcast())); |
629 | | ast::ModuleItem::MacroCall(_) => continue, | 656 | ( |
630 | }; | 657 | StructLoc { container, id: ast_id.with_value(id) } |
658 | .intern(self.db) | ||
659 | .into(), | ||
660 | def.name(), | ||
661 | ) | ||
662 | } | ||
663 | ast::ModuleItem::EnumDef(def) => { | ||
664 | let ast_id = self.expander.ast_id(&def); | ||
665 | let id = self.find_inner_item(ast_id.map(|id| id.upcast())); | ||
666 | ( | ||
667 | EnumLoc { container, id: ast_id.with_value(id) }.intern(self.db).into(), | ||
668 | def.name(), | ||
669 | ) | ||
670 | } | ||
671 | ast::ModuleItem::UnionDef(def) => { | ||
672 | let ast_id = self.expander.ast_id(&def); | ||
673 | let id = self.find_inner_item(ast_id.map(|id| id.upcast())); | ||
674 | ( | ||
675 | UnionLoc { container, id: ast_id.with_value(id) } | ||
676 | .intern(self.db) | ||
677 | .into(), | ||
678 | def.name(), | ||
679 | ) | ||
680 | } | ||
681 | ast::ModuleItem::TraitDef(def) => { | ||
682 | let ast_id = self.expander.ast_id(&def); | ||
683 | let id = self.find_inner_item(ast_id.map(|id| id.upcast())); | ||
684 | ( | ||
685 | TraitLoc { container, id: ast_id.with_value(id) } | ||
686 | .intern(self.db) | ||
687 | .into(), | ||
688 | def.name(), | ||
689 | ) | ||
690 | } | ||
691 | ast::ModuleItem::ExternBlock(_) => return None, // FIXME: collect from extern blocks | ||
692 | ast::ModuleItem::ImplDef(_) | ||
693 | | ast::ModuleItem::UseItem(_) | ||
694 | | ast::ModuleItem::ExternCrateItem(_) | ||
695 | | ast::ModuleItem::Module(_) | ||
696 | | ast::ModuleItem::MacroCall(_) => return None, | ||
697 | }; | ||
698 | |||
699 | Some((def, name)) | ||
700 | }) | ||
701 | .collect::<Vec<_>>(); | ||
702 | |||
703 | for (def, name) in items { | ||
631 | self.body.item_scope.define_def(def); | 704 | self.body.item_scope.define_def(def); |
632 | if let Some(name) = name { | 705 | if let Some(name) = name { |
633 | let vis = crate::visibility::Visibility::Public; // FIXME determine correctly | 706 | let vis = crate::visibility::Visibility::Public; // FIXME determine correctly |
diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs index e48ff38f9..81397b063 100644 --- a/crates/ra_hir_def/src/body/scope.rs +++ b/crates/ra_hir_def/src/body/scope.rs | |||
@@ -87,15 +87,13 @@ impl ExprScopes { | |||
87 | } | 87 | } |
88 | 88 | ||
89 | fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) { | 89 | fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) { |
90 | match &body[pat] { | 90 | let pattern = &body[pat]; |
91 | Pat::Bind { name, .. } => { | 91 | if let Pat::Bind { name, .. } = pattern { |
92 | // bind can have a sub pattern, but it's actually not allowed | 92 | let entry = ScopeEntry { name: name.clone(), pat }; |
93 | // to bind to things in there | 93 | self.scopes[scope].entries.push(entry); |
94 | let entry = ScopeEntry { name: name.clone(), pat }; | ||
95 | self.scopes[scope].entries.push(entry) | ||
96 | } | ||
97 | p => p.walk_child_pats(|pat| self.add_bindings(body, scope, pat)), | ||
98 | } | 94 | } |
95 | |||
96 | pattern.walk_child_pats(|pat| self.add_bindings(body, scope, pat)); | ||
99 | } | 97 | } |
100 | 98 | ||
101 | fn add_params_bindings(&mut self, body: &Body, scope: ScopeId, params: &[PatId]) { | 99 | fn add_params_bindings(&mut self, body: &Body, scope: ScopeId, params: &[PatId]) { |
@@ -190,21 +188,23 @@ mod tests { | |||
190 | } | 188 | } |
191 | } | 189 | } |
192 | 190 | ||
193 | fn do_check(code: &str, expected: &[&str]) { | 191 | fn do_check(ra_fixture: &str, expected: &[&str]) { |
194 | let (off, code) = extract_offset(code); | 192 | let (offset, code) = extract_offset(ra_fixture); |
195 | let code = { | 193 | let code = { |
196 | let mut buf = String::new(); | 194 | let mut buf = String::new(); |
197 | let off: usize = off.into(); | 195 | let off: usize = offset.into(); |
198 | buf.push_str(&code[..off]); | 196 | buf.push_str(&code[..off]); |
199 | buf.push_str("marker"); | 197 | buf.push_str("<|>marker"); |
200 | buf.push_str(&code[off..]); | 198 | buf.push_str(&code[off..]); |
201 | buf | 199 | buf |
202 | }; | 200 | }; |
203 | 201 | ||
204 | let (db, file_id) = TestDB::with_single_file(&code); | 202 | let (db, position) = TestDB::with_position(&code); |
203 | let file_id = position.file_id; | ||
204 | let offset = position.offset; | ||
205 | 205 | ||
206 | let file_syntax = db.parse(file_id).syntax_node(); | 206 | let file_syntax = db.parse(file_id).syntax_node(); |
207 | let marker: ast::PathExpr = find_node_at_offset(&file_syntax, off).unwrap(); | 207 | let marker: ast::PathExpr = find_node_at_offset(&file_syntax, offset).unwrap(); |
208 | let function = find_function(&db, file_id); | 208 | let function = find_function(&db, file_id); |
209 | 209 | ||
210 | let scopes = db.expr_scopes(function.into()); | 210 | let scopes = db.expr_scopes(function.into()); |
@@ -300,15 +300,52 @@ mod tests { | |||
300 | ); | 300 | ); |
301 | } | 301 | } |
302 | 302 | ||
303 | fn do_check_local_name(code: &str, expected_offset: u32) { | 303 | #[test] |
304 | let (off, code) = extract_offset(code); | 304 | fn test_bindings_after_at() { |
305 | do_check( | ||
306 | r" | ||
307 | fn foo() { | ||
308 | match Some(()) { | ||
309 | opt @ Some(unit) => { | ||
310 | <|> | ||
311 | } | ||
312 | _ => {} | ||
313 | } | ||
314 | } | ||
315 | ", | ||
316 | &["opt", "unit"], | ||
317 | ); | ||
318 | } | ||
319 | |||
320 | #[test] | ||
321 | fn macro_inner_item() { | ||
322 | do_check( | ||
323 | r" | ||
324 | macro_rules! mac { | ||
325 | () => {{ | ||
326 | fn inner() {} | ||
327 | inner(); | ||
328 | }}; | ||
329 | } | ||
330 | |||
331 | fn foo() { | ||
332 | mac!(); | ||
333 | <|> | ||
334 | } | ||
335 | ", | ||
336 | &[], | ||
337 | ); | ||
338 | } | ||
305 | 339 | ||
306 | let (db, file_id) = TestDB::with_single_file(&code); | 340 | fn do_check_local_name(ra_fixture: &str, expected_offset: u32) { |
341 | let (db, position) = TestDB::with_position(ra_fixture); | ||
342 | let file_id = position.file_id; | ||
343 | let offset = position.offset; | ||
307 | 344 | ||
308 | let file = db.parse(file_id).ok().unwrap(); | 345 | let file = db.parse(file_id).ok().unwrap(); |
309 | let expected_name = find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()) | 346 | let expected_name = find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into()) |
310 | .expect("failed to find a name at the target offset"); | 347 | .expect("failed to find a name at the target offset"); |
311 | let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); | 348 | let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), offset).unwrap(); |
312 | 349 | ||
313 | let function = find_function(&db, file_id); | 350 | let function = find_function(&db, file_id); |
314 | 351 | ||
@@ -336,15 +373,16 @@ mod tests { | |||
336 | fn test_resolve_local_name() { | 373 | fn test_resolve_local_name() { |
337 | do_check_local_name( | 374 | do_check_local_name( |
338 | r#" | 375 | r#" |
339 | fn foo(x: i32, y: u32) { | 376 | fn foo(x: i32, y: u32) { |
340 | { | 377 | { |
341 | let z = x * 2; | 378 | let z = x * 2; |
342 | } | 379 | } |
343 | { | 380 | { |
344 | let t = x<|> * 3; | 381 | let t = x<|> * 3; |
345 | } | 382 | } |
346 | }"#, | 383 | } |
347 | 21, | 384 | "#, |
385 | 7, | ||
348 | ); | 386 | ); |
349 | } | 387 | } |
350 | 388 | ||
@@ -352,10 +390,11 @@ mod tests { | |||
352 | fn test_resolve_local_name_declaration() { | 390 | fn test_resolve_local_name_declaration() { |
353 | do_check_local_name( | 391 | do_check_local_name( |
354 | r#" | 392 | r#" |
355 | fn foo(x: String) { | 393 | fn foo(x: String) { |
356 | let x : &str = &x<|>; | 394 | let x : &str = &x<|>; |
357 | }"#, | 395 | } |
358 | 21, | 396 | "#, |
397 | 7, | ||
359 | ); | 398 | ); |
360 | } | 399 | } |
361 | 400 | ||
@@ -363,12 +402,12 @@ mod tests { | |||
363 | fn test_resolve_local_name_shadow() { | 402 | fn test_resolve_local_name_shadow() { |
364 | do_check_local_name( | 403 | do_check_local_name( |
365 | r" | 404 | r" |
366 | fn foo(x: String) { | 405 | fn foo(x: String) { |
367 | let x : &str = &x; | 406 | let x : &str = &x; |
368 | x<|> | 407 | x<|> |
369 | } | 408 | } |
370 | ", | 409 | ", |
371 | 53, | 410 | 28, |
372 | ); | 411 | ); |
373 | } | 412 | } |
374 | 413 | ||
@@ -376,13 +415,13 @@ mod tests { | |||
376 | fn ref_patterns_contribute_bindings() { | 415 | fn ref_patterns_contribute_bindings() { |
377 | do_check_local_name( | 416 | do_check_local_name( |
378 | r" | 417 | r" |
379 | fn foo() { | 418 | fn foo() { |
380 | if let Some(&from) = bar() { | 419 | if let Some(&from) = bar() { |
381 | from<|>; | 420 | from<|>; |
382 | } | 421 | } |
383 | } | 422 | } |
384 | ", | 423 | ", |
385 | 53, | 424 | 28, |
386 | ); | 425 | ); |
387 | } | 426 | } |
388 | 427 | ||
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 53599e74a..f9e5701db 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs | |||
@@ -2,27 +2,19 @@ | |||
2 | 2 | ||
3 | use std::sync::Arc; | 3 | use std::sync::Arc; |
4 | 4 | ||
5 | use hir_expand::{ | 5 | use hir_expand::{name::Name, InFile}; |
6 | hygiene::Hygiene, | ||
7 | name::{name, AsName, Name}, | ||
8 | AstId, InFile, | ||
9 | }; | ||
10 | use ra_prof::profile; | 6 | use ra_prof::profile; |
11 | use ra_syntax::ast::{ | 7 | use ra_syntax::ast; |
12 | self, AssocItem, AstNode, ModuleItemOwner, NameOwner, TypeAscriptionOwner, TypeBoundsOwner, | ||
13 | VisibilityOwner, | ||
14 | }; | ||
15 | 8 | ||
16 | use crate::{ | 9 | use crate::{ |
17 | attr::Attrs, | 10 | attr::Attrs, |
18 | body::LowerCtx, | 11 | body::Expander, |
19 | db::DefDatabase, | 12 | db::DefDatabase, |
20 | path::{path, AssociatedTypeBinding, GenericArgs, Path}, | 13 | item_tree::{AssocItem, ItemTreeId, ModItem}, |
21 | src::HasSource, | 14 | type_ref::{TypeBound, TypeRef}, |
22 | type_ref::{Mutability, TypeBound, TypeRef}, | ||
23 | visibility::RawVisibility, | 15 | visibility::RawVisibility, |
24 | AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, | 16 | AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId, |
25 | ImplId, Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, | 17 | Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, |
26 | }; | 18 | }; |
27 | 19 | ||
28 | #[derive(Debug, Clone, PartialEq, Eq)] | 20 | #[derive(Debug, Clone, PartialEq, Eq)] |
@@ -41,82 +33,27 @@ pub struct FunctionData { | |||
41 | impl FunctionData { | 33 | impl FunctionData { |
42 | pub(crate) fn fn_data_query(db: &impl DefDatabase, func: FunctionId) -> Arc<FunctionData> { | 34 | pub(crate) fn fn_data_query(db: &impl DefDatabase, func: FunctionId) -> Arc<FunctionData> { |
43 | let loc = func.lookup(db); | 35 | let loc = func.lookup(db); |
44 | let src = loc.source(db); | 36 | let item_tree = db.item_tree(loc.id.file_id); |
45 | let ctx = LowerCtx::new(db, src.file_id); | 37 | let func = &item_tree[loc.id.value]; |
46 | let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); | 38 | |
47 | let mut params = Vec::new(); | 39 | Arc::new(FunctionData { |
48 | let mut has_self_param = false; | 40 | name: func.name.clone(), |
49 | if let Some(param_list) = src.value.param_list() { | 41 | params: func.params.to_vec(), |
50 | if let Some(self_param) = param_list.self_param() { | 42 | ret_type: func.ret_type.clone(), |
51 | let self_type = if let Some(type_ref) = self_param.ascribed_type() { | 43 | attrs: item_tree.attrs(loc.id.value.into()).clone(), |
52 | TypeRef::from_ast(&ctx, type_ref) | 44 | has_self_param: func.has_self_param, |
53 | } else { | 45 | is_unsafe: func.is_unsafe, |
54 | let self_type = TypeRef::Path(name![Self].into()); | 46 | visibility: item_tree[func.visibility].clone(), |
55 | match self_param.kind() { | 47 | }) |
56 | ast::SelfParamKind::Owned => self_type, | ||
57 | ast::SelfParamKind::Ref => { | ||
58 | TypeRef::Reference(Box::new(self_type), Mutability::Shared) | ||
59 | } | ||
60 | ast::SelfParamKind::MutRef => { | ||
61 | TypeRef::Reference(Box::new(self_type), Mutability::Mut) | ||
62 | } | ||
63 | } | ||
64 | }; | ||
65 | params.push(self_type); | ||
66 | has_self_param = true; | ||
67 | } | ||
68 | for param in param_list.params() { | ||
69 | let type_ref = TypeRef::from_ast_opt(&ctx, param.ascribed_type()); | ||
70 | params.push(type_ref); | ||
71 | } | ||
72 | } | ||
73 | let attrs = Attrs::new(&src.value, &Hygiene::new(db.upcast(), src.file_id)); | ||
74 | |||
75 | let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) { | ||
76 | TypeRef::from_ast(&ctx, type_ref) | ||
77 | } else { | ||
78 | TypeRef::unit() | ||
79 | }; | ||
80 | |||
81 | let ret_type = if src.value.async_token().is_some() { | ||
82 | let future_impl = desugar_future_path(ret_type); | ||
83 | let ty_bound = TypeBound::Path(future_impl); | ||
84 | TypeRef::ImplTrait(vec![ty_bound]) | ||
85 | } else { | ||
86 | ret_type | ||
87 | }; | ||
88 | |||
89 | let is_unsafe = src.value.unsafe_token().is_some(); | ||
90 | |||
91 | let vis_default = RawVisibility::default_for_container(loc.container); | ||
92 | let visibility = | ||
93 | RawVisibility::from_ast_with_default(db, vis_default, src.map(|s| s.visibility())); | ||
94 | |||
95 | let sig = | ||
96 | FunctionData { name, params, ret_type, has_self_param, is_unsafe, visibility, attrs }; | ||
97 | Arc::new(sig) | ||
98 | } | 48 | } |
99 | } | 49 | } |
100 | 50 | ||
101 | fn desugar_future_path(orig: TypeRef) -> Path { | ||
102 | let path = path![core::future::Future]; | ||
103 | let mut generic_args: Vec<_> = std::iter::repeat(None).take(path.segments.len() - 1).collect(); | ||
104 | let mut last = GenericArgs::empty(); | ||
105 | last.bindings.push(AssociatedTypeBinding { | ||
106 | name: name![Output], | ||
107 | type_ref: Some(orig), | ||
108 | bounds: Vec::new(), | ||
109 | }); | ||
110 | generic_args.push(Some(Arc::new(last))); | ||
111 | |||
112 | Path::from_known_path(path, generic_args) | ||
113 | } | ||
114 | |||
115 | #[derive(Debug, Clone, PartialEq, Eq)] | 51 | #[derive(Debug, Clone, PartialEq, Eq)] |