diff options
| author | Marcus Klaas <mail@marcusklaas.nl> | 2015-09-26 23:16:11 +0200 |
|---|---|---|
| committer | Marcus Klaas <mail@marcusklaas.nl> | 2015-09-26 23:18:57 +0200 |
| commit | 2d4a0cbe3b7a4c8f94bb1d422993ff856d714dae (patch) | |
| tree | 9f251622d3e61c8e791208e9388454dcfb26dfa6 /src/expr.rs | |
| parent | 078fff068a3d0e5401bd46214c112cb9276a6240 (diff) | |
Fix match arm indentation bug
Diffstat (limited to 'src/expr.rs')
| -rw-r--r-- | src/expr.rs | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/src/expr.rs b/src/expr.rs index 29966d09da9..b8a52a02cc7 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -155,11 +155,14 @@ impl Rewrite for ast::Expr { rewrite_chain(self, context, width, offset) } ast::Expr_::ExprMac(ref mac) => { - // Failure to rewrite a marco should not imply failure to rewrite the Expr - rewrite_macro(mac, context, width, offset).or(wrap_str(context.snippet(self.span), - context.config.max_width, - width, - offset)) + // Failure to rewrite a marco should not imply failure to + // rewrite the expression. + rewrite_macro(mac, context, width, offset).or_else(|| { + wrap_str(context.snippet(self.span), + context.config.max_width, + width, + offset) + }) } ast::Expr_::ExprRet(None) => { wrap_str("return".to_owned(), @@ -168,10 +171,10 @@ impl Rewrite for ast::Expr { offset) } ast::Expr_::ExprRet(Some(ref expr)) => { - rewrite_unary_prefix(context, "return ", &expr, width, offset) + rewrite_unary_prefix(context, "return ", expr, width, offset) } ast::Expr_::ExprBox(ref expr) => { - rewrite_unary_prefix(context, "box ", &expr, width, offset) + rewrite_unary_prefix(context, "box ", expr, width, offset) } ast::Expr_::ExprAddrOf(mutability, ref expr) => { rewrite_expr_addrof(context, mutability, &expr, width, offset) @@ -872,15 +875,10 @@ impl Rewrite for ast::Arm { let pats_str = format!("{}{}", pats_str, guard_str); // Where the next text can start. let mut line_start = last_line_width(&pats_str); - if pats_str.find('\n').is_none() { + if !pats_str.contains('\n') { line_start += offset.width(); } - let mut line_indent = offset + pats_width; - if vertical { - line_indent = line_indent.block_indent(context.config); - } - let comma = if let ast::ExprBlock(_) = body.node { "" } else { @@ -891,8 +889,9 @@ impl Rewrite for ast::Arm { // 4 = ` => `.len() let same_line_body = if context.config.max_width > line_start + comma.len() + 4 { let budget = context.config.max_width - line_start - comma.len() - 4; - let rewrite = nop_block_collapse(body.rewrite(context, budget, line_indent + 4), - budget); + let offset = Indent::new(offset.block_indent, + line_start + 4 - offset.block_indent); + let rewrite = nop_block_collapse(body.rewrite(context, budget, offset), budget); match rewrite { Some(ref body_str) if body_str.len() <= budget || comma.is_empty() => @@ -907,7 +906,6 @@ impl Rewrite for ast::Arm { None }; - // We have to push the body to the next line. if let ast::ExprBlock(_) = body.node { // We're trying to fit a block in, but it still failed, give up. return None; @@ -926,12 +924,17 @@ impl Rewrite for ast::Arm { .map(|x| &x[..]))); let spacer = if break_line { - format!("\n{}", offset.block_indent(context.config).to_string(context.config)) + format!("\n{}", + offset.block_indent(context.config).to_string(context.config)) } else { " ".to_owned() }; - Some(format!("{}{} =>{}{},", attr_str.trim_left(), pats_str, spacer, body_str)) + Some(format!("{}{} =>{}{},", + attr_str.trim_left(), + pats_str, + spacer, + body_str)) } } |
