Skip to content

Commit cf64f0d

Browse files
committed
Fix issues with formatting imports with comments
1 parent a9ae746 commit cf64f0d

File tree

3 files changed

+187
-2
lines changed

3 files changed

+187
-2
lines changed

src/imports.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ impl UseTree {
589589

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

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

1035-
let result = if (list_str.contains('\n') || list_str.len() > remaining_width)
1035+
let result = if (list_str.contains('\n')
1036+
|| list_str.len() > remaining_width
1037+
|| tactic == DefinitiveListTactic::Vertical)
10361038
&& context.config.imports_indent() == IndentStyle::Block
10371039
{
10381040
format!(

tests/source/use.rs

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
use foo :: bar
2+
;
3+
4+
use foo::{bar};
5+
6+
use foo::{
7+
bar
8+
// abc
9+
};
10+
11+
use foo::{
12+
bar,
13+
// abc
14+
};
15+
16+
use foo::{
17+
// 345
18+
bar
19+
};
20+
21+
use foo::{
22+
self
23+
// abc
24+
};
25+
26+
use foo::{
27+
self,
28+
// abc
29+
};
30+
31+
use foo::{
32+
// 345
33+
self
34+
};
35+
36+
use foo::{
37+
self // a
38+
,
39+
};
40+
41+
use foo::{ self /* a */ };
42+
43+
use foo::{ self /* a */, };
44+
45+
use foo::{
46+
// abc
47+
abc::{
48+
xyz
49+
// 123
50+
}
51+
};
52+
53+
use foo::{
54+
// abc
55+
bar,
56+
abc
57+
};
58+
59+
use foo::{
60+
bar,
61+
// abc
62+
abc
63+
};
64+
65+
use foo::{
66+
bar,
67+
abc
68+
// abc
69+
};
70+
71+
use foo::{
72+
bar,
73+
abc,
74+
// abc
75+
};
76+
77+
use foo::{
78+
self,
79+
// abc
80+
abc::{
81+
xyz
82+
// 123
83+
}
84+
};
85+
86+
use foo::{
87+
self,
88+
// abc
89+
abc::{
90+
// 123
91+
xyz
92+
}
93+
};
94+
95+
use path::{self /*comment*/,};

tests/target/use.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use foo::bar;
2+
3+
use foo::bar;
4+
5+
use foo::{
6+
bar, // abc
7+
};
8+
9+
use foo::{
10+
bar,
11+
// abc
12+
};
13+
14+
use foo::{
15+
// 345
16+
bar,
17+
};
18+
19+
use foo::{
20+
self, // abc
21+
};
22+
23+
use foo::{
24+
self,
25+
// abc
26+
};
27+
28+
use foo::{
29+
// 345
30+
self,
31+
};
32+
33+
use foo::{
34+
self, // a
35+
};
36+
37+
use foo::{self /* a */};
38+
39+
use foo::{self /* a */};
40+
41+
use foo::{
42+
// abc
43+
abc::{
44+
xyz, // 123
45+
},
46+
};
47+
48+
use foo::{
49+
abc,
50+
// abc
51+
bar,
52+
};
53+
54+
use foo::{
55+
// abc
56+
abc,
57+
bar,
58+
};
59+
60+
use foo::{
61+
abc, // abc
62+
bar,
63+
};
64+
65+
use foo::{
66+
abc,
67+
// abc
68+
bar,
69+
};
70+
71+
use foo::{
72+
self,
73+
// abc
74+
abc::{
75+
xyz, // 123
76+
},
77+
};
78+
79+
use foo::{
80+
self,
81+
// abc
82+
abc::{
83+
// 123
84+
xyz,
85+
},
86+
};
87+
88+
use path::{self /*comment*/};

0 commit comments

Comments
 (0)