Skip to content

Commit

Permalink
Add imports_granularity="Flatten".
Browse files Browse the repository at this point in the history
This option splits all imports into their own `use` statement.
  • Loading branch information
msmorgan committed Jan 12, 2021
1 parent e1ab878 commit 22e437d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
17 changes: 16 additions & 1 deletion Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`, `Flatten`
- **Stable**: No

#### `Preserve` (default):
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
17 changes: 17 additions & 0 deletions src/formatting/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ impl UseTree {
SharedPrefix::Module => {
self.path[..self.path.len() - 1] == other.path[..other.path.len() - 1]
}
SharedPrefix::NoPrefix => false,
}
}
}
Expand Down Expand Up @@ -854,6 +855,7 @@ impl Rewrite for UseTree {
pub(crate) enum SharedPrefix {
Crate,
Module,
NoPrefix,
}

#[cfg(test)]
Expand Down Expand Up @@ -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!(
Expand Down
15 changes: 8 additions & 7 deletions src/formatting/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::Flatten => {
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],
Expand Down
23 changes: 23 additions & 0 deletions tests/target/imports_granularity_flatten.rs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 22e437d

Please sign in to comment.