Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add One option to group_imports #4966

Merged
merged 3 commits into from
Oct 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2011,7 +2011,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 @@ -2057,6 +2057,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 @@ -117,7 +117,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;