Fix match arm indentation bug

This commit is contained in:
Marcus Klaas 2015-09-26 23:16:11 +02:00
parent 078fff068a
commit 2d4a0cbe3b
3 changed files with 35 additions and 25 deletions

View File

@ -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))
}
}

View File

@ -85,9 +85,9 @@ const SKIP_ANNOTATION: &'static str = "rustfmt_skip";
pub struct Indent {
// Width of the block indent, in characters. Must be a multiple of
// Config::tab_spaces.
block_indent: usize,
pub block_indent: usize,
// Alignment in characters.
alignment: usize,
pub alignment: usize,
}
impl Indent {

View File

@ -65,12 +65,19 @@ fn main() {
// Test that one-line bodies align.
fn main() {
match r {
Variableeeeeeeeeeeeeeeeee =>
("variable", vec!("id", "name", "qualname", "value", "type", "scopeid"), true, true),
Enummmmmmmmmmmmmmmmmmmmm =>
("enum", vec!("id", "qualname", "scopeid", "value"), true, true),
Variableeeeeeeeeeeeeeeeee => ("variable",
vec!("id", "name", "qualname", "value", "type", "scopeid"),
true,
true),
Enummmmmmmmmmmmmmmmmmmmm => ("enum",
vec!("id", "qualname", "scopeid", "value"),
true,
true),
Variantttttttttttttttttttttttt =>
("variant", vec!("id", "name", "qualname", "type", "value", "scopeid"), true, true),
("variant",
vec!("id", "name", "qualname", "type", "value", "scopeid"),
true,
true),
}
}