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

import_granularity: Don't normalize imports with comments #5311

Merged
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
17 changes: 15 additions & 2 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ impl UseSegment {
}
})
}

fn contains_comment(&self) -> bool {
if let UseSegment::List(list) = self {
list.iter().any(|subtree| subtree.contains_comment())
} else {
false
}
}
}

pub(crate) fn normalize_use_trees_with_granularity(
Expand All @@ -197,7 +205,7 @@ pub(crate) fn normalize_use_trees_with_granularity(

let mut result = Vec::with_capacity(use_trees.len());
for use_tree in use_trees {
if use_tree.has_comment() || use_tree.attrs.is_some() {
if use_tree.contains_comment() || use_tree.attrs.is_some() {
result.push(use_tree);
continue;
}
Expand Down Expand Up @@ -556,6 +564,10 @@ impl UseTree {
self.list_item.as_ref().map_or(false, ListItem::has_comment)
}

fn contains_comment(&self) -> bool {
self.has_comment() || self.path.iter().any(|path| path.contains_comment())
}

fn same_visibility(&self, other: &UseTree) -> bool {
match (&self.visibility, &other.visibility) {
(
Expand All @@ -582,6 +594,7 @@ impl UseTree {
if self.path.is_empty()
|| other.path.is_empty()
|| self.attrs.is_some()
|| self.contains_comment()
|| !self.same_visibility(other)
{
false
Expand All @@ -597,7 +610,7 @@ impl UseTree {
}

fn flatten(self, import_granularity: ImportGranularity) -> Vec<UseTree> {
if self.path.is_empty() {
if self.path.is_empty() || self.contains_comment() {
return vec![self];
}
match self.path.clone().last().unwrap() {
Expand Down
28 changes: 28 additions & 0 deletions tests/source/imports_granularity_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,31 @@ use j::{a::{self}};

use {k::{a, b}, l::{a, b}};
use {k::{c, d}, l::{c, d}};

use b::{f::g, h::{i, j} /* After b::h group */};
use b::e;
use b::{/* Before b::l group */ l::{self, m, n::o, p::*}, q};
use b::d;
use b::r; // After b::r
use b::q::{self /* After b::q::self */};
use b::u::{
a,
b,
};
use b::t::{
// Before b::t::a
a,
b,
};
use b::s::{
a,
b, // After b::s::b
};
use b::v::{
// Before b::v::a
a,
// Before b::v::b
b,
};
use b::t::{/* Before b::t::self */ self};
use b::c;
28 changes: 28 additions & 0 deletions tests/source/imports_granularity_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,31 @@ use a::{b, c, d};
use a::{f::g, h::{i, j}};
use a::{l::{self, m, n::o, p::*}};
use a::q::{self};

use b::{f::g, h::{i, j} /* After b::h group */};
use b::e;
use b::{/* Before b::l group */ l::{self, m, n::o, p::*}, q};
use b::d;
use b::r; // After b::r
use b::q::{self /* After b::q::self */};
use b::u::{
a,
b,
};
use b::t::{
// Before b::t::a
a,
b,
};
use b::s::{
a,
b, // After b::s::b
};
use b::v::{
// Before b::v::a
a,
// Before b::v::b
b,
};
use b::t::{/* Before b::t::self */ self};
use b::c;
28 changes: 28 additions & 0 deletions tests/source/imports_granularity_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,31 @@ use bar::{
c::d,
e::f,
};

use b::{f::g, h::{i, j} /* After b::h group */};
use b::e;
use b::{/* Before b::l group */ l::{self, m, n::o, p::*}, q};
use b::d;
use b::r; // After b::r
use b::q::{self /* After b::q::self */};
use b::u::{
a,
b,
};
use b::t::{
// Before b::t::a
a,
b,
};
use b::s::{
a,
b, // After b::s::b
};
use b::v::{
// Before b::v::a
a,
// Before b::v::b
b,
};
use b::t::{/* Before b::t::self */ self};
use b::c;
28 changes: 28 additions & 0 deletions tests/source/imports_granularity_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,31 @@ use a::{
};
use b as x;
use a::ad::ada;

use b::{f::g, h::{i, j} /* After b::h group */};
use b::e;
use b::{/* Before b::l group */ l::{self, m, n::o, p::*}, q};
use b::d;
use b::r; // After b::r
use b::q::{self /* After b::q::self */};
use b::u::{
a,
b,
};
use b::t::{
// Before b::t::a
a,
b,
};
use b::s::{
a,
b, // After b::s::b
};
use b::v::{
// Before b::v::a
a,
// Before b::v::b
b,
};
use b::t::{/* Before b::t::self */ self};
use b::c;
31 changes: 31 additions & 0 deletions tests/target/imports_granularity_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,34 @@ use j::a::{self};

use k::{a, b, c, d};
use l::{a, b, c, d};

use b::q::{self /* After b::q::self */};
use b::r; // After b::r
use b::s::{
a,
b, // After b::s::b
};
use b::t::{/* Before b::t::self */ self};
use b::t::{
// Before b::t::a
a,
b,
};
use b::v::{
// Before b::v::a
a,
// Before b::v::b
b,
};
use b::{
c, d, e,
u::{a, b},
};
use b::{
f::g,
h::{i, j}, /* After b::h group */
};
use b::{
/* Before b::l group */ l::{self, m, n::o, p::*},
q,
};
32 changes: 32 additions & 0 deletions tests/target/imports_granularity_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,35 @@ use a::l::n::o;
use a::l::p::*;
use a::l::{self};
use a::q::{self};

use b::c;
use b::d;
use b::e;
use b::q::{self /* After b::q::self */};
use b::r; // After b::r
use b::s::{
a,
b, // After b::s::b
};
use b::t::{/* Before b::t::self */ self};
use b::t::{
// Before b::t::a
a,
b,
};
use b::u::a;
use b::u::b;
use b::v::{
// Before b::v::a
a,
// Before b::v::b
b,
};
use b::{
f::g,
h::{i, j}, /* After b::h group */
};
use b::{
/* Before b::l group */ l::{self, m, n::o, p::*},
q,
};
39 changes: 36 additions & 3 deletions tests/target/imports_granularity_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,39 @@ use foo::e;
#[cfg(test)]
use foo::{a::b, c::d};

use bar::a::b;
use bar::c::d;
use bar::e::f;
use bar::{
// comment
a::b,
// more comment
c::d,
e::f,
};

use b::q::{self /* After b::q::self */};
use b::r; // After b::r
use b::s::{
a,
b, // After b::s::b
};
use b::t::{/* Before b::t::self */ self};
use b::t::{
// Before b::t::a
a,
b,
};
use b::u::{a, b};
use b::v::{
// Before b::v::a
a,
// Before b::v::b
b,
};
use b::{c, d, e};
use b::{
f::g,
h::{i, j}, /* After b::h group */
};
use b::{
/* Before b::l group */ l::{self, m, n::o, p::*},
q,
};
46 changes: 38 additions & 8 deletions tests/target/imports_granularity_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,42 @@ use {
c::{self, ca},
};

use {
a::{
aa::{aaa, aab},
ab,
ac::aca,
ad::ada,
},
b as x,
use a::{
// some comment
aa::{aaa, aab},
ab,
// another comment
ac::aca,
};
use {a::ad::ada, b as x};

use b::q::{self /* After b::q::self */};
use b::r; // After b::r
use b::s::{
a,
b, // After b::s::b
};
use b::t::{/* Before b::t::self */ self};
use b::t::{
// Before b::t::a
a,
b,
};
use b::v::{
// Before b::v::a
a,
// Before b::v::b
b,
};
use b::{
c, d, e,
u::{a, b},
};
use b::{
f::g,
h::{i, j}, /* After b::h group */
};
use b::{
/* Before b::l group */ l::{self, m, n::o, p::*},
q,
};