Consider max_width when rewriting struct in single-line
This commit is contained in:
parent
cf195fb326
commit
db5f77b7d0
13
src/items.rs
13
src/items.rs
@ -10,6 +10,8 @@
|
|||||||
|
|
||||||
// Formatting top-level items - functions, structs, enums, traits, impls.
|
// Formatting top-level items - functions, structs, enums, traits, impls.
|
||||||
|
|
||||||
|
use std::cmp::min;
|
||||||
|
|
||||||
use syntax::{abi, ast, ptr, symbol};
|
use syntax::{abi, ast, ptr, symbol};
|
||||||
use syntax::ast::ImplItem;
|
use syntax::ast::ImplItem;
|
||||||
use syntax::codemap::{BytePos, Span};
|
use syntax::codemap::{BytePos, Span};
|
||||||
@ -1120,15 +1122,22 @@ pub fn format_struct_struct(
|
|||||||
return Some(result);
|
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(
|
let items_str = try_opt!(rewrite_with_alignment(
|
||||||
fields,
|
fields,
|
||||||
context,
|
context,
|
||||||
Shape::indented(offset, context.config),
|
Shape::indented(offset, context.config),
|
||||||
mk_sp(body_lo, span.hi),
|
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))
|
Some(format!("{} {} }}", result, items_str))
|
||||||
} else {
|
} else {
|
||||||
Some(format!(
|
Some(format!(
|
||||||
|
11
tests/target/issue-1802.rs
Normal file
11
tests/target/issue-1802.rs
Normal 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,
|
||||||
|
},
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user