Skip to content

Fix up formatting bugs in impl and trait #2332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use types::join_bounds;
use utils::{colon_spaces, contains_skip, first_line_width, format_abi, format_constness,
format_defaultness, format_mutability, format_unsafety, format_visibility,
is_attributes_extendable, last_line_contains_single_line_comment,
last_line_used_width, last_line_width, mk_sp, semicolon_for_expr, starts_with_newline,
stmt_expr, trimmed_last_line_width};
last_line_extendable, last_line_used_width, last_line_width, mk_sp,
semicolon_for_expr, starts_with_newline, stmt_expr, trimmed_last_line_width};
use vertical::rewrite_with_alignment;
use visitor::FmtVisitor;

Expand Down Expand Up @@ -639,8 +639,10 @@ pub fn format_impl(
}
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() {
_ 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::PreferSameLine => result.push(' '),
BraceStyle::SameLineWhere => {
Expand Down Expand Up @@ -938,7 +940,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)

let body_lo = context.codemap.span_after(item.span, "{");

let shape = Shape::indented(offset, context.config);
let shape = Shape::indented(offset, context.config).offset_left(result.len())?;
let generics_str =
rewrite_generics(context, generics, shape, mk_sp(item.span.lo(), body_lo))?;
result.push_str(&generics_str);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub fn last_line_extendable(s: &str) -> bool {
}
for c in s.chars().rev() {
match c {
')' | ']' | '}' | '?' => continue,
')' | ']' | '}' | '?' | '>' => continue,
'\n' => break,
_ if c.is_whitespace() => continue,
_ => return false,
Expand Down
5 changes: 5 additions & 0 deletions tests/source/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ trait Foo {
type IteRev = <MergingUntypedTimeSeries<SliceSeries<SliceWindow>> as UntypedTimeSeries>::IterRev;
}

// #2331
trait MyTrait<AAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBBBBBBBBBB, CCCCCCCCCCCCCCCCCCCC, DDDDDDDDDDDDDDDDDDDD> {
fn foo() {}
}

// Trait aliases
trait FooBar =
Foo
Expand Down
9 changes: 6 additions & 3 deletions tests/target/big-impl-rfc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,22 @@ where

// #1369
impl<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName> Foo
for Bar {
for Bar
{
fn foo() {}
}
impl Foo<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName>
for Bar {
for Bar
{
fn foo() {}
}
impl<
ExcessivelyLongGenericName,
ExcessivelyLongGenericName,
AnotherExcessivelyLongGenericName,
> Foo<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName>
for Bar {
for Bar
{
fn foo() {}
}
impl<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName> Foo
Expand Down
9 changes: 6 additions & 3 deletions tests/target/big-impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,22 @@ where

// #1369
impl<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName> Foo
for Bar {
for Bar
{
fn foo() {}
}
impl Foo<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName>
for Bar {
for Bar
{
fn foo() {}
}
impl<
ExcessivelyLongGenericName,
ExcessivelyLongGenericName,
AnotherExcessivelyLongGenericName,
> Foo<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName>
for Bar {
for Bar
{
fn foo() {}
}
impl<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName> Foo
Expand Down
9 changes: 6 additions & 3 deletions tests/target/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,13 @@ mod m {
}

impl<BorrowType, K, V, NodeType, HandleType>
Handle<NodeRef<BorrowType, K, V, NodeType>, HandleType> {
Handle<NodeRef<BorrowType, K, V, NodeType>, HandleType>
{
}

impl<BorrowType, K, V, NodeType, HandleType> PartialEq
for Handle<NodeRef<BorrowType, K, V, NodeType>, HandleType> {
for Handle<NodeRef<BorrowType, K, V, NodeType>, HandleType>
{
}

mod x {
Expand All @@ -160,7 +162,8 @@ mod x {
}

impl<ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNodeFoo>
Issue1249<ConcreteThreadSafeLayoutNode> {
Issue1249<ConcreteThreadSafeLayoutNode>
{
// Creates a new flow constructor.
fn foo() {}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/target/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ trait Foo {
<MergingUntypedTimeSeries<SliceSeries<SliceWindow>> as UntypedTimeSeries>::IterRev;
}

// #2331
trait MyTrait<
AAAAAAAAAAAAAAAAAAAA,
BBBBBBBBBBBBBBBBBBBB,
CCCCCCCCCCCCCCCCCCCC,
DDDDDDDDDDDDDDDDDDDD,
> {
fn foo() {}
}

// Trait aliases
trait FooBar = Foo + Bar;
trait FooBar<A, B, C> = Foo + Bar;
Expand Down