Skip to content

Commit 42ab258

Browse files
committed
Put each nested import on its own line
while putting non-nested imports on the same line as much as possible.
1 parent 35fee77 commit 42ab258

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
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)

0 commit comments

Comments
 (0)