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.
This commit is contained in:
topecongiro 2017-06-17 21:11:55 +09:00
parent be18e7af90
commit 3dcd3d7fb0
3 changed files with 14 additions and 9 deletions

View file

@ -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.

View file

@ -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 {

View file

@ -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 {