Skip to content

Commit be5c40c

Browse files
committed
Handle very long struct
1 parent 332cc97 commit be5c40c

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

src/items.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ impl<'a> FmtVisitor<'a> {
392392
generics: &ast::Generics,
393393
span: Span,
394394
) {
395-
self.buffer.push_str(&format_header("enum ", ident, vis));
395+
let enum_header = format_header("enum ", ident, vis);
396+
self.buffer.push_str(&enum_header);
396397

397398
let enum_snippet = self.snippet(span);
398399
let brace_pos = enum_snippet.find_uncommented("{").unwrap();
@@ -406,6 +407,7 @@ impl<'a> FmtVisitor<'a> {
406407
enum_def.variants.is_empty(),
407408
self.block_indent,
408409
mk_sp(span.lo, body_start),
410+
last_line_width(&enum_header),
409411
).unwrap();
410412
self.buffer.push_str(&generics_str);
411413

@@ -1071,19 +1073,38 @@ fn format_struct_struct(
10711073
fields.is_empty(),
10721074
offset,
10731075
mk_sp(span.lo, body_lo),
1076+
last_line_width(&result),
10741077
))
10751078
}
10761079
None => {
1077-
if context.config.item_brace_style() == BraceStyle::AlwaysNextLine &&
1078-
!fields.is_empty()
1080+
// 3 = ` {}`, 2 = ` {`.
1081+
let overhead = if fields.is_empty() { 3 } else { 2 };
1082+
if (context.config.item_brace_style() == BraceStyle::AlwaysNextLine &&
1083+
!fields.is_empty()) ||
1084+
context
1085+
.config
1086+
.max_width()
1087+
.checked_sub(result.len())
1088+
.unwrap_or(0) < overhead
10791089
{
10801090
format!("\n{}{{", offset.block_only().to_string(context.config))
10811091
} else {
10821092
" {".to_owned()
10831093
}
10841094
}
10851095
};
1086-
result.push_str(&generics_str);
1096+
// 1 = `}`
1097+
let overhead = if fields.is_empty() { 1 } else { 0 };
1098+
let max_len = context.config.max_width() - offset.width();
1099+
if !generics_str.contains('\n') && result.len() + generics_str.len() + overhead > max_len {
1100+
result.push('\n');
1101+
result.push_str(&offset
1102+
.block_indent(context.config)
1103+
.to_string(context.config));
1104+
result.push_str(&generics_str.trim_left());
1105+
} else {
1106+
result.push_str(&generics_str);
1107+
}
10871108

10881109
if fields.is_empty() {
10891110
let snippet = context.snippet(mk_sp(body_lo, span.hi - BytePos(1)));
@@ -1147,16 +1168,13 @@ fn format_tuple_struct(
11471168

11481169
let where_clause_str = match generics {
11491170
Some(generics) => {
1150-
let shape = Shape::indented(offset + last_line_width(&header_str), context.config);
1171+
let budget = context.budget(last_line_width(&header_str));
1172+
let shape = Shape::legacy(budget, offset);
11511173
let g_span = mk_sp(span.lo, body_lo);
11521174
let generics_str = try_opt!(rewrite_generics(context, generics, shape, g_span));
11531175
result.push_str(&generics_str);
11541176

1155-
let where_budget = context
1156-
.config
1157-
.max_width()
1158-
.checked_sub(last_line_width(&result))
1159-
.unwrap_or(0);
1177+
let where_budget = context.budget(last_line_width(&result));
11601178
try_opt!(rewrite_where_clause(
11611179
context,
11621180
&generics.where_clause,
@@ -1173,6 +1191,18 @@ fn format_tuple_struct(
11731191
};
11741192

11751193
if fields.is_empty() {
1194+
// 3 = `();`
1195+
let used_width = if result.contains('\n') {
1196+
last_line_width(&result) + 3
1197+
} else {
1198+
offset.width() + result.len() + 3
1199+
};
1200+
if used_width > context.config.max_width() {
1201+
result.push('\n');
1202+
result.push_str(&offset
1203+
.block_indent(context.config)
1204+
.to_string(context.config))
1205+
}
11761206
result.push('(');
11771207
let snippet = context.snippet(mk_sp(body_lo, context.codemap.span_before(span, ")")));
11781208
if snippet.is_empty() {

0 commit comments

Comments
 (0)