Put the opening brace of impl on the next line

We put the opening brace on the next line if the following conditions hold:

1. the result before '{' ends with comments or contains newline
2. the last line of the result before '{' is not extendable (i.e. consists of
   '>' and whitespaces).
This commit is contained in:
topecongiro 2018-01-02 13:52:19 +09:00
parent c355f3854c
commit 19d6a3c786
3 changed files with 9 additions and 8 deletions

View file

@ -33,8 +33,8 @@ use types::join_bounds;
use utils::{colon_spaces, contains_skip, first_line_width, format_abi, format_constness, use utils::{colon_spaces, contains_skip, first_line_width, format_abi, format_constness,
format_defaultness, format_mutability, format_unsafety, format_visibility, format_defaultness, format_mutability, format_unsafety, format_visibility,
is_attributes_extendable, last_line_contains_single_line_comment, is_attributes_extendable, last_line_contains_single_line_comment,
last_line_used_width, last_line_width, mk_sp, semicolon_for_expr, starts_with_newline, last_line_extendable, last_line_used_width, last_line_width, mk_sp,
stmt_expr, trimmed_last_line_width}; semicolon_for_expr, starts_with_newline, stmt_expr, trimmed_last_line_width};
use vertical::rewrite_with_alignment; use vertical::rewrite_with_alignment;
use visitor::FmtVisitor; use visitor::FmtVisitor;
@ -639,8 +639,10 @@ pub fn format_impl(
} }
result.push_str(&where_clause_str); result.push_str(&where_clause_str);
let need_newline = !last_line_extendable(&result)
&& (last_line_contains_single_line_comment(&result) || result.contains('\n'));
match context.config.brace_style() { match context.config.brace_style() {
_ if last_line_contains_single_line_comment(&result) => result.push_str(&sep), _ if need_newline => result.push_str(&sep),
BraceStyle::AlwaysNextLine => result.push_str(&sep), BraceStyle::AlwaysNextLine => result.push_str(&sep),
BraceStyle::PreferSameLine => result.push(' '), BraceStyle::PreferSameLine => result.push(' '),
BraceStyle::SameLineWhere => { BraceStyle::SameLineWhere => {

View file

@ -178,7 +178,7 @@ pub fn last_line_extendable(s: &str) -> bool {
} }
for c in s.chars().rev() { for c in s.chars().rev() {
match c { match c {
')' | ']' | '}' | '?' => continue, ')' | ']' | '}' | '?' | '>' => continue,
'\n' => break, '\n' => break,
_ if c.is_whitespace() => continue, _ if c.is_whitespace() => continue,
_ => return false, _ => return false,

View file

@ -103,14 +103,13 @@ trait Foo {
// #2331 // #2331
trait MyTrait< trait MyTrait<
AAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAA,
BBBBBBBBBBBBBBBBBBBB, BBBBBBBBBBBBBBBBBBBB,
CCCCCCCCCCCCCCCCCCCC, CCCCCCCCCCCCCCCCCCCC,
DDDDDDDDDDDDDDDDDDDD, DDDDDDDDDDDDDDDDDDDD,
> { > {
fn foo() {} fn foo() {}
} }
// Trait aliases // Trait aliases
trait FooBar = Foo + Bar; trait FooBar = Foo + Bar;
trait FooBar<A, B, C> = Foo + Bar; trait FooBar<A, B, C> = Foo + Bar;