diff --git a/Configurations.md b/Configurations.md index aaefc46180b..ddd11a5f9d3 100644 --- a/Configurations.md +++ b/Configurations.md @@ -1710,7 +1710,7 @@ pub enum Foo {} Merge together related imports based on their paths. - **Default value**: `Preserve` -- **Possible values**: `Preserve`, `Crate`, `Module` +- **Possible values**: `Preserve`, `Crate`, `Module`, `Separate` - **Stable**: No #### `Preserve` (default): @@ -1749,6 +1749,21 @@ use foo::{a, b, c}; use qux::{h, i}; ``` +#### `Flatten`: + +Flatten imports so that each has its own `use` statement. + +```rust +use foo::a; +use foo::b; +use foo::b::f; +use foo::b::g; +use foo::c; +use foo::d::e; +use qux::h; +use qux::i; +``` + ## `merge_imports` This option is deprecated. Use `imports_granularity = "Crate"` instead. diff --git a/src/config/options.rs b/src/config/options.rs index a272eadd72f..3d03ca0acdd 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -128,6 +128,8 @@ pub enum ImportGranularity { Crate, /// Use one `use` statement per module. Module, + /// Use one `use` statement per imported item. + Flatten, } #[config_type] diff --git a/src/formatting/imports.rs b/src/formatting/imports.rs index bfb862ae6e6..17429028168 100644 --- a/src/formatting/imports.rs +++ b/src/formatting/imports.rs @@ -542,6 +542,7 @@ impl UseTree { SharedPrefix::Module => { self.path[..self.path.len() - 1] == other.path[..other.path.len() - 1] } + SharedPrefix::NoPrefix => false, } } } @@ -854,6 +855,7 @@ impl Rewrite for UseTree { pub(crate) enum SharedPrefix { Crate, Module, + NoPrefix, } #[cfg(test)] @@ -1068,6 +1070,21 @@ mod test { ); } + #[test] + fn test_use_tree_merge_no_prefix() { + test_merge!( + NoPrefix, + ["foo::{a::{b, c}, d::e}"], + ["foo::a::b", "foo::a::c", "foo::d::e"] + ); + + test_merge!( + NoPrefix, + ["foo::{self, a, b::{c, d}, e::*}"], + ["foo::self", "foo::a", "foo::b::c", "foo::b::d", "foo::e::*"] + ) + } + #[test] fn test_use_tree_flatten() { assert_eq!( diff --git a/src/formatting/reorder.rs b/src/formatting/reorder.rs index 9d1777d82df..9eeecdb0cac 100644 --- a/src/formatting/reorder.rs +++ b/src/formatting/reorder.rs @@ -228,15 +228,16 @@ fn rewrite_reorderable_or_regroupable_items( for (item, list_item) in normalized_items.iter_mut().zip(list_items) { item.list_item = Some(list_item.clone()); } - match context.config.imports_granularity() { - ImportGranularity::Crate => { - normalized_items = merge_use_trees(normalized_items, SharedPrefix::Crate) - } + normalized_items = match context.config.imports_granularity() { + ImportGranularity::Crate => merge_use_trees(normalized_items, SharedPrefix::Crate), ImportGranularity::Module => { - normalized_items = merge_use_trees(normalized_items, SharedPrefix::Module) + merge_use_trees(normalized_items, SharedPrefix::Module) } - ImportGranularity::Preserve => {} - } + ImportGranularity::Separate => { + merge_use_trees(normalized_items, SharedPrefix::NoPrefix) + } + ImportGranularity::Preserve => normalized_items, + }; let mut regrouped_items = match context.config.group_imports() { GroupImportsTactic::Preserve => vec![normalized_items], diff --git a/tests/target/imports_granularity_separate.rs b/tests/target/imports_granularity_separate.rs new file mode 100644 index 00000000000..d958d490afb --- /dev/null +++ b/tests/target/imports_granularity_separate.rs @@ -0,0 +1,23 @@ +// rustfmt-imports_granularity: Separate + +use a::b::c; +use a::d::e; +use a::f; +use a::g::h; +use a::g::i; +use a::j; +use a::j::k; +use a::j::k::l; +use a::j::m; +use a::n::o::p; +use a::n::q; +pub use a::r::s; +pub use a::t; + +use foo::e; +#[cfg(test)] +use foo::{a::b, c::d}; + +use bar::a::b; +use bar::c::d; +use bar::e::f;