Skip to content

Commit

Permalink
Preserve comments when flattening use statements
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlattimore committed Apr 17, 2022
1 parent a37d3ab commit 356c85b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,13 +610,13 @@ impl UseTree {
let prefix = &self.path[..self.path.len() - 1];
let mut result = vec![];
for nested_use_tree in list {
for flattend in &mut nested_use_tree.clone().flatten(import_granularity) {
for mut flattend in nested_use_tree.clone().flatten(import_granularity) {
let mut new_path = prefix.to_vec();
new_path.append(&mut flattend.path);
result.push(UseTree {
path: new_path,
span: self.span,
list_item: None,
list_item: flattend.list_item,
visibility: self.visibility.clone(),
// only retain attributes for `ImportGranularity::Item`
attrs: match import_granularity {
Expand Down
10 changes: 9 additions & 1 deletion tests/source/imports_granularity_item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
// rustfmt-imports_granularity: Item

use a::{b, c, d};
use a::{
b,
c,
// Comment before a::d
d,
e, // Comment after a::e
};
use a::{f::g, h::{i, j}};
use a::{l::{self, m, n::o, p::*}};
use a::q::{self};
// Comment before a::r
use a::r;
4 changes: 4 additions & 0 deletions tests/target/imports_granularity_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

use a::b;
use a::c;
// Comment before a::d
use a::d;
use a::e; // Comment after a::e
use a::f::g;
use a::h::i;
use a::h::j;
Expand All @@ -11,3 +13,5 @@ use a::l::n::o;
use a::l::p::*;
use a::l::{self};
use a::q::{self};
// Comment before a::r
use a::r;
2 changes: 2 additions & 0 deletions tests/target/imports_granularity_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use foo::e;
#[cfg(test)]
use foo::{a::b, c::d};

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

0 comments on commit 356c85b

Please sign in to comment.