From 64f7072c255bd97a58b8344d0beeae281b8f7e13 Mon Sep 17 00:00:00 2001 From: Lukas Tobias Wirth Date: Tue, 18 May 2021 19:49:15 +0200 Subject: MergeBehavior -> ImportGranularity --- crates/ide_db/src/helpers/insert_use.rs | 27 +++++++++++++++++++++++++-- crates/ide_db/src/helpers/insert_use/tests.rs | 16 ++++++++-------- 2 files changed, 33 insertions(+), 10 deletions(-) (limited to 'crates/ide_db/src') 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::{ pub use hir::PrefixKind; +/// How imports should be grouped into use statements. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum ImportGranularity { + /// Do not change the granularity of any imports and preserve the original structure written by the developer. + Preserve, + /// Merge imports from the same crate into a single use statement. + Crate, + /// Merge imports from the same module into a single use statement. + Module, + /// Flatten imports so that each has its own use statement. + Item, +} + +impl ImportGranularity { + pub fn merge_behavior(self) -> Option { + match self { + ImportGranularity::Crate => Some(MergeBehavior::Crate), + ImportGranularity::Module => Some(MergeBehavior::Module), + ImportGranularity::Preserve | ImportGranularity::Item => None, + } + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct InsertUseConfig { - pub merge: Option, + pub granularity: ImportGranularity, pub prefix_kind: PrefixKind, pub group: bool, } @@ -73,7 +96,7 @@ pub fn insert_use<'a>(scope: &ImportScope, path: ast::Path, cfg: InsertUseConfig let use_item = make::use_(None, make::use_tree(path.clone(), None, None, false)).clone_for_update(); // merge into existing imports if possible - if let Some(mb) = cfg.merge { + if let Some(mb) = cfg.granularity.merge_behavior() { for existing_use in scope.as_syntax_node().children().filter_map(ast::Use::cast) { if let Some(merged) = try_merge_imports(&existing_use, &use_item, mb) { 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; use self::bar::A; use super::bar::A; use external_crate2::bar::A;", - None, + ImportGranularity::Item, false, false, ); @@ -36,7 +36,7 @@ fn insert_not_group_empty() { r"use external_crate2::bar::A; ", - None, + ImportGranularity::Item, false, false, ); @@ -281,7 +281,7 @@ fn insert_empty_module() { r"{ use foo::bar; }", - None, + ImportGranularity::Item, true, true, ) @@ -635,7 +635,7 @@ fn check( path: &str, ra_fixture_before: &str, ra_fixture_after: &str, - mb: Option, + granularity: ImportGranularity, module: bool, group: bool, ) { @@ -651,21 +651,21 @@ fn check( .find_map(ast::Path::cast) .unwrap(); - insert_use(&file, path, InsertUseConfig { merge: mb, prefix_kind: PrefixKind::Plain, group }); + insert_use(&file, path, InsertUseConfig { granularity, prefix_kind: PrefixKind::Plain, group }); let result = file.as_syntax_node().to_string(); assert_eq_text!(ra_fixture_after, &result); } fn check_crate(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { - check(path, ra_fixture_before, ra_fixture_after, Some(MergeBehavior::Crate), false, true) + check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate, false, true) } fn check_module(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { - check(path, ra_fixture_before, ra_fixture_after, Some(MergeBehavior::Module), false, true) + check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module, false, true) } fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { - check(path, ra_fixture_before, ra_fixture_after, None, false, true) + check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item, false, true) } fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) { -- cgit v1.2.3