aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db
diff options
context:
space:
mode:
authorLukas Tobias Wirth <[email protected]>2021-05-18 18:49:15 +0100
committerLukas Tobias Wirth <[email protected]>2021-05-18 18:53:20 +0100
commit64f7072c255bd97a58b8344d0beeae281b8f7e13 (patch)
treea0e0b102c4880ff5c6b9c58a266479b513186acb /crates/ide_db
parente3d0d89d7e3e253234271008df9324ec1faf1746 (diff)
MergeBehavior -> ImportGranularity
Diffstat (limited to 'crates/ide_db')
-rw-r--r--crates/ide_db/src/helpers/insert_use.rs27
-rw-r--r--crates/ide_db/src/helpers/insert_use/tests.rs16
2 files changed, 33 insertions, 10 deletions
diff --git a/crates/ide_db/src/helpers/insert_use.rs b/crates/ide_db/src/helpers/insert_use.rs
index 55cdc4da3..a4eb31815 100644
--- a/crates/ide_db/src/helpers/insert_use.rs
+++ b/crates/ide_db/src/helpers/insert_use.rs
@@ -15,9 +15,32 @@ use crate::{
15 15
16pub use hir::PrefixKind; 16pub use hir::PrefixKind;
17 17
18/// How imports should be grouped into use statements.
19#[derive(Copy, Clone, Debug, PartialEq, Eq)]
20pub enum ImportGranularity {
21 /// Do not change the granularity of any imports and preserve the original structure written by the developer.
22 Preserve,
23 /// Merge imports from the same crate into a single use statement.
24 Crate,
25 /// Merge imports from the same module into a single use statement.
26 Module,
27 /// Flatten imports so that each has its own use statement.
28 Item,
29}
30
31impl ImportGranularity {
32 pub fn merge_behavior(self) -> Option<MergeBehavior> {
33 match self {
34 ImportGranularity::Crate => Some(MergeBehavior::Crate),
35 ImportGranularity::Module => Some(MergeBehavior::Module),
36 ImportGranularity::Preserve | ImportGranularity::Item => None,
37 }
38 }
39}
40
18#[derive(Clone, Copy, Debug, PartialEq, Eq)] 41#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub struct InsertUseConfig { 42pub struct InsertUseConfig {
20 pub merge: Option<MergeBehavior>, 43 pub granularity: ImportGranularity,
21 pub prefix_kind: PrefixKind, 44 pub prefix_kind: PrefixKind,
22 pub group: bool, 45 pub group: bool,
23} 46}
@@ -73,7 +96,7 @@ pub fn insert_use<'a>(scope: &ImportScope, path: ast::Path, cfg: InsertUseConfig
73 let use_item = 96 let use_item =
74 make::use_(None, make::use_tree(path.clone(), None, None, false)).clone_for_update(); 97 make::use_(None, make::use_tree(path.clone(), None, None, false)).clone_for_update();
75 // merge into existing imports if possible 98 // merge into existing imports if possible
76 if let Some(mb) = cfg.merge { 99 if let Some(mb) = cfg.granularity.merge_behavior() {
77 for existing_use in scope.as_syntax_node().children().filter_map(ast::Use::cast) { 100 for existing_use in scope.as_syntax_node().children().filter_map(ast::Use::cast) {
78 if let Some(merged) = try_merge_imports(&existing_use, &use_item, mb) { 101 if let Some(merged) = try_merge_imports(&existing_use, &use_item, mb) {
79 ted::replace(existing_use.syntax(), merged.syntax()); 102 ted::replace(existing_use.syntax(), merged.syntax());
diff --git a/crates/ide_db/src/helpers/insert_use/tests.rs b/crates/ide_db/src/helpers/insert_use/tests.rs
index 248227d29..f99857a89 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;
21use self::bar::A; 21use self::bar::A;
22use super::bar::A; 22use super::bar::A;
23use external_crate2::bar::A;", 23use 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 )
@@ -635,7 +635,7 @@ fn check(
635 path: &str, 635 path: &str,
636 ra_fixture_before: &str, 636 ra_fixture_before: &str,
637 ra_fixture_after: &str, 637 ra_fixture_after: &str,
638 mb: Option<MergeBehavior>, 638 granularity: ImportGranularity,
639 module: bool, 639 module: bool,
640 group: bool, 640 group: bool,
641) { 641) {
@@ -651,21 +651,21 @@ fn check(
651 .find_map(ast::Path::cast) 651 .find_map(ast::Path::cast)
652 .unwrap(); 652 .unwrap();
653 653
654 insert_use(&file, path, InsertUseConfig { merge: mb, prefix_kind: PrefixKind::Plain, group }); 654 insert_use(&file, path, InsertUseConfig { granularity, prefix_kind: PrefixKind::Plain, group });
655 let result = file.as_syntax_node().to_string(); 655 let result = file.as_syntax_node().to_string();
656 assert_eq_text!(ra_fixture_after, &result); 656 assert_eq_text!(ra_fixture_after, &result);
657} 657}
658 658
659fn check_crate(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { 659fn 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) 660 check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate, false, true)
661} 661}
662 662
663fn check_module(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { 663fn 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) 664 check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module, false, true)
665} 665}
666 666
667fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { 667fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
668 check(path, ra_fixture_before, ra_fixture_after, None, false, true) 668 check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item, false, true)
669} 669}
670 670
671fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) { 671fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) {