Skip to content

Commit da49c5f

Browse files
committed
Add ExternalCrate import grouping style
This creates two groups: 1. `std`, `core`, `alloc` and external crates, 2. `self`, `super` and `crate` imports.
1 parent a57d57b commit da49c5f

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

Configurations.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2229,7 +2229,7 @@ Controls the strategy for how consecutive imports are grouped together.
22292229
Controls the strategy for grouping sets of consecutive imports. Imports may contain newlines between imports and still be grouped together as a single set, but other statements between imports will result in different grouping sets.
22302230

22312231
- **Default value**: `Preserve`
2232-
- **Possible values**: `Preserve`, `StdExternalCrate`, `One`
2232+
- **Possible values**: `Preserve`, `StdExternalCrate`, `ExternalCrate`, `One`
22332233
- **Stable**: No (tracking issue: [#5083](https://github.com/rust-lang/rustfmt/issues/5083))
22342234

22352235
Each set of imports (one or more `use` statements, optionally separated by newlines) will be formatted independently. Other statements such as `mod ...` or `extern crate ...` will cause imports to not be grouped together.
@@ -2277,6 +2277,26 @@ use super::update::convert_publish_payload;
22772277
use crate::models::Event;
22782278
```
22792279

2280+
#### `ExternalCrate`:
2281+
2282+
Discard existing import groups, and create two groups for:
2283+
1. `std`, `core`, `alloc` and external crates,
2284+
2. `self`, `super` and `crate` imports.
2285+
2286+
```rust
2287+
use alloc::alloc::Layout;
2288+
use broker::database::PooledConnection;
2289+
use chrono::Utc;
2290+
use core::f32;
2291+
use juniper::{FieldError, FieldResult};
2292+
use std::sync::Arc;
2293+
use uuid::Uuid;
2294+
2295+
use super::schema::{Context, Payload};
2296+
use super::update::convert_publish_payload;
2297+
use crate::models::Event;
2298+
```
2299+
22802300
#### `One`:
22812301

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

src/config/options.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ pub enum GroupImportsTactic {
112112
/// 2. other imports
113113
/// 3. `self` / `crate` / `super` imports
114114
StdExternalCrate,
115+
/// Discard existing groups, and create new groups for
116+
/// 1. `std` / `core` / `alloc` / other imports
117+
/// 2. `self` / `crate` / `super` imports
118+
ExternalCrate,
115119
/// Discard existing groups, and create a single group for everything
116120
One,
117121
}

src/reorder.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ fn rewrite_reorderable_or_regroupable_items(
116116
GroupImportsTactic::Preserve | GroupImportsTactic::One => {
117117
vec![normalized_items]
118118
}
119-
GroupImportsTactic::StdExternalCrate => group_imports(normalized_items),
119+
GroupImportsTactic::StdExternalCrate => {
120+
group_imports_std_external_crate(normalized_items)
121+
}
122+
GroupImportsTactic::ExternalCrate => group_imports_external_crate(normalized_items),
120123
};
121124

122125
if context.config.reorder_imports() {
@@ -172,7 +175,7 @@ fn contains_macro_use_attr(item: &ast::Item) -> bool {
172175

173176
/// Divides imports into three groups, corresponding to standard, external
174177
/// and local imports. Sorts each subgroup.
175-
fn group_imports(uts: Vec<UseTree>) -> Vec<Vec<UseTree>> {
178+
fn group_imports_std_external_crate(uts: Vec<UseTree>) -> Vec<Vec<UseTree>> {
176179
let mut std_imports = Vec::new();
177180
let mut external_imports = Vec::new();
178181
let mut local_imports = Vec::new();
@@ -198,6 +201,30 @@ fn group_imports(uts: Vec<UseTree>) -> Vec<Vec<UseTree>> {
198201
vec![std_imports, external_imports, local_imports]
199202
}
200203

204+
/// Divides imports into two groups, corresponding to external crates
205+
/// and local imports. Sorts each subgroup.
206+
fn group_imports_external_crate(uts: Vec<UseTree>) -> Vec<Vec<UseTree>> {
207+
let mut external_imports = Vec::new();
208+
let mut local_imports = Vec::new();
209+
210+
for ut in uts.into_iter() {
211+
if ut.path.is_empty() {
212+
external_imports.push(ut);
213+
continue;
214+
}
215+
match &ut.path[0].kind {
216+
UseSegmentKind::Ident(..) => external_imports.push(ut),
217+
UseSegmentKind::Slf(_) | UseSegmentKind::Super(_) | UseSegmentKind::Crate(_) => {
218+
local_imports.push(ut)
219+
}
220+
// These are probably illegal here
221+
UseSegmentKind::Glob | UseSegmentKind::List(_) => external_imports.push(ut),
222+
}
223+
}
224+
225+
vec![external_imports, local_imports]
226+
}
227+
201228
/// A simplified version of `ast::ItemKind`.
202229
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
203230
enum ReorderableItemKind {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// rustfmt-group_imports: ExternalCrate
2+
use chrono::Utc;
3+
use super::update::convert_publish_payload;
4+
5+
use juniper::{FieldError, FieldResult};
6+
use uuid::Uuid;
7+
use alloc::alloc::Layout;
8+
9+
use std::sync::Arc;
10+
11+
use broker::database::PooledConnection;
12+
13+
use super::schema::{Context, Payload};
14+
use core::f32;
15+
use crate::models::Event;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-group_imports: ExternalCrate
2+
use alloc::alloc::Layout;
3+
use broker::database::PooledConnection;
4+
use chrono::Utc;
5+
use core::f32;
6+
use juniper::{FieldError, FieldResult};
7+
use std::sync::Arc;
8+
use uuid::Uuid;
9+
10+
use super::schema::{Context, Payload};
11+
use super::update::convert_publish_payload;
12+
use crate::models::Event;

0 commit comments

Comments
 (0)