Skip to content

Commit

Permalink
Fix overlong function signature (#1089)
Browse files Browse the repository at this point in the history
* Fix issue-1049

* Add testcase suggested by pepyakin

* Fix last commit

* Handle special case

* Remove debugging println

* Fix grammar in comment

* Change word in comment

* Add test for long func without ret type

* Add one more test
  • Loading branch information
dawirstejeck authored and nrc committed Aug 1, 2016
1 parent 6380937 commit 22de7ce
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/items.rs
Expand Up @@ -1369,10 +1369,21 @@ fn rewrite_fn_base(context: &RewriteContext,
FnArgLayoutStyle::Block if put_args_in_block => false,
FnArgLayoutStyle::BlockAlways => false,
_ => {
// If we've already gone multi-line, or the return type would push
// over the max width, then put the return type on a new line.
result.contains("\n") || multi_line_ret_str ||
result.len() + indent.width() + ret_str_len > context.config.max_width
// If we've already gone multi-line, or the return type would push over the max
// width, then put the return type on a new line. With the +1 for the signature
// length an additional space between the closing parenthesis of the argument and
// the arrow '->' is considered.
let mut sig_length = result.len() + indent.width() + ret_str_len + 1;

// If there is no where clause, take into account the space after the return type
// and the brace.
if where_clause.predicates.is_empty() {
sig_length += 2;
}

let overlong_sig = sig_length > context.config.max_width;

result.contains("\n") || multi_line_ret_str || overlong_sig
}
};
let ret_indent = if ret_should_indent {
Expand Down Expand Up @@ -1400,10 +1411,8 @@ fn rewrite_fn_base(context: &RewriteContext,
if multi_line_ret_str {
// Now that we know the proper indent and width, we need to
// re-layout the return type.

let budget = try_opt!(context.config.max_width.checked_sub(ret_indent.width()));
let ret_str = try_opt!(fd.output
.rewrite(context, budget, ret_indent));
let ret_str = try_opt!(fd.output.rewrite(context, budget, ret_indent));
result.push_str(&ret_str);
} else {
result.push_str(&ret_str);
Expand Down
16 changes: 16 additions & 0 deletions tests/source/issue-1049.rs
@@ -0,0 +1,16 @@
// Test overlong function signature
pub unsafe fn reborrow_mut(&mut X: Abcde) -> Handle<NodeRef<marker::Mut, K, V, NodeType>, HandleType> {
}

pub fn merge(mut X: Abcdef) -> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::Edge> {
}

impl Handle {
pub fn merge(a: Abcd) -> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::Edge> {
}
}

// Long function without return type that should not be reformated.
fn veeeeeeeeeeeeeeeeeeeeery_long_name(a: FirstTypeeeeeeeeee, b: SecondTypeeeeeeeeeeeeeeeeeeeeeee) {}

fn veeeeeeeeeeeeeeeeeeeeeery_long_name(a: FirstTypeeeeeeeeee, b: SecondTypeeeeeeeeeeeeeeeeeeeeeee) {}
21 changes: 21 additions & 0 deletions tests/target/issue-1049.rs
@@ -0,0 +1,21 @@
// Test overlong function signature
pub unsafe fn reborrow_mut(&mut X: Abcde)
-> Handle<NodeRef<marker::Mut, K, V, NodeType>, HandleType> {
}

pub fn merge(mut X: Abcdef)
-> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::Edge> {
}

impl Handle {
pub fn merge(a: Abcd)
-> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::Edge> {
}
}

// Long function without return type that should not be reformated.
fn veeeeeeeeeeeeeeeeeeeeery_long_name(a: FirstTypeeeeeeeeee, b: SecondTypeeeeeeeeeeeeeeeeeeeeeee) {}

fn veeeeeeeeeeeeeeeeeeeeeery_long_name(a: FirstTypeeeeeeeeee,
b: SecondTypeeeeeeeeeeeeeeeeeeeeeee) {
}

0 comments on commit 22de7ce

Please sign in to comment.