Put each nested import on its own line
while putting non-nested imports on the same line as much as possible.
This commit is contained in:
parent
35fee77d39
commit
42ab258757
3 changed files with 17 additions and 7 deletions
|
|
@ -19,6 +19,8 @@ pub enum DefinitiveListTactic {
|
||||||
Vertical,
|
Vertical,
|
||||||
Horizontal,
|
Horizontal,
|
||||||
Mixed,
|
Mixed,
|
||||||
|
/// Tactic for nested import.
|
||||||
|
NestedImport,
|
||||||
/// Special case tactic for `format!()`, `write!()` style macros.
|
/// Special case tactic for `format!()`, `write!()` style macros.
|
||||||
SpecialMacro(usize),
|
SpecialMacro(usize),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -706,12 +706,16 @@ fn rewrite_nested_use_tree(
|
||||||
shape.width.saturating_sub(2)
|
shape.width.saturating_sub(2)
|
||||||
};
|
};
|
||||||
|
|
||||||
let tactic = definitive_tactic(
|
let tactic = if has_nested_list {
|
||||||
&list_items,
|
DefinitiveListTactic::NestedImport
|
||||||
context.config.imports_layout(),
|
} else {
|
||||||
Separator::Comma,
|
definitive_tactic(
|
||||||
remaining_width,
|
&list_items,
|
||||||
);
|
context.config.imports_layout(),
|
||||||
|
Separator::Comma,
|
||||||
|
remaining_width,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
|
let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
|
||||||
&& tactic != DefinitiveListTactic::Horizontal;
|
&& tactic != DefinitiveListTactic::Horizontal;
|
||||||
|
|
|
||||||
|
|
@ -229,6 +229,7 @@ where
|
||||||
let sep_place =
|
let sep_place =
|
||||||
SeparatorPlace::from_tactic(formatting.separator_place, tactic, formatting.separator);
|
SeparatorPlace::from_tactic(formatting.separator_place, tactic, formatting.separator);
|
||||||
let mut prev_item_had_post_comment = false;
|
let mut prev_item_had_post_comment = false;
|
||||||
|
let mut prev_item_is_nested_import = false;
|
||||||
|
|
||||||
let mut line_len = 0;
|
let mut line_len = 0;
|
||||||
let indent_str = &formatting.shape.indent.to_string(formatting.config);
|
let indent_str = &formatting.shape.indent.to_string(formatting.config);
|
||||||
|
|
@ -281,12 +282,14 @@ where
|
||||||
result.push('\n');
|
result.push('\n');
|
||||||
result.push_str(indent_str);
|
result.push_str(indent_str);
|
||||||
}
|
}
|
||||||
DefinitiveListTactic::Mixed => {
|
DefinitiveListTactic::Mixed | DefinitiveListTactic::NestedImport => {
|
||||||
let total_width = total_item_width(item) + item_sep_len;
|
let total_width = total_item_width(item) + item_sep_len;
|
||||||
|
|
||||||
// 1 is space between separator and item.
|
// 1 is space between separator and item.
|
||||||
if (line_len > 0 && line_len + 1 + total_width > formatting.shape.width)
|
if (line_len > 0 && line_len + 1 + total_width > formatting.shape.width)
|
||||||
|| prev_item_had_post_comment
|
|| prev_item_had_post_comment
|
||||||
|
|| (tactic == DefinitiveListTactic::NestedImport
|
||||||
|
&& (prev_item_is_nested_import || (!first && inner_item.contains("::"))))
|
||||||
{
|
{
|
||||||
result.push('\n');
|
result.push('\n');
|
||||||
result.push_str(indent_str);
|
result.push_str(indent_str);
|
||||||
|
|
@ -452,6 +455,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
prev_item_had_post_comment = item.post_comment.is_some();
|
prev_item_had_post_comment = item.post_comment.is_some();
|
||||||
|
prev_item_is_nested_import = inner_item.contains("::");
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(result)
|
Some(result)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue