From 3dcd3d7fb0452403e050e3b01c5678b0403c41f8 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Sat, 17 Jun 2017 21:11:55 +0900 Subject: [PATCH] Combine condition and body of control flow If the condition of control flow expressions ends with closing parens and alike, put the opening bracket of the body on the same line with closing parens. --- src/chains.rs | 9 ++------- src/expr.rs | 5 +++-- src/utils.rs | 9 +++++++++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/chains.rs b/src/chains.rs index 9b27c92c270a..1729505d63b6 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -78,7 +78,7 @@ use Shape; use rewrite::{Rewrite, RewriteContext}; -use utils::{wrap_str, first_line_width, last_line_width, mk_sp}; +use utils::{wrap_str, first_line_width, last_line_width, mk_sp, last_line_extendable}; use expr::rewrite_call; use config::IndentStyle; use macros::convert_try_mac; @@ -322,12 +322,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) - } fn is_extendable_parent(context: &RewriteContext, parent_str: &str) -> bool { - context.config.chain_indent() == IndentStyle::Block && - parent_str.lines().last().map_or(false, |s| { - s.trim() - .chars() - .all(|c| c == ')' || c == ']' || c == '}' || c == '?') - }) + context.config.chain_indent() == IndentStyle::Block && last_line_extendable(parent_str) } // True if the chain is only `?`s. diff --git a/src/expr.rs b/src/expr.rs index b69e8bbd42ea..c299ffadad5b 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -21,7 +21,7 @@ use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTacti use string::{StringFormat, rewrite_string}; use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_width, semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr, - colon_spaces, contains_skip, mk_sp}; + colon_spaces, contains_skip, mk_sp, last_line_extendable}; use visitor::FmtVisitor; use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle, Style}; use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed}; @@ -1145,7 +1145,8 @@ impl<'a> ControlFlow<'a> { }; let force_newline_brace = context.config.control_style() == Style::Rfc && - pat_expr_string.contains('\n'); + pat_expr_string.contains('\n') && + !last_line_extendable(&pat_expr_string); // Try to format if-else on single line. if self.allow_single_line && context.config.single_line_if_else_max_width() > 0 { diff --git a/src/utils.rs b/src/utils.rs index 9c3d023c2ed8..1be83d01baba 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -108,6 +108,15 @@ pub fn trimmed_last_line_width(s: &str) -> usize { } } +#[inline] +pub fn last_line_extendable(s: &str) -> bool { + s.lines().last().map_or(false, |s| { + s.trim() + .chars() + .all(|c| c == ')' || c == ']' || c == '}' || c == '?') + }) +} + #[inline] fn is_skip(meta_item: &MetaItem) -> bool { match meta_item.node {