Skip to content

Commit

Permalink
Fix issues with formatting imports with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rsammelson committed Jul 21, 2023
1 parent a9ae746 commit 2026c0b
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ impl UseTree {

// Normalise foo::{bar} -> foo::bar
if let UseSegmentKind::List(ref list) = last.kind {
if list.len() == 1 && list[0].to_string() != "self" {
if list.len() == 1 && list[0].to_string() != "self" && !list[0].has_comment() {
normalize_sole_list = true;
}
}
Expand Down Expand Up @@ -1032,7 +1032,9 @@ fn rewrite_nested_use_tree(

let list_str = write_list(&list_items, &fmt)?;

let result = if (list_str.contains('\n') || list_str.len() > remaining_width)
let result = if (list_str.contains('\n')
|| list_str.len() > remaining_width
|| tactic == DefinitiveListTactic::Vertical)
&& context.config.imports_indent() == IndentStyle::Block
{
format!(
Expand Down
104 changes: 104 additions & 0 deletions tests/source/issue-5852.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use std::{
fs,
// (temporarily commented, we'll need this again in a second) io,
};

use foo::{
self // this is important
};

use foo :: bar
;

use foo::{bar};

use foo::{
bar
// abc
};

use foo::{
bar,
// abc
};

use foo::{
// 345
bar
};

use foo::{
self
// abc
};

use foo::{
self,
// abc
};

use foo::{
// 345
self
};

use foo::{
self // a
,
};

use foo::{ self /* a */ };

use foo::{ self /* a */, };

use foo::{
// abc
abc::{
xyz
// 123
}
};

use foo::{
// abc
bar,
abc
};

use foo::{
bar,
// abc
abc
};

use foo::{
bar,
abc
// abc
};

use foo::{
bar,
abc,
// abc
};

use foo::{
self,
// abc
abc::{
xyz
// 123
}
};

use foo::{
self,
// abc
abc::{
// 123
xyz
}
};

use path::{self /*comment*/,};
97 changes: 97 additions & 0 deletions tests/target/issue-5852.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use std::{
fs,
// (temporarily commented, we'll need this again in a second) io,
};

use foo::{
self, // this is important
};

use foo::bar;

use foo::bar;

use foo::{
bar, // abc
};

use foo::{
bar,
// abc
};

use foo::{
// 345
bar,
};

use foo::{
self, // abc
};

use foo::{
self,
// abc
};

use foo::{
// 345
self,
};

use foo::{
self, // a
};

use foo::{self /* a */};

use foo::{self /* a */};

use foo::{
// abc
abc::{
xyz, // 123
},
};

use foo::{
abc,
// abc
bar,
};

use foo::{
// abc
abc,
bar,
};

use foo::{
abc, // abc
bar,
};

use foo::{
abc,
// abc
bar,
};

use foo::{
self,
// abc
abc::{
xyz, // 123
},
};

use foo::{
self,
// abc
abc::{
// 123
xyz,
},
};

use path::{self /*comment*/};

0 comments on commit 2026c0b

Please sign in to comment.