Skip to content

Fix overlong impl #1091

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 4 commits into from
Jul 26, 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
14 changes: 13 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
ref self_ty,
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");
Expand Down Expand Up @@ -470,7 +471,18 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
result.push_str(" for ");
}

let budget = try_opt!(context.config.max_width.checked_sub(result.len()));
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,
}
}

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)));

Expand Down
3 changes: 3 additions & 0 deletions tests/source/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@ mod m {

impl<T> PartialEq for S<T> where T: PartialEq { }
}

impl<BorrowType, K, V, NodeType, HandleType> Handle<NodeRef<BorrowType, K, V, NodeType>, HandleType> {
}
4 changes: 4 additions & 0 deletions tests/target/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,7 @@ mod m {

impl<T> PartialEq for S<T> where T: PartialEq {}
}

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