Skip to content

Put each nested import on its own line #2769

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
Jun 6, 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
2 changes: 2 additions & 0 deletions src/config/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum DefinitiveListTactic {
Vertical,
Horizontal,
Mixed,
/// Tactic for nested import.
NestedImport,
/// Special case tactic for `format!()`, `write!()` style macros.
SpecialMacro(usize),
}
Expand Down
16 changes: 10 additions & 6 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,12 +706,16 @@ fn rewrite_nested_use_tree(
shape.width.saturating_sub(2)
};

let tactic = definitive_tactic(
&list_items,
context.config.imports_layout(),
Separator::Comma,
remaining_width,
);
let tactic = if has_nested_list {
DefinitiveListTactic::NestedImport
} else {
definitive_tactic(
&list_items,
context.config.imports_layout(),
Separator::Comma,
remaining_width,
)
};

let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
&& tactic != DefinitiveListTactic::Horizontal;
Expand Down
6 changes: 5 additions & 1 deletion src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ where
let sep_place =
SeparatorPlace::from_tactic(formatting.separator_place, tactic, formatting.separator);
let mut prev_item_had_post_comment = false;
let mut prev_item_is_nested_import = false;

let mut line_len = 0;
let indent_str = &formatting.shape.indent.to_string(formatting.config);
Expand Down Expand Up @@ -281,12 +282,14 @@ where
result.push('\n');
result.push_str(indent_str);
}
DefinitiveListTactic::Mixed => {
DefinitiveListTactic::Mixed | DefinitiveListTactic::NestedImport => {
let total_width = total_item_width(item) + item_sep_len;

// 1 is space between separator and item.
if (line_len > 0 && line_len + 1 + total_width > formatting.shape.width)
|| prev_item_had_post_comment
|| (tactic == DefinitiveListTactic::NestedImport
&& (prev_item_is_nested_import || (!first && inner_item.contains("::"))))
{
result.push('\n');
result.push_str(indent_str);
Expand Down Expand Up @@ -452,6 +455,7 @@ where
}

prev_item_had_post_comment = item.post_comment.is_some();
prev_item_is_nested_import = inner_item.contains("::");
}

Some(result)
Expand Down
2 changes: 2 additions & 0 deletions tests/source/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ use foo::{a, bar::{baz, qux, xxxxxxxxxxx, yyyyyyyyyyyyy, zzzzzzzzzzzzzzzz, foo::

use fooo::{baar::{foobar::{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz}}, z, bar, bar::*, x, y};

use exonum::{api::{Api, ApiError}, blockchain::{self, BlockProof, Blockchain, Transaction, TransactionSet}, crypto::{Hash, PublicKey}, helpers::Height, node::TransactionSend, storage::{ListProof, MapProof}};

// nested imports with a single sub-tree.
use a::{b::{c::*}};
use a::{b::{c::{}}};
Expand Down
3 changes: 2 additions & 1 deletion tests/target/configs/imports_layout/merge_mixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
// rustfmt-imports_layout: Mixed

use std::{
fmt, io, str::{self, FromStr},
fmt, io,
str::{self, FromStr},
};
18 changes: 15 additions & 3 deletions tests/target/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ use self::unix::{};
use foo::{
a, b,
bar::{
baz, foo::{a, b, cxxxxxxxxxxxxx, yyyyyyyyyyyyyy, zzzzzzzzzzzzzzzz}, qux, xxxxxxxxxxx,
yyyyyyyyyyyyy, zzzzzzzzzzzzzzzz,
baz,
foo::{a, b, cxxxxxxxxxxxxx, yyyyyyyyyyyyyy, zzzzzzzzzzzzzzzz},
qux, xxxxxxxxxxx, yyyyyyyyyyyyy, zzzzzzzzzzzzzzzz,
},
boo, c,
};
Expand All @@ -93,7 +94,18 @@ use fooo::{
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
},
bar, bar::*, x, y, z,
bar,
bar::*,
x, y, z,
};

use exonum::{
api::{Api, ApiError},
blockchain::{self, BlockProof, Blockchain, Transaction, TransactionSet},
crypto::{Hash, PublicKey},
helpers::Height,
node::TransactionSend,
storage::{ListProof, MapProof},
};

// nested imports with a single sub-tree.
Expand Down