Skip to content

Commit 15a28f7

Browse files
valfftopecongiro
authored andcommitted
Another fix for merge_imports (#3769)
1 parent 950b288 commit 15a28f7

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

src/imports.rs

+31-14
Original file line numberDiff line numberDiff line change
@@ -166,23 +166,16 @@ pub(crate) fn merge_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
166166
}
167167

168168
for flattened in use_tree.flatten() {
169-
merge_use_trees_inner(&mut result, flattened);
169+
if let Some(tree) = result.iter_mut().find(|tree| tree.share_prefix(&flattened)) {
170+
tree.merge(&flattened);
171+
} else {
172+
result.push(flattened);
173+
}
170174
}
171175
}
172176
result
173177
}
174178

175-
fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree) {
176-
for tree in trees.iter_mut() {
177-
if tree.share_prefix(&use_tree) {
178-
tree.merge(&use_tree);
179-
return;
180-
}
181-
}
182-
183-
trees.push(use_tree);
184-
}
185-
186179
impl fmt::Debug for UseTree {
187180
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
188181
fmt::Display::fmt(self, f)
@@ -595,7 +588,6 @@ fn merge_rest(a: &[UseSegment], b: &[UseSegment], mut len: usize) -> Option<Vec<
595588
if a.len() != len && b.len() != len {
596589
if let UseSegment::List(mut list) = a[len].clone() {
597590
merge_use_trees_inner(&mut list, UseTree::from_path(b[len..].to_vec(), DUMMY_SP));
598-
list.sort();
599591
let mut new_path = b[..len].to_vec();
600592
new_path.push(UseSegment::List(list));
601593
return Some(new_path);
@@ -622,6 +614,26 @@ fn merge_rest(a: &[UseSegment], b: &[UseSegment], mut len: usize) -> Option<Vec<
622614
Some(new_path)
623615
}
624616

617+
fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree) {
618+
let similar_trees = trees.iter_mut().filter(|tree| tree.share_prefix(&use_tree));
619+
if use_tree.path.len() == 1 {
620+
if let Some(tree) = similar_trees.min_by_key(|tree| tree.path.len()) {
621+
if tree.path.len() == 1 {
622+
return;
623+
}
624+
}
625+
} else {
626+
if let Some(tree) = similar_trees.max_by_key(|tree| tree.path.len()) {
627+
if tree.path.len() > 1 {
628+
tree.merge(&use_tree);
629+
return;
630+
}
631+
}
632+
}
633+
trees.push(use_tree);
634+
trees.sort();
635+
}
636+
625637
impl PartialOrd for UseSegment {
626638
fn partial_cmp(&self, other: &UseSegment) -> Option<Ordering> {
627639
Some(self.cmp(other))
@@ -988,7 +1000,12 @@ mod test {
9881000
test_merge!(["a::b::{c, d}", "a::b::{e, f}"], ["a::b::{c, d, e, f}"]);
9891001
test_merge!(["a::b::c", "a::b"], ["a::{b, b::c}"]);
9901002
test_merge!(["a::b", "a::b"], ["a::b"]);
991-
test_merge!(["a", "a::b", "a::b::c"], ["a::{self, b::{self, c}}"]);
1003+
test_merge!(["a", "a::b", "a::b::c"], ["a::{self, b, b::c}"]);
1004+
test_merge!(
1005+
["a", "a::b", "a::b::c", "a::b::c::d"],
1006+
["a::{self, b, b::{c, c::d}}"]
1007+
);
1008+
test_merge!(["a", "a::b", "a::b::c", "a::b"], ["a::{self, b, b::c}"]);
9921009
test_merge!(
9931010
["a::{b::{self, c}, d::e}", "a::d::f"],
9941011
["a::{b::{self, c}, d::{e, f}}"]

tests/target/configs/imports_layout/merge_mixed.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@
22
// rustfmt-merge_imports: true
33
// rustfmt-imports_layout: Mixed
44

5-
use std::{
6-
fmt, io,
7-
str::{self, FromStr},
8-
};
5+
use std::{fmt, io, str, str::FromStr};

0 commit comments

Comments
 (0)