Refactor closure formatting routine

This commit is contained in:
Marcus Klaas 2015-08-20 23:05:41 +02:00
parent a75017e50e
commit c8fd23ca68
4 changed files with 34 additions and 25 deletions

View File

@ -160,7 +160,8 @@ fn rewrite_closure(capture: ast::CaptureClause,
let prefix = format!("{}|{}|", mover, write_list(&arg_items.collect::<Vec<_>>(), &fmt));
let block_indent = closure_block_indent(context, offset);
let body_rewrite = if body.stmts.is_empty() {
// Try to format closure body as a single line expression without braces.
if body.stmts.is_empty() {
let expr = body.expr.as_ref().unwrap();
// All closure bodies are blocks in the eyes of the AST, but we may not
// want to unwrap them when they only contain a single expression.
@ -179,29 +180,16 @@ fn rewrite_closure(capture: ast::CaptureClause,
let accept_rewrite = rewrite.as_ref().map(|result| !result.contains('\n')).unwrap_or(false);
if accept_rewrite {
rewrite
} else {
if let ast::Expr_::ExprBlock(ref inner_body) = expr.node {
// Closure body is a proper block, with braces and all.
let inner_context = &RewriteContext { block_indent: block_indent, ..*context };
inner_body.rewrite(inner_context, 0, 0)
} else {
// Closure body is an expression, but not a block. It does not
// fit a single line, so we format it as it were wrapped in a
// block.
let inner_context = &RewriteContext {
block_indent: block_indent + context.config.tab_spaces,
..*context
};
let indent = offset + context.config.tab_spaces;
expr.rewrite(inner_context, context.config.max_width - indent, indent)
.map(|result| {
format!("{{\n{}{}\n{}}}", make_indent(indent), result, make_indent(offset))
})
}
return Some(format!("{} {}", prefix, rewrite.unwrap()));
}
}
// We couldn't format the closure body as a single line expression; fall
// back to block formatting.
let inner_context = &RewriteContext { block_indent: block_indent, ..*context };
let body_rewrite = if let ast::Expr_::ExprBlock(ref inner) = body.expr.as_ref().unwrap().node {
inner.rewrite(inner_context, 0, 0)
} else {
let inner_context = &RewriteContext { block_indent: block_indent, ..*context };
body.rewrite(inner_context, 0, 0)
};

View File

@ -68,9 +68,18 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
self.codemap.lookup_char_pos(b.span.lo),
self.codemap.lookup_char_pos(b.span.hi));
self.buffer.push_str("{");
self.last_pos = self.last_pos + BytePos(1);
// Check if this block has braces.
let snippet = self.snippet(b.span);
let has_braces = snippet.chars().next().unwrap() == '{' || &snippet[..6] == "unsafe";
let brace_compensation = if has_braces {
BytePos(1)
} else {
BytePos(0)
};
self.last_pos = self.last_pos + brace_compensation;
self.block_indent += self.config.tab_spaces;
self.buffer.push_str("{");
for stmt in &b.stmts {
self.visit_stmt(&stmt)
@ -86,7 +95,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
self.block_indent -= self.config.tab_spaces;
// TODO we should compress any newlines here to just one
self.format_missing_with_indent(b.span.hi - BytePos(1));
self.format_missing_with_indent(b.span.hi - brace_compensation);
self.buffer.push_str("}");
self.last_pos = b.span.hi;
}

View File

@ -30,6 +30,10 @@ fn main() {
let test = | | { do_something(); do_something_else(); };
let arg_test = |big_argument_name, test123| looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame();
let arg_test = |big_argument_name, test123| {looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame()};
|arg1, arg2, _, _, arg3, arg4| { let temp = arg4 + arg3;
arg2 * arg1 - temp }
}

View File

@ -51,6 +51,14 @@ fn main() {
do_something_else();
};
let arg_test = |big_argument_name, test123| {
looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame()
};
let arg_test = |big_argument_name, test123| {
looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame()
};
|arg1, arg2, _, _, arg3, arg4| {
let temp = arg4 + arg3;
arg2 * arg1 - temp