Skip to content

Commit

Permalink
Add One option to group_imports (rust-lang#4966)
Browse files Browse the repository at this point in the history
* Add Together option to group_imports

* Rename option to One

* Rename files from Together to One
  • Loading branch information
Heliozoa authored Oct 23, 2021
1 parent efa8f55 commit 599b2fd
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 2 deletions.
19 changes: 18 additions & 1 deletion Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2062,7 +2062,7 @@ use sit;
Controls the strategy for how imports are grouped together.

- **Default value**: `Preserve`
- **Possible values**: `Preserve`, `StdExternalCrate`
- **Possible values**: `Preserve`, `StdExternalCrate`, `One`
- **Stable**: No

#### `Preserve` (default):
Expand Down Expand Up @@ -2108,6 +2108,23 @@ use super::update::convert_publish_payload;
use crate::models::Event;
```

#### `One`:

Discard existing import groups, and create a single group for everything

```rust
use super::schema::{Context, Payload};
use super::update::convert_publish_payload;
use crate::models::Event;
use alloc::alloc::Layout;
use broker::database::PooledConnection;
use chrono::Utc;
use core::f32;
use juniper::{FieldError, FieldResult};
use std::sync::Arc;
use uuid::Uuid;
```

## `reorder_modules`

Reorder `mod` declarations alphabetically in group.
Expand Down
2 changes: 2 additions & 0 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ pub enum GroupImportsTactic {
/// 2. other imports
/// 3. `self` / `crate` / `super` imports
StdExternalCrate,
/// Discard existing groups, and create a single group for everything
One,
}

#[config_type]
Expand Down
4 changes: 3 additions & 1 deletion src/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ fn rewrite_reorderable_or_regroupable_items(
};

let mut regrouped_items = match context.config.group_imports() {
GroupImportsTactic::Preserve => vec![normalized_items],
GroupImportsTactic::Preserve | GroupImportsTactic::One => {
vec![normalized_items]
}
GroupImportsTactic::StdExternalCrate => group_imports(normalized_items),
};

Expand Down
17 changes: 17 additions & 0 deletions tests/source/configs/group_imports/One-merge_imports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-group_imports: One
// rustfmt-imports_granularity: Crate
use chrono::Utc;
use super::update::convert_publish_payload;

use juniper::{FieldError, FieldResult};
use uuid::Uuid;
use alloc::alloc::Layout;

use std::sync::Arc;
use alloc::vec::Vec;

use broker::database::PooledConnection;

use super::schema::{Context, Payload};
use core::f32;
use crate::models::Event;
7 changes: 7 additions & 0 deletions tests/source/configs/group_imports/One-nested.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// rustfmt-group_imports: One
mod test {
use crate::foo::bar;

use std::path;
use crate::foo::bar2;
}
16 changes: 16 additions & 0 deletions tests/source/configs/group_imports/One-no_reorder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// rustfmt-group_imports: One
// rustfmt-reorder_imports: false
use chrono::Utc;
use super::update::convert_publish_payload;

use juniper::{FieldError, FieldResult};
use uuid::Uuid;
use alloc::alloc::Layout;

use std::sync::Arc;

use broker::database::PooledConnection;

use super::schema::{Context, Payload};
use core::f32;
use crate::models::Event;
15 changes: 15 additions & 0 deletions tests/source/configs/group_imports/One.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// rustfmt-group_imports: One
use chrono::Utc;
use super::update::convert_publish_payload;

use juniper::{FieldError, FieldResult};
use uuid::Uuid;
use alloc::alloc::Layout;

use std::sync::Arc;

use broker::database::PooledConnection;

use super::schema::{Context, Payload};
use core::f32;
use crate::models::Event;
14 changes: 14 additions & 0 deletions tests/target/configs/group_imports/One-merge_imports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// rustfmt-group_imports: One
// rustfmt-imports_granularity: Crate
use super::{
schema::{Context, Payload},
update::convert_publish_payload,
};
use crate::models::Event;
use alloc::{alloc::Layout, vec::Vec};
use broker::database::PooledConnection;
use chrono::Utc;
use core::f32;
use juniper::{FieldError, FieldResult};
use std::sync::Arc;
use uuid::Uuid;
6 changes: 6 additions & 0 deletions tests/target/configs/group_imports/One-nested.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-group_imports: One
mod test {
use crate::foo::bar;
use crate::foo::bar2;
use std::path;
}
12 changes: 12 additions & 0 deletions tests/target/configs/group_imports/One-no_reorder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// rustfmt-group_imports: One
// rustfmt-reorder_imports: false
use chrono::Utc;
use super::update::convert_publish_payload;
use juniper::{FieldError, FieldResult};
use uuid::Uuid;
use alloc::alloc::Layout;
use std::sync::Arc;
use broker::database::PooledConnection;
use super::schema::{Context, Payload};
use core::f32;
use crate::models::Event;
11 changes: 11 additions & 0 deletions tests/target/configs/group_imports/One.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// rustfmt-group_imports: One
use super::schema::{Context, Payload};
use super::update::convert_publish_payload;
use crate::models::Event;
use alloc::alloc::Layout;
use broker::database::PooledConnection;
use chrono::Utc;
use core::f32;
use juniper::{FieldError, FieldResult};
use std::sync::Arc;
use uuid::Uuid;

0 comments on commit 599b2fd

Please sign in to comment.