Merge pull request #2795 from jechase/issue-2794

Add test and fix for #2794
This commit is contained in:
Nick Cameron 2018-06-20 10:35:16 +12:00 committed by GitHub
commit 1ead31ae9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 45 additions and 14 deletions

View File

@ -247,6 +247,7 @@ impl Rewrite for ast::MetaItem {
shape: item_shape,
ends_with_newline: false,
preserve_newline: false,
nested: false,
config: context.config,
};
let item_str = write_list(&item_vec, &fmt)?;

View File

@ -260,6 +260,7 @@ fn rewrite_closure_fn_decl(
shape: arg_shape,
ends_with_newline: false,
preserve_newline: true,
nested: false,
config: context.config,
};
let list_str = write_list(&item_vec, &fmt)?;

View File

@ -19,8 +19,6 @@ pub enum DefinitiveListTactic {
Vertical,
Horizontal,
Mixed,
/// Tactic for nested import.
NestedImport,
/// Special case tactic for `format!()`, `write!()` style macros.
SpecialMacro(usize),
}

View File

@ -1338,6 +1338,7 @@ pub fn rewrite_multiple_patterns(
shape,
ends_with_newline: false,
preserve_newline: false,
nested: false,
config: context.config,
};
write_list(&items, &fmt)
@ -1902,6 +1903,7 @@ where
shape,
ends_with_newline: false,
preserve_newline: false,
nested: false,
config: context.config,
};
let list_str = write_list(&item_vec, &fmt)?;

View File

@ -706,16 +706,12 @@ fn rewrite_nested_use_tree(
shape.width.saturating_sub(2)
};
let tactic = if has_nested_list {
DefinitiveListTactic::NestedImport
} else {
definitive_tactic(
&list_items,
context.config.imports_layout(),
Separator::Comma,
remaining_width,
)
};
let tactic = 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;
@ -731,6 +727,7 @@ fn rewrite_nested_use_tree(
shape: nested_shape,
ends_with_newline,
preserve_newline: true,
nested: has_nested_list,
config: context.config,
};

View File

@ -533,6 +533,7 @@ impl<'a> FmtVisitor<'a> {
shape,
ends_with_newline: true,
preserve_newline: true,
nested: false,
config: self.config,
};
@ -2307,6 +2308,7 @@ fn rewrite_args(
shape: Shape::legacy(budget, indent),
ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
preserve_newline: true,
nested: false,
config: context.config,
};
@ -2494,6 +2496,7 @@ fn rewrite_where_clause_rfc_style(
shape: clause_shape,
ends_with_newline: true,
preserve_newline: true,
nested: false,
config: context.config,
};
let preds_str = write_list(&items.collect::<Vec<_>>(), &fmt)?;
@ -2607,6 +2610,7 @@ fn rewrite_where_clause(
shape: Shape::legacy(budget, offset),
ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
preserve_newline: true,
nested: false,
config: context.config,
};
let preds_str = write_list(&item_vec, &fmt)?;

View File

@ -34,6 +34,8 @@ pub struct ListFormatting<'a> {
pub ends_with_newline: bool,
// Remove newlines between list elements for expressions.
pub preserve_newline: bool,
// Nested import lists get some special handling for the "Mixed" list type
pub nested: bool,
pub config: &'a Config,
}
@ -282,13 +284,13 @@ where
result.push('\n');
result.push_str(indent_str);
}
DefinitiveListTactic::Mixed | DefinitiveListTactic::NestedImport => {
DefinitiveListTactic::Mixed => {
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
|| (formatting.nested
&& (prev_item_is_nested_import || (!first && inner_item.contains("::"))))
{
result.push('\n');
@ -825,6 +827,7 @@ pub fn struct_lit_formatting<'a>(
shape,
ends_with_newline,
preserve_newline: true,
nested: false,
config: context.config,
}
}

View File

@ -399,6 +399,7 @@ pub fn rewrite_macro_def(
shape: arm_shape,
ends_with_newline: true,
preserve_newline: true,
nested: false,
config: context.config,
};

View File

@ -224,6 +224,7 @@ fn rewrite_match_arms(
shape: arm_shape,
ends_with_newline: true,
preserve_newline: true,
nested: false,
config: context.config,
};

View File

@ -388,6 +388,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
_ => false,
},
preserve_newline: false,
nested: false,
config: self.context.config,
};

View File

@ -77,6 +77,7 @@ fn wrap_reorderable_items(
shape,
ends_with_newline: true,
preserve_newline: false,
nested: false,
config: context.config,
};

View File

@ -366,6 +366,7 @@ where
shape: list_shape,
ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
preserve_newline: true,
nested: false,
config: context.config,
};

View File

@ -252,6 +252,7 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
shape: item_shape,
ends_with_newline: true,
preserve_newline: true,
nested: false,
config: context.config,
};
write_list(&items, &fmt)

View File

@ -0,0 +1,7 @@
// rustfmt-indent_style: Block
// rustfmt-imports_indent: Block
// rustfmt-imports_layout: Vertical
use std::{
env, fs, io::{Read, Write},
};

View File

@ -0,0 +1,12 @@
// rustfmt-indent_style: Block
// rustfmt-imports_indent: Block
// rustfmt-imports_layout: Vertical
use std::{
env,
fs,
io::{
Read,
Write,
},
};