mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-11 16:15:03 +00:00
Put braces on the next line if it exceeds max width
This commit is contained in:
parent
cb351440e9
commit
65ad7d3bb0
58
src/items.rs
58
src/items.rs
@ -1095,12 +1095,14 @@ pub fn format_struct_struct(
|
|||||||
};
|
};
|
||||||
// 1 = `}`
|
// 1 = `}`
|
||||||
let overhead = if fields.is_empty() { 1 } else { 0 };
|
let overhead = if fields.is_empty() { 1 } else { 0 };
|
||||||
let max_len = context.config.max_width() - offset.width();
|
let max_len = context
|
||||||
|
.config
|
||||||
|
.max_width()
|
||||||
|
.checked_sub(offset.width())
|
||||||
|
.unwrap_or(0);
|
||||||
if !generics_str.contains('\n') && result.len() + generics_str.len() + overhead > max_len {
|
if !generics_str.contains('\n') && result.len() + generics_str.len() + overhead > max_len {
|
||||||
result.push('\n');
|
result.push('\n');
|
||||||
result.push_str(&offset
|
result.push_str(&offset.to_string(context.config));
|
||||||
.block_indent(context.config)
|
|
||||||
.to_string(context.config));
|
|
||||||
result.push_str(&generics_str.trim_left());
|
result.push_str(&generics_str.trim_left());
|
||||||
} else {
|
} else {
|
||||||
result.push_str(&generics_str);
|
result.push_str(&generics_str);
|
||||||
@ -2730,13 +2732,14 @@ fn format_generics(
|
|||||||
let shape = Shape::legacy(context.budget(used_width + offset.width()), offset);
|
let shape = Shape::legacy(context.budget(used_width + offset.width()), offset);
|
||||||
let mut result = try_opt!(rewrite_generics(context, generics, shape, span));
|
let mut result = try_opt!(rewrite_generics(context, generics, shape, span));
|
||||||
|
|
||||||
if !generics.where_clause.predicates.is_empty() || result.contains('\n') {
|
let same_line_brace = if !generics.where_clause.predicates.is_empty() ||
|
||||||
let budget = try_opt!(
|
result.contains('\n')
|
||||||
context
|
{
|
||||||
|
let budget = context
|
||||||
.config
|
.config
|
||||||
.max_width()
|
.max_width()
|
||||||
.checked_sub(last_line_width(&result))
|
.checked_sub(last_line_width(&result))
|
||||||
);
|
.unwrap_or(0);
|
||||||
let where_clause_str = try_opt!(rewrite_where_clause(
|
let where_clause_str = try_opt!(rewrite_where_clause(
|
||||||
context,
|
context,
|
||||||
&generics.where_clause,
|
&generics.where_clause,
|
||||||
@ -2751,29 +2754,38 @@ fn format_generics(
|
|||||||
generics.span.hi,
|
generics.span.hi,
|
||||||
));
|
));
|
||||||
result.push_str(&where_clause_str);
|
result.push_str(&where_clause_str);
|
||||||
let same_line_brace = force_same_line_brace ||
|
force_same_line_brace || brace_style == BraceStyle::PreferSameLine ||
|
||||||
(generics.where_clause.predicates.is_empty() && trimmed_last_line_width(&result) == 1);
|
(generics.where_clause.predicates.is_empty() && trimmed_last_line_width(&result) == 1)
|
||||||
if !same_line_brace &&
|
|
||||||
(brace_style == BraceStyle::SameLineWhere ||
|
|
||||||
brace_style == BraceStyle::AlwaysNextLine)
|
|
||||||
{
|
|
||||||
result.push('\n');
|
|
||||||
result.push_str(&offset.block_only().to_string(context.config));
|
|
||||||
} else {
|
} else {
|
||||||
result.push(' ');
|
force_same_line_brace || trimmed_last_line_width(&result) == 1 ||
|
||||||
}
|
|
||||||
result.push_str(opener);
|
|
||||||
} else {
|
|
||||||
if force_same_line_brace || trimmed_last_line_width(&result) == 1 ||
|
|
||||||
brace_style != BraceStyle::AlwaysNextLine
|
brace_style != BraceStyle::AlwaysNextLine
|
||||||
{
|
};
|
||||||
|
let total_used_width = if result.contains('\n') {
|
||||||
|
last_line_width(&result)
|
||||||
|
} else {
|
||||||
|
used_width + result.len()
|
||||||
|
};
|
||||||
|
let remaining_budget = context
|
||||||
|
.config
|
||||||
|
.max_width()
|
||||||
|
.checked_sub(total_used_width)
|
||||||
|
.unwrap_or(0);
|
||||||
|
// If the same line brace if forced, it indicates that we are rewriting an item with empty body,
|
||||||
|
// and hence we take the closer into account as well for one line budget.
|
||||||
|
// We assume that the closer has the same length as the opener.
|
||||||
|
let overhead = if force_same_line_brace {
|
||||||
|
1 + opener.len() + opener.len()
|
||||||
|
} else {
|
||||||
|
1 + opener.len()
|
||||||
|
};
|
||||||
|
let forbid_same_line_brace = overhead > remaining_budget;
|
||||||
|
if !forbid_same_line_brace && same_line_brace {
|
||||||
result.push(' ');
|
result.push(' ');
|
||||||
} else {
|
} else {
|
||||||
result.push('\n');
|
result.push('\n');
|
||||||
result.push_str(&offset.block_only().to_string(context.config));
|
result.push_str(&offset.block_only().to_string(context.config));
|
||||||
}
|
}
|
||||||
result.push_str(opener);
|
result.push_str(opener);
|
||||||
}
|
|
||||||
|
|
||||||
Some(result)
|
Some(result)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user