Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another fix for merge_imports #3769

Merged
merged 1 commit into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,16 @@ pub(crate) fn merge_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
}

for flattened in use_tree.flatten() {
merge_use_trees_inner(&mut result, flattened);
if let Some(tree) = result.iter_mut().find(|tree| tree.share_prefix(&flattened)) {
tree.merge(&flattened);
} else {
result.push(flattened);
}
}
}
result
}

fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree) {
for tree in trees.iter_mut() {
if tree.share_prefix(&use_tree) {
tree.merge(&use_tree);
return;
}
}

trees.push(use_tree);
}

impl fmt::Debug for UseTree {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
Expand Down Expand Up @@ -595,7 +588,6 @@ fn merge_rest(a: &[UseSegment], b: &[UseSegment], mut len: usize) -> Option<Vec<
if a.len() != len && b.len() != len {
if let UseSegment::List(mut list) = a[len].clone() {
merge_use_trees_inner(&mut list, UseTree::from_path(b[len..].to_vec(), DUMMY_SP));
list.sort();
let mut new_path = b[..len].to_vec();
new_path.push(UseSegment::List(list));
return Some(new_path);
Expand All @@ -622,6 +614,26 @@ fn merge_rest(a: &[UseSegment], b: &[UseSegment], mut len: usize) -> Option<Vec<
Some(new_path)
}

fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree) {
let similar_trees = trees.iter_mut().filter(|tree| tree.share_prefix(&use_tree));
if use_tree.path.len() == 1 {
if let Some(tree) = similar_trees.min_by_key(|tree| tree.path.len()) {
if tree.path.len() == 1 {
return;
}
}
} else {
if let Some(tree) = similar_trees.max_by_key(|tree| tree.path.len()) {
if tree.path.len() > 1 {
tree.merge(&use_tree);
return;
}
}
}
trees.push(use_tree);
trees.sort();
}

impl PartialOrd for UseSegment {
fn partial_cmp(&self, other: &UseSegment) -> Option<Ordering> {
Some(self.cmp(other))
Expand Down Expand Up @@ -988,7 +1000,12 @@ mod test {
test_merge!(["a::b::{c, d}", "a::b::{e, f}"], ["a::b::{c, d, e, f}"]);
test_merge!(["a::b::c", "a::b"], ["a::{b, b::c}"]);
test_merge!(["a::b", "a::b"], ["a::b"]);
test_merge!(["a", "a::b", "a::b::c"], ["a::{self, b::{self, c}}"]);
test_merge!(["a", "a::b", "a::b::c"], ["a::{self, b, b::c}"]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, looking back to this test, merging use a; into use a::{self}; does not seem to be correct w.r.t. #3750.
@valff IIUC this has existed before this PR, but while you are at it, would you mind having a look if you have time? This won't be a block for this PR - if this is not easy to fix then we can leave it for the future work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@topecongiro I have kept use a::{self} as a special case where self is at depth 2, because the first path segment is always a crate name or a special keyword (at least in edition 2018?). So if the first segment is always a module (crate), it should be correct w.r.t. #3750. I find this special case useful because this way we still have a single use statement per crate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I misunderstood this. Thanks!

test_merge!(
["a", "a::b", "a::b::c", "a::b::c::d"],
["a::{self, b, b::{c, c::d}}"]
);
test_merge!(["a", "a::b", "a::b::c", "a::b"], ["a::{self, b, b::c}"]);
test_merge!(
["a::{b::{self, c}, d::e}", "a::d::f"],
["a::{b::{self, c}, d::{e, f}}"]
Expand Down
5 changes: 1 addition & 4 deletions tests/target/configs/imports_layout/merge_mixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@
// rustfmt-merge_imports: true
// rustfmt-imports_layout: Mixed

use std::{
fmt, io,
str::{self, FromStr},
};
use std::{fmt, io, str, str::FromStr};