Skip to content

Split impl at 'for' if a line break is needed #1148

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 8 commits into from
Sep 6, 2016
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
127 changes: 80 additions & 47 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,58 +446,21 @@ impl<'a> FmtVisitor<'a> {
}

pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -> Option<String> {
if let ast::ItemKind::Impl(unsafety,
polarity,
ref generics,
ref trait_ref,
ref self_ty,
ref items) = item.node {
if let ast::ItemKind::Impl(_, _, ref generics, ref trait_ref, _, ref items) = item.node {
let mut result = String::new();

result.push_str(&*format_visibility(&item.vis));
result.push_str(format_unsafety(unsafety));
result.push_str("impl");

let lo = context.codemap.span_after(item.span, "impl");
let hi = match *trait_ref {
Some(ref tr) => tr.path.span.lo,
None => self_ty.span.lo,
};
let generics_str = try_opt!(rewrite_generics(context,
generics,
offset,
context.config.max_width,
offset + result.len(),
mk_sp(lo, hi)));
result.push_str(&generics_str);

// FIXME might need to linebreak in the impl header, here would be a
// good place.
result.push(' ');
if polarity == ast::ImplPolarity::Negative {
result.push_str("!");
}
if let Some(ref trait_ref) = *trait_ref {
let budget = try_opt!(context.config.max_width.checked_sub(result.len()));
let indent = offset + result.len();
result.push_str(&*try_opt!(trait_ref.rewrite(context, budget, indent)));
result.push_str(" for ");
}
// First try to format the ref and type without a split at the 'for'.
let mut ref_and_type = try_opt!(format_impl_ref_and_type(context, item, offset, false));

let mut used_space = result.len();
if generics.where_clause.predicates.is_empty() {
// If there is no where clause adapt budget for type formatting to take space and curly
// brace into account.
match context.config.item_brace_style {
BraceStyle::AlwaysNextLine => {}
BraceStyle::PreferSameLine => used_space += 2,
BraceStyle::SameLineWhere => used_space += 2,
// If there is a line break present in the first result format it again
// with a split at the 'for'. Skip this if there is no trait ref and
// therefore no 'for'.
if let Some(_) = *trait_ref {
if ref_and_type.contains('\n') {
ref_and_type = try_opt!(format_impl_ref_and_type(context, item, offset, true));
}
}

let budget = try_opt!(context.config.max_width.checked_sub(used_space));
let indent = offset + result.len();
result.push_str(&*try_opt!(self_ty.rewrite(context, budget, indent)));
result.push_str(&ref_and_type);

let where_budget = try_opt!(context.config.max_width.checked_sub(last_line_width(&result)));
let where_clause_str = try_opt!(rewrite_where_clause(context,
Expand Down Expand Up @@ -594,6 +557,76 @@ fn is_impl_single_line(context: &RewriteContext,
!contains_comment(&snippet[open_pos..]))
}

fn format_impl_ref_and_type(context: &RewriteContext,
item: &ast::Item,
offset: Indent,
split_at_for: bool)
-> Option<String> {
if let ast::ItemKind::Impl(unsafety, polarity, ref generics, ref trait_ref, ref self_ty, _) =
item.node {
let mut result = String::new();

result.push_str(&*format_visibility(&item.vis));
result.push_str(format_unsafety(unsafety));
result.push_str("impl");

let lo = context.codemap.span_after(item.span, "impl");
let hi = match *trait_ref {
Some(ref tr) => tr.path.span.lo,
None => self_ty.span.lo,
};
let generics_str = try_opt!(rewrite_generics(context,
generics,
offset,
context.config.max_width,
offset + result.len(),
mk_sp(lo, hi)));
result.push_str(&generics_str);

result.push(' ');
if polarity == ast::ImplPolarity::Negative {
result.push('!');
}
if let Some(ref trait_ref) = *trait_ref {
let budget = try_opt!(context.config.max_width.checked_sub(result.len()));
let indent = offset + result.len();
result.push_str(&*try_opt!(trait_ref.rewrite(context, budget, indent)));

if split_at_for {
result.push('\n');

// Add indentation of one additional tab.
let width = context.block_indent.width() + context.config.tab_spaces;
let for_indent = Indent::new(0, width);
result.push_str(&for_indent.to_string(context.config));

result.push_str("for ");
} else {
result.push_str(" for ");
}
}

let mut used_space = last_line_width(&result);
if generics.where_clause.predicates.is_empty() {
// If there is no where clause adapt budget for type formatting to take space and curly
// brace into account.
match context.config.item_brace_style {
BraceStyle::AlwaysNextLine => {}
BraceStyle::PreferSameLine => used_space += 2,
BraceStyle::SameLineWhere => used_space += 2,
}
}

let budget = try_opt!(context.config.max_width.checked_sub(used_space));
let indent = offset + result.len();
result.push_str(&*try_opt!(self_ty.rewrite(context, budget, indent)));

Some(result)
} else {
unreachable!();
}
}

pub fn format_struct(context: &RewriteContext,
item_name: &str,
ident: ast::Ident,
Expand Down
3 changes: 3 additions & 0 deletions tests/source/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,6 @@ mod m {

impl<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> {
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you change the source of the test to be incorrectly formatted please

4 changes: 4 additions & 0 deletions tests/target/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,7 @@ mod m {
impl<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> {
}