Skip to content

Commit 44f8b6e

Browse files
committed
Rename reorder_imports_opinionated -> group_imports
1 parent c5fbbfc commit 44f8b6e

File tree

6 files changed

+26
-14
lines changed

6 files changed

+26
-14
lines changed

Configurations.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -2008,22 +2008,22 @@ use dolor;
20082008
use sit;
20092009
```
20102010

2011-
## `reorder_imports_opinionated`
2011+
## `group_imports`
20122012

20132013
Discard existing import groups, and create three groups for:
20142014
1. `std`, `core` and `alloc`,
20152015
2. external crates,
2016-
3. this module.
2016+
3. `self`, `super` and `crate` imports.
20172017

20182018
Within each group, imports are sorted as with `reorder_imports`.
20192019

20202020
This has no effect is `reorder_imports` is `false`.
20212021

2022-
- **Default value**: `false`
2023-
- **Possible values**: `true`, `false`
2022+
- **Default value**: `None`
2023+
- **Possible values**: `None`, `StdExternalCrate`
20242024
- **Stable**: No
20252025

2026-
#### `false` (default):
2026+
#### `None` (default):
20272027

20282028
```rust
20292029
use super::update::convert_publish_payload;
@@ -2042,7 +2042,7 @@ use crate::models::Event;
20422042
use core::f32;
20432043
```
20442044

2045-
#### `true`:
2045+
#### `StdExternalCrate`:
20462046

20472047
```rust
20482048
use alloc::alloc::Layout;

src/config.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ create_config! {
7878
imports_indent: IndentStyle, IndentStyle::Block, false, "Indent of imports";
7979
imports_layout: ListTactic, ListTactic::Mixed, false, "Item layout inside a import block";
8080
merge_imports: bool, false, false, "Merge imports";
81+
group_imports: GroupImportsTactic, GroupImportsTactic::None, false,
82+
"Reorganize import groups";
8183

8284
// Ordering
8385
reorder_imports: bool, true, true, "Reorder import and extern crate statements alphabetically";
84-
reorder_imports_opinionated: bool, false, false,
85-
"Create blocks for standard, external, and local imports";
8686
reorder_modules: bool, true, true, "Reorder module statements alphabetically in group";
8787
reorder_impl_items: bool, false, false, "Reorder impl items";
8888

@@ -595,8 +595,8 @@ where_single_line = false
595595
imports_indent = "Block"
596596
imports_layout = "Mixed"
597597
merge_imports = false
598+
group_imports = "None"
598599
reorder_imports = true
599-
reorder_imports_opinionated = false
600600
reorder_modules = true
601601
reorder_impl_items = false
602602
type_punctuation_density = "Wide"

src/config/options.rs

+12
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ impl Density {
106106
}
107107
}
108108

109+
#[config_type]
110+
/// Configuration for import groups, i.e. sets of imports separated by newlines.
111+
pub enum GroupImportsTactic {
112+
/// Keep groups as they are.
113+
None,
114+
/// Discard existing groups, and create new groups for
115+
/// 1. `std` / `core` / `alloc` imports
116+
/// 2. other imports
117+
/// 3. `self` / `crate` / `super` imports
118+
StdExternalCrate,
119+
}
120+
109121
#[config_type]
110122
pub enum ReportTactic {
111123
Always,

src/formatting/reorder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::cmp::{Ord, Ordering};
1111
use rustc_ast::ast;
1212
use rustc_span::{symbol::sym, Span};
1313

14-
use crate::config::Config;
14+
use crate::config::{Config, GroupImportsTactic};
1515
use crate::formatting::imports::UseSegment;
1616
use crate::formatting::modules::{get_mod_inner_attrs, FileModMap};
1717
use crate::formatting::{
@@ -229,7 +229,7 @@ fn rewrite_reorderable_items(
229229
normalized_items = merge_use_trees(normalized_items);
230230
}
231231

232-
let reordered_imports = if context.config.reorder_imports_opinionated() {
232+
let reordered_imports = if context.config.group_imports() != GroupImportsTactic::None {
233233
group_and_sort_imports(normalized_items)
234234
} else {
235235
normalized_items.sort();
@@ -360,7 +360,7 @@ impl ReorderableItemKind {
360360
fn in_group(self, config: &Config) -> bool {
361361
match self {
362362
ReorderableItemKind::ExternCrate | ReorderableItemKind::Mod => true,
363-
ReorderableItemKind::Use => !config.reorder_imports_opinionated(),
363+
ReorderableItemKind::Use => config.group_imports() == GroupImportsTactic::None,
364364
ReorderableItemKind::Other => false,
365365
}
366366
}

tests/source/imports-reorder-opinionated.rs renamed to tests/source/group_imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-reorder_imports_opinionated: true
1+
// rustfmt-group_imports: StdExternalCrate
22
use chrono::Utc;
33
use super::update::convert_publish_payload;
44

tests/target/imports-reorder-opinionated.rs renamed to tests/target/group_imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-reorder_imports_opinionated: true
1+
// rustfmt-group_imports: StdExternalCrate
22
use alloc::alloc::Layout;
33
use core::f32;
44
use std::sync::Arc;

0 commit comments

Comments
 (0)