diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-05-20 09:27:16 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-05-20 09:27:16 +0100 |
commit | 8bb37737c9e8b7a390ae29d5fb0daaeeb495d2b5 (patch) | |
tree | 0f8d45124abe86151e1954c1766534116e174209 /crates/ide_db/src/helpers/insert_use/tests.rs | |
parent | 764241e38e46316b6370977e8b51e841e93e84b9 (diff) | |
parent | 2bf720900f94e36969af44ff8ac52470faf9af4b (diff) |
Merge #8873
8873: Implement import-granularity guessing r=matklad a=Veykril
This renames our `MergeBehavior` to `ImportGranularity` as rustfmt has it as the purpose of them are basically the same. `ImportGranularity::Preserve` currently has no specific purpose for us as we don't have an organize imports assist yet, so it currently acts the same as `ImportGranularity::Item`.
We now try to guess the import style on a per file basis and fall back to the user granularity setting if the file has no specific style yet or where it is ambiguous. This can be turned off by setting `import.enforceGranularity` to `true`.
Closes https://github.com/rust-analyzer/rust-analyzer/issues/8870
Co-authored-by: Lukas Tobias Wirth <[email protected]>
Diffstat (limited to 'crates/ide_db/src/helpers/insert_use/tests.rs')
-rw-r--r-- | crates/ide_db/src/helpers/insert_use/tests.rs | 141 |
1 files changed, 133 insertions, 8 deletions
diff --git a/crates/ide_db/src/helpers/insert_use/tests.rs b/crates/ide_db/src/helpers/insert_use/tests.rs index 248227d29..78a2a87b3 100644 --- a/crates/ide_db/src/helpers/insert_use/tests.rs +++ b/crates/ide_db/src/helpers/insert_use/tests.rs | |||
@@ -21,7 +21,7 @@ use crate::bar::A; | |||
21 | use self::bar::A; | 21 | use self::bar::A; |
22 | use super::bar::A; | 22 | use super::bar::A; |
23 | use external_crate2::bar::A;", | 23 | use external_crate2::bar::A;", |
24 | None, | 24 | ImportGranularity::Item, |
25 | false, | 25 | false, |
26 | false, | 26 | false, |
27 | ); | 27 | ); |
@@ -36,7 +36,7 @@ fn insert_not_group_empty() { | |||
36 | r"use external_crate2::bar::A; | 36 | r"use external_crate2::bar::A; |
37 | 37 | ||
38 | ", | 38 | ", |
39 | None, | 39 | ImportGranularity::Item, |
40 | false, | 40 | false, |
41 | false, | 41 | false, |
42 | ); | 42 | ); |
@@ -281,7 +281,7 @@ fn insert_empty_module() { | |||
281 | r"{ | 281 | r"{ |
282 | use foo::bar; | 282 | use foo::bar; |
283 | }", | 283 | }", |
284 | None, | 284 | ImportGranularity::Item, |
285 | true, | 285 | true, |
286 | true, | 286 | true, |
287 | ) | 287 | ) |
@@ -631,11 +631,121 @@ fn merge_last_fail3() { | |||
631 | ); | 631 | ); |
632 | } | 632 | } |
633 | 633 | ||
634 | #[test] | ||
635 | fn guess_empty() { | ||
636 | check_guess("", ImportGranularityGuess::Unknown); | ||
637 | } | ||
638 | |||
639 | #[test] | ||
640 | fn guess_single() { | ||
641 | check_guess(r"use foo::{baz::{qux, quux}, bar};", ImportGranularityGuess::Crate); | ||
642 | check_guess(r"use foo::bar;", ImportGranularityGuess::Unknown); | ||
643 | check_guess(r"use foo::bar::{baz, qux};", ImportGranularityGuess::CrateOrModule); | ||
644 | } | ||
645 | |||
646 | #[test] | ||
647 | fn guess_unknown() { | ||
648 | check_guess( | ||
649 | r" | ||
650 | use foo::bar::baz; | ||
651 | use oof::rab::xuq; | ||
652 | ", | ||
653 | ImportGranularityGuess::Unknown, | ||
654 | ); | ||
655 | } | ||
656 | |||
657 | #[test] | ||
658 | fn guess_item() { | ||
659 | check_guess( | ||
660 | r" | ||
661 | use foo::bar::baz; | ||
662 | use foo::bar::qux; | ||
663 | ", | ||
664 | ImportGranularityGuess::Item, | ||
665 | ); | ||
666 | } | ||
667 | |||
668 | #[test] | ||
669 | fn guess_module() { | ||
670 | check_guess( | ||
671 | r" | ||
672 | use foo::bar::baz; | ||
673 | use foo::bar::{qux, quux}; | ||
674 | ", | ||
675 | ImportGranularityGuess::Module, | ||
676 | ); | ||
677 | // this is a rather odd case, technically this file isn't following any style properly. | ||
678 | check_guess( | ||
679 | r" | ||
680 | use foo::bar::baz; | ||
681 | use foo::{baz::{qux, quux}, bar}; | ||
682 | ", | ||
683 | ImportGranularityGuess::Module, | ||
684 | ); | ||
685 | } | ||
686 | |||
687 | #[test] | ||
688 | fn guess_crate_or_module() { | ||
689 | check_guess( | ||
690 | r" | ||
691 | use foo::bar::baz; | ||
692 | use oof::bar::{qux, quux}; | ||
693 | ", | ||
694 | ImportGranularityGuess::CrateOrModule, | ||
695 | ); | ||
696 | } | ||
697 | |||
698 | #[test] | ||
699 | fn guess_crate() { | ||
700 | check_guess( | ||
701 | r" | ||
702 | use frob::bar::baz; | ||
703 | use foo::{baz::{qux, quux}, bar}; | ||
704 | ", | ||
705 | ImportGranularityGuess::Crate, | ||
706 | ); | ||
707 | } | ||
708 | |||
709 | #[test] | ||
710 | fn guess_skips_differing_vis() { | ||
711 | check_guess( | ||
712 | r" | ||
713 | use foo::bar::baz; | ||
714 | pub use foo::bar::qux; | ||
715 | ", | ||
716 | ImportGranularityGuess::Unknown, | ||
717 | ); | ||
718 | } | ||
719 | |||
720 | #[test] | ||
721 | fn guess_skips_differing_attrs() { | ||
722 | check_guess( | ||
723 | r" | ||
724 | pub use foo::bar::baz; | ||
725 | #[doc(hidden)] | ||
726 | pub use foo::bar::qux; | ||
727 | ", | ||
728 | ImportGranularityGuess::Unknown, | ||
729 | ); | ||
730 | } | ||
731 | |||
732 | #[test] | ||
733 | fn guess_grouping_matters() { | ||
734 | check_guess( | ||
735 | r" | ||
736 | use foo::bar::baz; | ||
737 | use oof::bar::baz; | ||
738 | use foo::bar::qux; | ||
739 | ", | ||
740 | ImportGranularityGuess::Unknown, | ||
741 | ); | ||
742 | } | ||
743 | |||
634 | fn check( | 744 | fn check( |
635 | path: &str, | 745 | path: &str, |
636 | ra_fixture_before: &str, | 746 | ra_fixture_before: &str, |
637 | ra_fixture_after: &str, | 747 | ra_fixture_after: &str, |
638 | mb: Option<MergeBehavior>, | 748 | granularity: ImportGranularity, |
639 | module: bool, | 749 | module: bool, |
640 | group: bool, | 750 | group: bool, |
641 | ) { | 751 | ) { |
@@ -651,21 +761,30 @@ fn check( | |||
651 | .find_map(ast::Path::cast) | 761 | .find_map(ast::Path::cast) |
652 | .unwrap(); | 762 | .unwrap(); |
653 | 763 | ||
654 | insert_use(&file, path, InsertUseConfig { merge: mb, prefix_kind: PrefixKind::Plain, group }); | 764 | insert_use( |
765 | &file, | ||
766 | path, | ||
767 | InsertUseConfig { | ||
768 | granularity, | ||
769 | enforce_granularity: true, | ||
770 | prefix_kind: PrefixKind::Plain, | ||
771 | group, | ||
772 | }, | ||
773 | ); | ||
655 | let result = file.as_syntax_node().to_string(); | 774 | let result = file.as_syntax_node().to_string(); |
656 | assert_eq_text!(ra_fixture_after, &result); | 775 | assert_eq_text!(ra_fixture_after, &result); |
657 | } | 776 | } |
658 | 777 | ||
659 | fn check_crate(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { | 778 | fn check_crate(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { |
660 | check(path, ra_fixture_before, ra_fixture_after, Some(MergeBehavior::Crate), false, true) | 779 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate, false, true) |
661 | } | 780 | } |
662 | 781 | ||
663 | fn check_module(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { | 782 | fn check_module(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { |
664 | check(path, ra_fixture_before, ra_fixture_after, Some(MergeBehavior::Module), false, true) | 783 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module, false, true) |
665 | } | 784 | } |
666 | 785 | ||
667 | fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { | 786 | fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { |
668 | check(path, ra_fixture_before, ra_fixture_after, None, false, true) | 787 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item, false, true) |
669 | } | 788 | } |
670 | 789 | ||
671 | fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) { | 790 | fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) { |
@@ -686,3 +805,9 @@ fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior | |||
686 | let result = try_merge_imports(&use0, &use1, mb); | 805 | let result = try_merge_imports(&use0, &use1, mb); |
687 | assert_eq!(result.map(|u| u.to_string()), None); | 806 | assert_eq!(result.map(|u| u.to_string()), None); |
688 | } | 807 | } |
808 | |||
809 | fn check_guess(ra_fixture: &str, expected: ImportGranularityGuess) { | ||
810 | let syntax = ast::SourceFile::parse(ra_fixture).tree().syntax().clone(); | ||
811 | let file = super::ImportScope::from(syntax).unwrap(); | ||
812 | assert_eq!(file.guess_granularity_from_scope(), expected); | ||
813 | } | ||