Skip to content

Commit 576e9af

Browse files
authored
Merge pull request #2769 from topecongiro/issue-2765
Put each nested import on its own line
2 parents f545dfe + 42ab258 commit 576e9af

File tree

6 files changed

+36
-11
lines changed

6 files changed

+36
-11
lines changed

src/config/lists.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub enum DefinitiveListTactic {
1919
Vertical,
2020
Horizontal,
2121
Mixed,
22+
/// Tactic for nested import.
23+
NestedImport,
2224
/// Special case tactic for `format!()`, `write!()` style macros.
2325
SpecialMacro(usize),
2426
}

src/imports.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -706,12 +706,16 @@ fn rewrite_nested_use_tree(
706706
shape.width.saturating_sub(2)
707707
};
708708

709-
let tactic = definitive_tactic(
710-
&list_items,
711-
context.config.imports_layout(),
712-
Separator::Comma,
713-
remaining_width,
714-
);
709+
let tactic = if has_nested_list {
710+
DefinitiveListTactic::NestedImport
711+
} else {
712+
definitive_tactic(
713+
&list_items,
714+
context.config.imports_layout(),
715+
Separator::Comma,
716+
remaining_width,
717+
)
718+
};
715719

716720
let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
717721
&& tactic != DefinitiveListTactic::Horizontal;

src/lists.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ where
229229
let sep_place =
230230
SeparatorPlace::from_tactic(formatting.separator_place, tactic, formatting.separator);
231231
let mut prev_item_had_post_comment = false;
232+
let mut prev_item_is_nested_import = false;
232233

233234
let mut line_len = 0;
234235
let indent_str = &formatting.shape.indent.to_string(formatting.config);
@@ -281,12 +282,14 @@ where
281282
result.push('\n');
282283
result.push_str(indent_str);
283284
}
284-
DefinitiveListTactic::Mixed => {
285+
DefinitiveListTactic::Mixed | DefinitiveListTactic::NestedImport => {
285286
let total_width = total_item_width(item) + item_sep_len;
286287

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

454457
prev_item_had_post_comment = item.post_comment.is_some();
458+
prev_item_is_nested_import = inner_item.contains("::");
455459
}
456460

457461
Some(result)

tests/source/imports.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ use foo::{a, bar::{baz, qux, xxxxxxxxxxx, yyyyyyyyyyyyy, zzzzzzzzzzzzzzzz, foo::
8383

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

86+
use exonum::{api::{Api, ApiError}, blockchain::{self, BlockProof, Blockchain, Transaction, TransactionSet}, crypto::{Hash, PublicKey}, helpers::Height, node::TransactionSend, storage::{ListProof, MapProof}};
87+
8688
// nested imports with a single sub-tree.
8789
use a::{b::{c::*}};
8890
use a::{b::{c::{}}};

tests/target/configs/imports_layout/merge_mixed.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
// rustfmt-imports_layout: Mixed
44

55
use std::{
6-
fmt, io, str::{self, FromStr},
6+
fmt, io,
7+
str::{self, FromStr},
78
};

tests/target/imports.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ use self::unix::{};
8282
use foo::{
8383
a, b,
8484
bar::{
85-
baz, foo::{a, b, cxxxxxxxxxxxxx, yyyyyyyyyyyyyy, zzzzzzzzzzzzzzzz}, qux, xxxxxxxxxxx,
86-
yyyyyyyyyyyyy, zzzzzzzzzzzzzzzz,
85+
baz,
86+
foo::{a, b, cxxxxxxxxxxxxx, yyyyyyyyyyyyyy, zzzzzzzzzzzzzzzz},
87+
qux, xxxxxxxxxxx, yyyyyyyyyyyyy, zzzzzzzzzzzzzzzz,
8788
},
8889
boo, c,
8990
};
@@ -93,7 +94,18 @@ use fooo::{
9394
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
9495
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
9596
},
96-
bar, bar::*, x, y, z,
97+
bar,
98+
bar::*,
99+
x, y, z,
100+
};
101+
102+
use exonum::{
103+
api::{Api, ApiError},
104+
blockchain::{self, BlockProof, Blockchain, Transaction, TransactionSet},
105+
crypto::{Hash, PublicKey},
106+
helpers::Height,
107+
node::TransactionSend,
108+
storage::{ListProof, MapProof},
97109
};
98110

99111
// nested imports with a single sub-tree.

0 commit comments

Comments
 (0)