Skip to content

Commit 1f9350c

Browse files
utkarshgupta137l4l
authored andcommitted
feat: support custom fallback group for wildcards in group_imports
1 parent 99eadaf commit 1f9350c

File tree

9 files changed

+91
-16
lines changed

9 files changed

+91
-16
lines changed

Configurations.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2307,6 +2307,7 @@ For a given config:
23072307
```toml
23082308
group_imports = [
23092309
["$std::*", "proc_macro::*"],
2310+
["*"],
23102311
["my_crate::*", "crate::*::xyz"],
23112312
["$crate::*"],
23122313
]
@@ -2318,6 +2319,8 @@ The following order would be set:
23182319
use proc_macro::Span;
23192320
use std::rc::Rc;
23202321

2322+
use rand;
2323+
23212324
use crate::abc::xyz;
23222325
use my_crate::a::B;
23232326
use my_crate::A;
@@ -2375,7 +2378,7 @@ specific version of rustfmt is used in your CI, use this option.
23752378

23762379
The width threshold for an array element to be considered "short".
23772380

2378-
The layout of an array is dependent on the length of each of its elements.
2381+
The layout of an array is dependent on the length of each of its elements.
23792382
If the length of every element in an array is below this threshold (all elements are "short") then the array can be formatted in the mixed/compressed style, but if any one element has a length that exceeds this threshold then the array elements will have to be formatted vertically.
23802383

23812384
- **Default value**: `10`

src/config/options.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,12 @@ impl Serialize for GroupImportsTactic {
145145
GroupImportsTactic::One => Simple::One.serialize(serializer),
146146
GroupImportsTactic::Wildcards(w) => WildcardHelper(
147147
w.iter()
148-
.map(|w| w.0.iter().map(|l| l.as_str().to_string()).collect())
148+
.map(|w| match w {
149+
WildcardGroup::Group(res) => {
150+
res.iter().map(|l| l.as_str().to_string()).collect()
151+
}
152+
WildcardGroup::Fallback => vec!["*".to_owned()],
153+
})
149154
.collect(),
150155
)
151156
.serialize(serializer),
@@ -222,11 +227,21 @@ impl fmt::Display for WildcardGroups {
222227
}
223228

224229
#[derive(Debug, Clone)]
225-
pub struct WildcardGroup(Vec<regex::Regex>);
230+
pub enum WildcardGroup {
231+
Group(Vec<regex::Regex>),
232+
Fallback,
233+
}
226234

227235
impl WildcardGroup {
236+
pub(crate) fn is_fallback(&self) -> bool {
237+
self == &Self::Fallback
238+
}
239+
228240
pub(crate) fn matches(&self, path: &str) -> bool {
229-
self.0.iter().any(|w| w.is_match(path))
241+
match self {
242+
Self::Group(res) => res.iter().any(|w| w.is_match(path)),
243+
Self::Fallback => false,
244+
}
230245
}
231246
}
232247

@@ -239,6 +254,10 @@ impl TryFrom<Vec<String>> for WildcardGroup {
239254
Crate,
240255
}
241256

257+
if i == vec!["*"] {
258+
return Ok(Self::Fallback);
259+
}
260+
242261
i.into_iter()
243262
.map(|s| {
244263
let (s, kind) = if let Some(tail) = s.strip_prefix("$std") {
@@ -260,22 +279,27 @@ impl TryFrom<Vec<String>> for WildcardGroup {
260279
regex::Regex::new(&format!("^{wildcard}$"))
261280
})
262281
.collect::<Result<_, _>>()
263-
.map(Self)
282+
.map(Self::Group)
264283
}
265284
}
266285

267286
impl Eq for WildcardGroup {}
268287

269288
impl std::cmp::PartialEq for WildcardGroup {
270289
fn eq(&self, other: &Self) -> bool {
271-
if self.0.len() != other.0.len() {
272-
return false;
273-
}
290+
match (self, other) {
291+
(Self::Fallback, Self::Fallback) => true,
292+
(Self::Group(left), Self::Group(right)) => {
293+
if left.len() != right.len() {
294+
return false;
295+
}
274296

275-
self.0
276-
.iter()
277-
.zip(other.0.iter())
278-
.all(|(a, b)| a.as_str() == b.as_str())
297+
left.iter()
298+
.zip(right.iter())
299+
.all(|(a, b)| a.as_str() == b.as_str())
300+
}
301+
(_, _) => false,
302+
}
279303
}
280304
}
281305

src/reorder.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::cmp::{Ord, Ordering};
1111
use rustc_ast::{ast, attr};
1212
use rustc_span::{symbol::sym, Span};
1313

14-
use crate::config::{Config, GroupImportsTactic, WildcardGroups};
14+
use crate::config::{Config, GroupImportsTactic, WildcardGroup, WildcardGroups};
1515
use crate::imports::{normalize_use_trees_with_granularity, UseSegmentKind, UseTree};
1616
use crate::items::{is_mod_decl, rewrite_extern_crate, rewrite_mod};
1717
use crate::lists::{itemize_list, write_list, ListFormatting, ListItem};
@@ -173,7 +173,10 @@ fn contains_macro_use_attr(item: &ast::Item) -> bool {
173173

174174
fn group_imports_custom(wildcards: WildcardGroups, uts: Vec<UseTree>) -> Vec<Vec<UseTree>> {
175175
let mut groups = vec![vec![]; wildcards.len() + 1];
176-
let fallback_group = groups.len() - 1;
176+
let fallback_group = wildcards
177+
.iter()
178+
.position(WildcardGroup::is_fallback)
179+
.unwrap_or(groups.len() - 1);
177180

178181
for ut in uts.into_iter() {
179182
let ut_path_str = ut.to_string();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
group_imports = [
22
["$std::*", "proc_macro::*"],
3+
["*"],
34
["my_crate::*", "crate::*::xyz"],
45
["$crate::*"],
56
]

tests/config/wildcard-fallback.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
group_imports = [
2+
["a", "b::c"],
3+
["$crate::x::*"],
4+
["$std::sync"],
5+
["*"],
6+
["*::ipsum::*"],
7+
["xyz::abc::dez"],
8+
]
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// rustfmt-unstable: true
2-
// rustfmt-config: wildcard-example.rs
2+
// rustfmt-config: wildcard-example.toml
33
use self::X;
44
use super::Y;
55
use crate::abc::xyz;
66
use crate::Z;
77
use my_crate::a::B;
88
use my_crate::A;
99
use proc_macro::Span;
10+
use rand;
1011
use std::rc::Rc;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-unstable: true
2+
// rustfmt-config: wildcard-fallback.toml
3+
use self::x;
4+
use crate::x::y;
5+
use crate::y;
6+
use a;
7+
use a::ipsum::b;
8+
use b::c;
9+
use b::c::d;
10+
use c::d::ipsum::b;
11+
use core::sync;
12+
use core::sync::A;
13+
use std::sync;
14+
use xyz::abc::dez;

tests/target/wildcard-group-import/config-example.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// rustfmt-unstable: true
2-
// rustfmt-config: wildcard-example.rs
2+
// rustfmt-config: wildcard-example.toml
33
use proc_macro::Span;
44
use std::rc::Rc;
55

6+
use rand;
7+
68
use crate::abc::xyz;
79
use my_crate::a::B;
810
use my_crate::A;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// rustfmt-unstable: true
2+
// rustfmt-config: wildcard-fallback.toml
3+
use a;
4+
use b::c;
5+
6+
use crate::x::y;
7+
8+
use core::sync;
9+
use std::sync;
10+
11+
use self::x;
12+
use crate::y;
13+
use b::c::d;
14+
use core::sync::A;
15+
16+
use a::ipsum::b;
17+
use c::d::ipsum::b;
18+
19+
use xyz::abc::dez;

0 commit comments

Comments
 (0)