Skip to content

Commit 4b2160e

Browse files
authored
Merge pull request #2599 from topecongiro/issue-2596
Use multiple lines for imports list which exceeds max width
2 parents a3fba04 + 97ec417 commit 4b2160e

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

src/imports.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,8 @@ fn rewrite_nested_use_tree(
497497

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

500-
let result = if list_str.contains('\n') && context.config.imports_indent() == IndentStyle::Block
500+
let result = if (list_str.contains('\n') || list_str.len() > remaining_width)
501+
&& context.config.imports_indent() == IndentStyle::Block
501502
{
502503
format!(
503504
"{{\n{}{}\n{}}}",

src/lists.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,10 @@ where
172172

173173
let limit = match tactic {
174174
_ if pre_line_comments => return DefinitiveListTactic::Vertical,
175-
ListTactic::Mixed => return DefinitiveListTactic::Mixed,
176175
ListTactic::Horizontal => return DefinitiveListTactic::Horizontal,
177176
ListTactic::Vertical => return DefinitiveListTactic::Vertical,
178177
ListTactic::LimitedHorizontalVertical(limit) => ::std::cmp::min(width, limit),
179-
ListTactic::HorizontalVertical => width,
178+
ListTactic::Mixed | ListTactic::HorizontalVertical => width,
180179
};
181180

182181
let (sep_count, total_width) = calculate_width(items.clone());
@@ -188,7 +187,10 @@ where
188187
{
189188
DefinitiveListTactic::Horizontal
190189
} else {
191-
DefinitiveListTactic::Vertical
190+
match tactic {
191+
ListTactic::Mixed => DefinitiveListTactic::Mixed,
192+
_ => DefinitiveListTactic::Vertical,
193+
}
192194
}
193195
}
194196

@@ -278,8 +280,7 @@ where
278280

279281
if last && formatting.ends_with_newline {
280282
match formatting.trailing_separator {
281-
SeparatorTactic::Always => separate = true,
282-
SeparatorTactic::Vertical if result.contains('\n') => separate = true,
283+
SeparatorTactic::Always | SeparatorTactic::Vertical => separate = true,
283284
_ => (),
284285
}
285286
}

tests/source/imports_block_indent.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// rustfmt-imports_indent: Block
2+
3+
// #2569
4+
use apns2::request::notification::{Notificatio, NotificationBuilder, Priority, SilentNotificationBuilder};

tests/target/imports_block_indent.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-imports_indent: Block
2+
3+
// #2569
4+
use apns2::request::notification::{
5+
Notificatio, NotificationBuilder, Priority, SilentNotificationBuilder,
6+
};

0 commit comments

Comments
 (0)