Merge pull request #1804 from topecongiro/issue-1802

Consider max_width when rewriting struct in single-line
This commit is contained in:
Nick Cameron 2017-07-21 11:31:43 +12:00 committed by GitHub
commit 0786efcab2
2 changed files with 22 additions and 2 deletions

View file

@ -10,6 +10,8 @@
// Formatting top-level items - functions, structs, enums, traits, impls.
use std::cmp::min;
use syntax::{abi, ast, ptr, symbol};
use syntax::ast::ImplItem;
use syntax::codemap::{BytePos, Span};
@ -1120,15 +1122,22 @@ pub fn format_struct_struct(
return Some(result);
}
// 3 = ` ` and ` }`
let one_line_budget = context
.config
.max_width()
.checked_sub(result.len() + 3 + offset.width())
.unwrap_or(0);
let items_str = try_opt!(rewrite_with_alignment(
fields,
context,
Shape::indented(offset, context.config),
mk_sp(body_lo, span.hi),
one_line_width.unwrap_or(0),
one_line_width.map_or(0, |one_line_width| min(one_line_width, one_line_budget)),
));
if one_line_width.is_some() && !items_str.contains('\n') {
if one_line_width.is_some() && !items_str.contains('\n') && !result.contains('\n') {
Some(format!("{} {} }}", result, items_str))
} else {
Some(format!(

View file

@ -0,0 +1,11 @@
// rustfmt-tab_spaces: 2
// rustfmt-max_width: 10
// rustfmt-struct_variant_width: 10
// rustfmt-error_on_line_overflow: false
enum F {
X {
a: d,
b: e,
},
}