Handle empty tuple struct def properly
@@ -908,7 +908,6 @@ fn format_struct_struct(context: &RewriteContext,
};
result.push_str(&generics_str);
- // FIXME(#919): properly format empty structs and their comments.
if fields.is_empty() {
let snippet = context.snippet(mk_sp(body_lo, span.hi - BytePos(1)));
if snippet.trim().is_empty() {
@@ -995,9 +994,8 @@ fn format_tuple_struct(context: &RewriteContext,
let header_str = format_header(item_name, ident, vis);
result.push_str(&header_str);
- // FIXME(#919): don't lose comments on empty tuple structs.
let body_lo = if fields.is_empty() {
- span.hi
+ context.codemap.span_after(span, "(")
} else {
fields[0].span.lo
};
@@ -1027,6 +1025,22 @@ fn format_tuple_struct(context: &RewriteContext,
None => "".to_owned(),
};
+ if fields.is_empty() {
+ result.push('(');
+ let snippet = context.snippet(mk_sp(body_lo, context.codemap.span_before(span, ")")));
+ if snippet.is_empty() {
+ //
+ } else if snippet
+ .trim_right_matches(&[' ', '\t'][..])
+ .ends_with('\n') {
+ result.push_str(&snippet.trim_right());
+ result.push('\n');
+ result.push_str(&offset.to_string(context.config));
+ } else {
+ result.push_str(&snippet);
+ }
+ result.push(')');
+ } else {
let (tactic, item_indent) = match context.config.fn_args_layout {
IndentStyle::Visual => {
// 1 = `(`
@@ -1041,9 +1055,9 @@ fn format_tuple_struct(context: &RewriteContext,
.config
.max_width
.checked_sub(item_indent.width() + 3));
- let shape = Shape::legacy(item_budget, item_indent);
- let items = itemize_list(context.codemap,
+ let items =
+ itemize_list(context.codemap,
fields.iter(),
")",
|field| {
@@ -1055,10 +1069,19 @@ fn format_tuple_struct(context: &RewriteContext,
}
},
|field| field.ty.span.hi,
- |field| field.rewrite(context, shape),
+ |field| field.rewrite(context, Shape::legacy(item_budget, item_indent)),
context.codemap.span_after(span, "("),
span.hi);
- let body = try_opt!(list_helper(items, shape, context.config, tactic));
+ let body_budget = try_opt!(context
+ .config
+ .max_width
+ .checked_sub(offset.block_only().width() + result.len() +
+ 3));
+ let body = try_opt!(list_helper(items,
+ // TODO budget is wrong in block case
+ Shape::legacy(body_budget, item_indent),
+ context.config,
+ tactic));
if context.config.fn_args_layout == IndentStyle::Visual || !body.contains('\n') {
result.push('(');
@@ -1080,6 +1103,7 @@ fn format_tuple_struct(context: &RewriteContext,
result.push_str(&offset.block_only().to_string(&context.config));
result.push(')');
}
+ }
if !where_clause_str.is_empty() && !where_clause_str.contains('\n') &&
(result.contains('\n') ||
