use lists::{definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting,
struct_lit_shape, struct_lit_tactic, write_list, DefinitiveListTactic, ListFormatting,
ListItem, ListTactic, Separator, SeparatorPlace, SeparatorTactic};
-use macros::{rewrite_macro, MacroPosition};
+use macros::{rewrite_macro, MacroArg, MacroPosition};
use patterns::{can_be_overflowed_pat, TuplePatField};
use rewrite::{Rewrite, RewriteContext};
use string::{rewrite_string, StringFormat};
Some(format!(
"{}{}",
"do catch ",
- try_opt!(block.rewrite(&context, Shape::legacy(budget, shape.indent)))
+ try_opt!(block.rewrite(context, Shape::legacy(budget, shape.indent)))
))
}
}
false
};
if no_return_type && !needs_block {
- // lock.stmts.len() == 1
+ // block.stmts.len() == 1
if let Some(expr) = stmt_expr(&block.stmts[0]) {
if let Some(rw) = rewrite_closure_expr(expr, &prefix, context, body_shape) {
return Some(rw);
}
}
- if !needs_block {
- // We need braces, but we might still prefer a one-liner.
- let stmt = &block.stmts[0];
- // 4 = braces and spaces.
- if let Some(body_shape) = body_shape.sub_width(4) {
- // Checks if rewrite succeeded and fits on a single line.
- if let Some(rewrite) = and_one_line(stmt.rewrite(context, body_shape)) {
- return Some(format!("{} {{ {} }}", prefix, rewrite));
- }
- }
- }
-
// Either we require a block, or tried without and failed.
rewrite_closure_block(block, &prefix, context, body_shape)
} else {
}
let prefix = try_opt!(block_prefix(context, self, shape));
- if let rw @ Some(_) = rewrite_single_line_block(context, &prefix, self, shape) {
- return rw;
+
+ let result = rewrite_block_with_visitor(context, &prefix, self, shape);
+ if let Some(ref result_str) = result {
+ if result_str.lines().count() <= 3 {
+ if let rw @ Some(_) = rewrite_single_line_block(context, &prefix, self, shape) {
+ return rw;
+ }
+ }
}
- rewrite_block_with_visitor(context, &prefix, self, shape)
+ result
}
}
""
};
- let expr_type = match self.node {
- ast::StmtKind::Expr(_) => ExprType::SubExpression,
- ast::StmtKind::Semi(_) => ExprType::Statement,
- _ => unreachable!(),
- };
let shape = try_opt!(shape.sub_width(suffix.len()));
- format_expr(ex, expr_type, context, shape).map(|s| s + suffix)
+ format_expr(ex, ExprType::Statement, context, shape).map(|s| s + suffix)
}
ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None,
};
false
}
}
+
+impl<'a> ToExpr for MacroArg {
+ fn to_expr(&self) -> Option<&ast::Expr> {
+ match *self {
+ MacroArg::Expr(ref expr) => Some(expr),
+ _ => None,
+ }
+ }
+
+ fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
+ match *self {
+ MacroArg::Expr(ref expr) => can_be_overflowed_expr(context, expr, len),
+ MacroArg::Ty(ref ty) => can_be_overflowed_type(context, ty, len),
+ MacroArg::Pat(..) => false,
+ }
+ }
+}