From c7a33062e25e1d80545ae6094d3faa99348c79e1 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Tue, 7 Mar 2017 09:34:28 +1300 Subject: [PATCH] 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. --- src/chains.rs | 5 ++++- src/config.rs | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/chains.rs b/src/chains.rs index e9bd220158f..a2c69b2cb42 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -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)); diff --git a/src/config.rs b/src/config.rs index 57b8ba190f8..8e0a839d29e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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";