From 261865ecc9f83cec4264c534c4498942bfd3eb46 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Wed, 26 Jul 2017 22:43:36 +0900 Subject: [PATCH] Remove newlines between list elements for expressions --- src/expr.rs | 5 +++++ src/imports.rs | 1 + src/items.rs | 5 +++++ src/lists.rs | 7 ++++++- src/types.rs | 1 + src/vertical.rs | 1 + src/visitor.rs | 1 + 7 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/expr.rs b/src/expr.rs index 35e1e432733..06b32ab9ff5 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -567,6 +567,7 @@ pub fn rewrite_array<'a, I>( }, shape: nested_shape, ends_with_newline: ends_with_newline, + preserve_newline: false, config: context.config, }; let list_str = try_opt!(write_list(&items, &fmt)); @@ -642,6 +643,7 @@ fn rewrite_closure_fn_decl( trailing_separator: SeparatorTactic::Never, shape: arg_shape, ends_with_newline: false, + preserve_newline: true, config: context.config, }; let list_str = try_opt!(write_list(&item_vec, &fmt)); @@ -1696,6 +1698,7 @@ fn rewrite_match_pattern( trailing_separator: SeparatorTactic::Never, shape: pat_shape, ends_with_newline: false, + preserve_newline: false, config: context.config, }; let pats_str = try_opt!(write_list(&items, &fmt)); @@ -2188,6 +2191,7 @@ fn rewrite_call_args<'a, T>( }, shape: shape, ends_with_newline: context.use_block_indent() && tactic == DefinitiveListTactic::Vertical, + preserve_newline: false, config: context.config, }; @@ -2783,6 +2787,7 @@ fn rewrite_tuple_in_visual_indent_style<'a, T>( trailing_separator: SeparatorTactic::Never, shape: shape, ends_with_newline: false, + preserve_newline: false, config: context.config, }; let list_str = try_opt!(write_list(&item_vec, &fmt)); diff --git a/src/imports.rs b/src/imports.rs index d506fb583b4..a5ebb65c099 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -478,6 +478,7 @@ fn rewrite_use_list( }, shape: nested_shape, ends_with_newline: ends_with_newline, + preserve_newline: true, config: context.config, }; let list_str = try_opt!(write_list(&items[first_index..], &fmt)); diff --git a/src/items.rs b/src/items.rs index ac19e85a113..5f976d7283e 100644 --- a/src/items.rs +++ b/src/items.rs @@ -477,6 +477,7 @@ fn format_variant_list( trailing_separator: self.config.trailing_comma(), shape: shape, ends_with_newline: true, + preserve_newline: true, config: self.config, }; @@ -2252,6 +2253,7 @@ enum ArgumentKind<'a> { }, shape: Shape::legacy(budget, indent), ends_with_newline: tactic.ends_with_newline(context.config.fn_args_layout()), + preserve_newline: true, config: context.config, }; @@ -2425,6 +2427,7 @@ pub fn format_generics_item_list( }, shape: shape, ends_with_newline: tactic.ends_with_newline(context.config.generics_indent()), + preserve_newline: true, config: context.config, }; @@ -2538,6 +2541,7 @@ fn rewrite_where_clause_rfc_style( trailing_separator: comma_tactic, shape: clause_shape, ends_with_newline: true, + preserve_newline: true, config: context.config, }; let preds_str = try_opt!(write_list(&items.collect::>(), &fmt)); @@ -2639,6 +2643,7 @@ fn rewrite_where_clause( trailing_separator: comma_tactic, shape: Shape::legacy(budget, offset), ends_with_newline: tactic.ends_with_newline(context.config.where_pred_indent()), + preserve_newline: true, config: context.config, }; let preds_str = try_opt!(write_list(&item_vec, &fmt)); diff --git a/src/lists.rs b/src/lists.rs index 6dfab314d97..5a6225a5903 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -65,6 +65,8 @@ pub struct ListFormatting<'a> { // Non-expressions, e.g. items, will have a new line at the end of the list. // Important for comment styles. pub ends_with_newline: bool, + // Remove newlines between list elements for expressions. + pub preserve_newline: bool, pub config: &'a Config, } @@ -342,7 +344,9 @@ pub fn write_list(items: I, formatting: &ListFormatting) -> Option item_max_width = None; } - if !last && tactic == DefinitiveListTactic::Vertical && item.new_lines { + if formatting.preserve_newline && !last && tactic == DefinitiveListTactic::Vertical && + item.new_lines + { item_max_width = None; result.push('\n'); } @@ -675,6 +679,7 @@ pub fn struct_lit_formatting<'a>( }, shape: shape, ends_with_newline: ends_with_newline, + preserve_newline: true, config: context.config, } } diff --git a/src/types.rs b/src/types.rs index c34a3bfaa6a..0439bf2cfac 100644 --- a/src/types.rs +++ b/src/types.rs @@ -361,6 +361,7 @@ enum ArgumentKind }, shape: list_shape, ends_with_newline: tactic.ends_with_newline(context.config.fn_call_style()), + preserve_newline: true, config: context.config, }; diff --git a/src/vertical.rs b/src/vertical.rs index 04d9ac9d35b..57876d644c2 100644 --- a/src/vertical.rs +++ b/src/vertical.rs @@ -229,6 +229,7 @@ fn rewrite_aligned_items_inner( trailing_separator: context.config.trailing_comma(), shape: item_shape, ends_with_newline: true, + preserve_newline: true, config: context.config, }; write_list(&items, &fmt) diff --git a/src/visitor.rs b/src/visitor.rs index 0f7005e2856..5daa9109cad 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -820,6 +820,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { trailing_separator: SeparatorTactic::Never, shape: item_shape, ends_with_newline: false, + preserve_newline: false, config: context.config, }; format!("{}({})", name, try_opt!(write_list(&item_vec, &fmt)))