Add a heuristic for maximum number of elements in a single-line chain

And turn the source hints option to false by default. This should make formatting more deterministic.
This commit is contained in:
Nick Cameron 2017-03-07 09:34:28 +13:00
parent 63114f3cac
commit c7a33062e2
2 changed files with 6 additions and 2 deletions

View File

@ -162,7 +162,10 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
.iter()
.fold(0, |a, b| a + first_line_width(b)) + parent_rewrite.len();
let veto_single_line = if context.config.take_source_hints && subexpr_list.len() > 1 {
let veto_single_line = if subexpr_list.len() > context.config.chain_one_line_max - 1 {
// -1 above because subexpr_list does not include the parent.
true
} else if context.config.take_source_hints && subexpr_list.len() > 1 {
// Look at the source code. Unless all chain elements start on the same
// line, we won't consider putting them on a single line either.
let last_span = context.snippet(mk_sp(subexpr_list[1].span.hi, total_span.hi));

View File

@ -378,6 +378,7 @@ create_config! {
report_fixme: ReportTactic, ReportTactic::Never,
"Report all, none or unnumbered occurrences of FIXME in source file comments";
chain_indent: BlockIndentStyle, BlockIndentStyle::Tabbed, "Indentation of chain";
chain_one_line_max: usize, 4, "Maximum number of elements in a chain to fit on a single line";
reorder_imports: bool, false, "Reorder import statements alphabetically";
reorder_imported_names: bool, false,
"Reorder lists of names in import statements alphabetically";
@ -386,7 +387,7 @@ create_config! {
if-else expressions.";
format_strings: bool, false, "Format string literals where necessary";
force_format_strings: bool, false, "Always format string literals";
take_source_hints: bool, true, "Retain some formatting characteristics from the source code";
take_source_hints: bool, false, "Retain some formatting characteristics from the source code";
hard_tabs: bool, false, "Use tab characters for indentation, spaces for alignment";
wrap_comments: bool, false, "Break comments to fit on the line";
normalize_comments: bool, false, "Convert /* */ comments to // comments where possible";