Modify the placement of the opening brace of trait
Put the opening brace on the next line if
1. putting it one the current line exceeds max width.
2. trait bounds uses multiple lines.
@@ -934,8 +934,9 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
)?;
}
- let where_density =
- if context.config.indent_style() == IndentStyle::Block && result.is_empty() {
+ // Rewrite where clause.
+ if !generics.where_clause.predicates.is_empty() {
+ let where_density = if context.config.indent_style() == IndentStyle::Block {
Density::Compressed
} else {
Density::Tall
@@ -971,8 +972,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
result.push_str(&where_indent.to_string_with_newline(context.config));
}
result.push_str(&where_clause_str);
-
- if generics.where_clause.predicates.is_empty() {
+ } else {
let item_snippet = context.snippet(item.span);
if let Some(lo) = item_snippet.chars().position(|c| c == '/') {
// 1 = `{`
@@ -995,7 +995,9 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
}
match context.config.brace_style() {
- _ if last_line_contains_single_line_comment(&result) => {
+ _ if last_line_contains_single_line_comment(&result)
+ || last_line_width(&result) + 2 > context.budget(offset.width()) =>
+ {
result.push_str(&offset.to_string_with_newline(context.config));
}
BraceStyle::AlwaysNextLine => {
@@ -1003,8 +1005,8 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
}
BraceStyle::PreferSameLine => result.push(' '),
BraceStyle::SameLineWhere => {
- if !where_clause_str.is_empty()
- && (!trait_items.is_empty() || result.contains('\n'))
+ if result.contains('\n')
+ || (!generics.where_clause.predicates.is_empty() && !trait_items.is_empty())
{
result.push_str(&offset.to_string_with_newline(context.config));
} else {
