Fix indentation in multi lined pattern

This commit is contained in:
Seiichi Uchida 2017-12-10 23:54:34 +09:00
parent a7060f9fdf
commit b29a3afb96

View file

@ -937,21 +937,16 @@ impl<'a> ControlFlow<'a> {
let offset = self.keyword.len() + label_string.len() + 1;
let pat_expr_string = match self.cond {
Some(cond) => {
let cond_shape = match context.config.indent_style() {
IndentStyle::Visual => constr_shape.shrink_left(offset)?,
IndentStyle::Block => constr_shape.offset_left(offset)?,
};
rewrite_pat_expr(
context,
self.pat,
cond,
self.matcher,
self.connector,
self.keyword,
cond_shape,
)?
}
Some(cond) => rewrite_pat_expr(
context,
self.pat,
cond,
self.matcher,
self.connector,
self.keyword,
constr_shape,
offset,
)?,
None => String::new(),
};
@ -967,8 +962,8 @@ impl<'a> ControlFlow<'a> {
.max_width()
.checked_sub(constr_shape.used_width() + offset + brace_overhead)
.unwrap_or(0);
let force_newline_brace = context.config.indent_style() == IndentStyle::Block
&& (pat_expr_string.contains('\n') || pat_expr_string.len() > one_line_budget)
let force_newline_brace = (pat_expr_string.contains('\n')
|| pat_expr_string.len() > one_line_budget)
&& !last_line_extendable(&pat_expr_string);
// Try to format if-else on single line.
@ -1704,23 +1699,25 @@ fn rewrite_pat_expr(
connector: &str,
keyword: &str,
shape: Shape,
offset: usize,
) -> Option<String> {
debug!("rewrite_pat_expr {:?} {:?} {:?}", shape, pat, expr);
let cond_shape = shape.offset_left(offset)?;
if let Some(pat) = pat {
let matcher = if matcher.is_empty() {
matcher.to_owned()
} else {
format!("{} ", matcher)
};
let pat_shape = shape
let pat_shape = cond_shape
.offset_left(matcher.len())?
.sub_width(connector.len())?;
let pat_string = pat.rewrite(context, pat_shape)?;
let result = format!("{}{}{}", matcher, pat_string, connector);
return rewrite_assign_rhs(context, result, expr, shape);
return rewrite_assign_rhs(context, result, expr, cond_shape);
}
let expr_rw = expr.rewrite(context, shape);
let expr_rw = expr.rewrite(context, cond_shape);
// The expression may (partially) fit on the current line.
// We do not allow splitting between `if` and condition.
if keyword == "if" || expr_rw.is_some() {