Skip to content

Use multiple lines for imports list which exceeds max width #2599

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

Merged
merged 3 commits into from
Apr 5, 2018
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
3 changes: 2 additions & 1 deletion src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ fn rewrite_nested_use_tree(
};
let list_str = write_list(&items[first_index..], &fmt)?;

let result = if list_str.contains('\n') && context.config.imports_indent() == IndentStyle::Block
let result = if (list_str.contains('\n') || list_str.len() > remaining_width)
&& context.config.imports_indent() == IndentStyle::Block
{
format!(
"{}{{\n{}{}\n{}}}",
Expand Down
11 changes: 6 additions & 5 deletions src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,10 @@ where

let limit = match tactic {
_ if pre_line_comments => return DefinitiveListTactic::Vertical,
ListTactic::Mixed => return DefinitiveListTactic::Mixed,
ListTactic::Horizontal => return DefinitiveListTactic::Horizontal,
ListTactic::Vertical => return DefinitiveListTactic::Vertical,
ListTactic::LimitedHorizontalVertical(limit) => ::std::cmp::min(width, limit),
ListTactic::HorizontalVertical => width,
ListTactic::Mixed | ListTactic::HorizontalVertical => width,
};

let (sep_count, total_width) = calculate_width(items.clone());
Expand All @@ -188,7 +187,10 @@ where
{
DefinitiveListTactic::Horizontal
} else {
DefinitiveListTactic::Vertical
match tactic {
ListTactic::Mixed => DefinitiveListTactic::Mixed,
_ => DefinitiveListTactic::Vertical,
}
}
}

Expand Down Expand Up @@ -276,8 +278,7 @@ where

if last && formatting.ends_with_newline {
match formatting.trailing_separator {
SeparatorTactic::Always => separate = true,
SeparatorTactic::Vertical if result.contains('\n') => separate = true,
SeparatorTactic::Always | SeparatorTactic::Vertical => separate = true,
_ => (),
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/source/imports_block_indent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// rustfmt-imports_indent: Block

// #2569
use apns2::request::notification::{Notificatio, NotificationBuilder, Priority, SilentNotificationBuilder};
6 changes: 6 additions & 0 deletions tests/target/imports_block_indent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-imports_indent: Block

// #2569
use apns2::request::notification::{
Notificatio, NotificationBuilder, Priority, SilentNotificationBuilder,
};