SubExpression,
}
+fn combine_attr_and_expr(
+ context: &RewriteContext,
+ shape: Shape,
+ attr_str: &str,
+ expr_str: &str,
+) -> String {
+ let separator = if attr_str.is_empty() {
+ String::new()
+ } else {
+ if expr_str.contains('\n') || attr_str.contains('\n') ||
+ attr_str.len() + expr_str.len() > shape.width
+ {
+ format!("\n{}", shape.indent.to_string(context.config))
+ } else {
+ String::from(" ")
+ }
+ };
+ format!("{}{}{}", attr_str, separator, expr_str)
+}
+
fn format_expr(
expr: &ast::Expr,
expr_type: ExprType,
context: &RewriteContext,
shape: Shape,
) -> Option<String> {
+ let attr_rw = (&*expr.attrs).rewrite(context, shape);
if contains_skip(&*expr.attrs) {
- return Some(context.snippet(expr.span));
+ if let Some(attr_str) = attr_rw {
+ return Some(combine_attr_and_expr(
+ context,
+ shape,
+ &attr_str,
+ &context.snippet(expr.span),
+ ));
+ } else {
+ return Some(context.snippet(expr.span));
+ }
}
- let attr_rw = (&*expr.attrs).rewrite(context, shape);
let expr_rw = match expr.node {
ast::ExprKind::Array(ref expr_vec) => {
rewrite_array(
};
match (attr_rw, expr_rw) {
(Some(attr_str), Some(expr_str)) => {
- let space = if attr_str.is_empty() { "" } else { " " };
recover_comment_removed(
- format!("{}{}{}", attr_str, space, expr_str),
+ combine_attr_and_expr(context, shape, &attr_str, &expr_str),
expr.span,
context,
shape,
ast::StmtKind::Item(ref item) => {
self.visit_item(item);
}
- ast::StmtKind::Local(..) |
- ast::StmtKind::Expr(..) |
- ast::StmtKind::Semi(..) => {
+ ast::StmtKind::Local(..) => {
let rewrite = stmt.rewrite(
&self.get_context(),
Shape::indented(self.block_indent, self.config),
);
self.push_rewrite(stmt.span, rewrite);
}
+ ast::StmtKind::Expr(ref expr) |
+ ast::StmtKind::Semi(ref expr) => {
+ let rewrite = stmt.rewrite(
+ &self.get_context(),
+ Shape::indented(self.block_indent, self.config),
+ );
+ let span = if expr.attrs.is_empty() {
+ stmt.span
+ } else {
+ mk_sp(expr.attrs[0].span.lo, stmt.span.hi)
+ };
+ self.push_rewrite(span, rewrite)
+ }
ast::StmtKind::Mac(ref mac) => {
let (ref mac, _macro_style, _) = **mac;
self.visit_mac(mac, None, MacroPosition::Statement);