Fix match arm indentation bug
This commit is contained in:
parent
078fff068a
commit
2d4a0cbe3b
3 changed files with 35 additions and 25 deletions
39
src/expr.rs
39
src/expr.rs
|
@ -155,11 +155,14 @@ impl Rewrite for ast::Expr {
|
||||||
rewrite_chain(self, context, width, offset)
|
rewrite_chain(self, context, width, offset)
|
||||||
}
|
}
|
||||||
ast::Expr_::ExprMac(ref mac) => {
|
ast::Expr_::ExprMac(ref mac) => {
|
||||||
// Failure to rewrite a marco should not imply failure to rewrite the Expr
|
// Failure to rewrite a marco should not imply failure to
|
||||||
rewrite_macro(mac, context, width, offset).or(wrap_str(context.snippet(self.span),
|
// rewrite the expression.
|
||||||
context.config.max_width,
|
rewrite_macro(mac, context, width, offset).or_else(|| {
|
||||||
width,
|
wrap_str(context.snippet(self.span),
|
||||||
offset))
|
context.config.max_width,
|
||||||
|
width,
|
||||||
|
offset)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
ast::Expr_::ExprRet(None) => {
|
ast::Expr_::ExprRet(None) => {
|
||||||
wrap_str("return".to_owned(),
|
wrap_str("return".to_owned(),
|
||||||
|
@ -168,10 +171,10 @@ impl Rewrite for ast::Expr {
|
||||||
offset)
|
offset)
|
||||||
}
|
}
|
||||||
ast::Expr_::ExprRet(Some(ref expr)) => {
|
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) => {
|
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) => {
|
ast::Expr_::ExprAddrOf(mutability, ref expr) => {
|
||||||
rewrite_expr_addrof(context, mutability, &expr, width, offset)
|
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);
|
let pats_str = format!("{}{}", pats_str, guard_str);
|
||||||
// Where the next text can start.
|
// Where the next text can start.
|
||||||
let mut line_start = last_line_width(&pats_str);
|
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();
|
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 {
|
let comma = if let ast::ExprBlock(_) = body.node {
|
||||||
""
|
""
|
||||||
} else {
|
} else {
|
||||||
|
@ -891,8 +889,9 @@ impl Rewrite for ast::Arm {
|
||||||
// 4 = ` => `.len()
|
// 4 = ` => `.len()
|
||||||
let same_line_body = if context.config.max_width > line_start + comma.len() + 4 {
|
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 budget = context.config.max_width - line_start - comma.len() - 4;
|
||||||
let rewrite = nop_block_collapse(body.rewrite(context, budget, line_indent + 4),
|
let offset = Indent::new(offset.block_indent,
|
||||||
budget);
|
line_start + 4 - offset.block_indent);
|
||||||
|
let rewrite = nop_block_collapse(body.rewrite(context, budget, offset), budget);
|
||||||
|
|
||||||
match rewrite {
|
match rewrite {
|
||||||
Some(ref body_str) if body_str.len() <= budget || comma.is_empty() =>
|
Some(ref body_str) if body_str.len() <= budget || comma.is_empty() =>
|
||||||
|
@ -907,7 +906,6 @@ impl Rewrite for ast::Arm {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
// We have to push the body to the next line.
|
|
||||||
if let ast::ExprBlock(_) = body.node {
|
if let ast::ExprBlock(_) = body.node {
|
||||||
// We're trying to fit a block in, but it still failed, give up.
|
// We're trying to fit a block in, but it still failed, give up.
|
||||||
return None;
|
return None;
|
||||||
|
@ -926,12 +924,17 @@ impl Rewrite for ast::Arm {
|
||||||
.map(|x| &x[..])));
|
.map(|x| &x[..])));
|
||||||
|
|
||||||
let spacer = if break_line {
|
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 {
|
} else {
|
||||||
" ".to_owned()
|
" ".to_owned()
|
||||||
};
|
};
|
||||||
|
|
||||||
Some(format!("{}{} =>{}{},", attr_str.trim_left(), pats_str, spacer, body_str))
|
Some(format!("{}{} =>{}{},",
|
||||||
|
attr_str.trim_left(),
|
||||||
|
pats_str,
|
||||||
|
spacer,
|
||||||
|
body_str))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -85,9 +85,9 @@ const SKIP_ANNOTATION: &'static str = "rustfmt_skip";
|
||||||
pub struct Indent {
|
pub struct Indent {
|
||||||
// Width of the block indent, in characters. Must be a multiple of
|
// Width of the block indent, in characters. Must be a multiple of
|
||||||
// Config::tab_spaces.
|
// Config::tab_spaces.
|
||||||
block_indent: usize,
|
pub block_indent: usize,
|
||||||
// Alignment in characters.
|
// Alignment in characters.
|
||||||
alignment: usize,
|
pub alignment: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Indent {
|
impl Indent {
|
||||||
|
|
|
@ -65,12 +65,19 @@ fn main() {
|
||||||
// Test that one-line bodies align.
|
// Test that one-line bodies align.
|
||||||
fn main() {
|
fn main() {
|
||||||
match r {
|
match r {
|
||||||
Variableeeeeeeeeeeeeeeeee =>
|
Variableeeeeeeeeeeeeeeeee => ("variable",
|
||||||
("variable", vec!("id", "name", "qualname", "value", "type", "scopeid"), true, true),
|
vec!("id", "name", "qualname", "value", "type", "scopeid"),
|
||||||
Enummmmmmmmmmmmmmmmmmmmm =>
|
true,
|
||||||
("enum", vec!("id", "qualname", "scopeid", "value"), true, true),
|
true),
|
||||||
|
Enummmmmmmmmmmmmmmmmmmmm => ("enum",
|
||||||
|
vec!("id", "qualname", "scopeid", "value"),
|
||||||
|
true,
|
||||||
|
true),
|
||||||
Variantttttttttttttttttttttttt =>
|
Variantttttttttttttttttttttttt =>
|
||||||
("variant", vec!("id", "name", "qualname", "type", "value", "scopeid"), true, true),
|
("variant",
|
||||||
|
vec!("id", "name", "qualname", "type", "value", "scopeid"),
|
||||||
|
true,
|
||||||
|
true),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue