8000 Move imports_granularity=Item logic into sep fn. · rust-lang/rustfmt@9c9ad03 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c9ad03

Browse files
committed
Move imports_granularity=Item logic into sep fn.
1 parent ff07d6a commit 9c9ad03

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

src/formatting/imports.rs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,26 @@ pub(crate) fn merge_use_trees(use_trees: Vec<UseTree>, merge_by: SharedPrefix) -
179179
}
180180
}
181181
}
182-
for merged in &mut result {
183-
// If a path ends with `::self`, rewrite it to `::{self}`.
184-
if let Some(UseSegment::Slf(..)) = merged.path.last() {
185-
let slf_segment = merged.path.pop().unwrap();
186-
merged.path.push(UseSegment::List(vec![UseTree::from_path(
187-
vec![slf_segment],
188-
DUMMY_SP,
189-
)]));
190-
}
191-
}
192182
result
193183
}
194184

185+
pub(crate) fn flatten_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
186+
use_trees
187+
.into_iter()
188+
.flat_map(UseTree::flatten)
189+
.map(|mut tree| {
190+
if let Some(UseSegment::Slf(..)) = tree.path.last() {
191+
let self_segment = tree.path.pop().unwrap();
192+
tree.path.push(UseSegment::List(vec![UseTree::from_path(
193+
vec![self_segment],
194+
DUMMY_SP,
195+
)]));
196+
}
197+
tree
198+
})
199+
.collect()
200+
}
201+
195202
impl fmt::Debug for UseTree {
196203
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
197204
fmt::Display::fmt(self, f)
@@ -552,25 +559,24 @@ impl UseTree {
552559
SharedPrefix::Module => {
553560
self.path[..self.path.len() - 1] == other.path[..other.path.len() - 1]
554561
}
555-
SharedPrefix::NoPrefix => false,
556562
}
557563
}
558564
}
559565

560566
fn flatten(self) -> Vec<UseTree> {
561567
match self.path.last() {
562568
Some(UseSegment::List(list)) => {
569+
if list.len() == 1 && list[0].path.len() == 1 {
570+
if let UseSegment::Slf(..) = list[0].path[0] {
571+
return vec![self];
572+
};
573+
}
563574
let prefix = &self.path[..self.path.len() - 1];
564575
let mut result = vec![];
565576
for nested_use_tree in list {
566577
for flattened in &mut nested_use_tree.clone().flatten() {
567578
let mut new_path = prefix.to_vec();
568579
new_path.append(&mut flattened.path);
569-
if flattened.path.len() == 1 {
570-
if let UseSegment::Slf(..) = flattened.path[0] {
571-
new_path.pop();
572-
}
573-
}
574580
result.push(UseTree {
575581
path: new_path,
576582
span: self.span,
@@ -865,7 +871,6 @@ impl Rewrite for UseTree {
865871
pub(crate) enum SharedPrefix {
866872
Crate,
867873
Module,
868-
NoPrefix D7AE ,
869874
}
870875

871876
#[cfg(test)]
@@ -1081,24 +1086,22 @@ mod test {
10811086
}
10821087

10831088
#[test]
1084-
fn test_use_tree_merge_no_prefix() {
1085-
test_merge!(
1086-
NoPrefix,
1087-
["foo::{a::{b, c}, d::e}"],
1088-
["foo::a::b", "foo::a::c", "foo::d::e"]
1089+
fn test_flatten_use_trees() {
1090+
assert_eq!(
1091+
flatten_use_trees(parse_use_trees!["foo::{a::{b, c}, d::e}"]),
1092+
parse_use_trees!["foo::a::b", "foo::a::c", "foo::d::e"]
10891093
);
10901094

1091-
test_merge!(
1092-
NoPrefix,
1093-
["foo::{self, a, b::{c, d}, e::*}"],
1094-
[
1095+
assert_eq!(
1096+
flatten_use_trees(parse_use_trees!["foo::{self, a, b::{c, d}, e::*}"]),
1097+
parse_use_trees![
10951098
"foo::{self}",
10961099
"foo::a",
10971100
"foo::b::c",
10981101
"foo::b::d",
10991102
"foo::e::*"
11001103
]
1101-
)
1104+
);
11021105
}
11031106

11041107
#[test]

src/formatting/reorder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_ast::ast;
1212
use rustc_span::{symbol::sym, Span};
1313

1414
use crate::config::{Config, GroupImportsTactic, ImportGranularity};
15-
use crate::formatting::imports::UseSegment;
15+
use crate::formatting::imports::{flatten_use_trees, UseSegment};
1616
use crate::formatting::modules::{get_mod_inner_attrs, FileModMap};
1717
use crate::formatting::{
1818
imports::{merge_use_trees, UseTree},
@@ -233,9 +233,7 @@ fn rewrite_reorderable_or_regroupable_items(
233233
ImportGranularity::Module => {
234234
merge_use_trees(normalized_items, SharedPrefix::Module)
235235
}
236-
ImportGranularity::Item => {
237-
merge_use_trees(normalized_items, SharedPrefix::NoPrefix)
238-
}
236+
ImportGranularity::Item => flatten_use_trees(normalized_items),
239237
ImportGranularity::Preserve => normalized_items,
240238
};
241239

0 commit comments

Comments
 (0)
0