Merge pull request #1627 from topecongiro/issue-1624
Refactor vertical layout of function definition with options
This commit is contained in:
commit
3090086000
55
src/items.rs
55
src/items.rs
@ -1587,7 +1587,10 @@ fn rewrite_fn_base(context: &RewriteContext,
|
||||
let generics_str = try_opt!(rewrite_generics(context, generics, shape, generics_span));
|
||||
result.push_str(&generics_str);
|
||||
|
||||
let snuggle_angle_bracket = last_line_width(&generics_str) == 1;
|
||||
let snuggle_angle_bracket = generics_str
|
||||
.lines()
|
||||
.last()
|
||||
.map_or(false, |l| l.trim_left().len() == 1);
|
||||
|
||||
// Note that the width and indent don't really matter, we'll re-layout the
|
||||
// return type later anyway.
|
||||
@ -1619,24 +1622,28 @@ fn rewrite_fn_base(context: &RewriteContext,
|
||||
// Check if vertical layout was forced.
|
||||
if one_line_budget == 0 {
|
||||
if snuggle_angle_bracket {
|
||||
result.push_str("(");
|
||||
} else if context.config.fn_args_paren_newline() {
|
||||
result.push('\n');
|
||||
result.push_str(&arg_indent.to_string(context.config));
|
||||
arg_indent = arg_indent + 1; // extra space for `(`
|
||||
result.push('(');
|
||||
if context.config.spaces_within_parens() && fd.inputs.len() > 0 {
|
||||
result.push(' ')
|
||||
}
|
||||
} else {
|
||||
result.push_str("(\n");
|
||||
result.push_str(&arg_indent.to_string(context.config));
|
||||
if context.config.fn_args_paren_newline() {
|
||||
result.push('\n');
|
||||
result.push_str(&arg_indent.to_string(context.config));
|
||||
if context.config.fn_args_layout() == IndentStyle::Visual {
|
||||
arg_indent = arg_indent + 1; // extra space for `(`
|
||||
}
|
||||
result.push('(');
|
||||
} else {
|
||||
result.push_str("(");
|
||||
if context.config.fn_args_layout() == IndentStyle::Visual {
|
||||
result.push('\n');
|
||||
result.push_str(&arg_indent.to_string(context.config));
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result.push('(');
|
||||
if context.config.spaces_within_parens() && fd.inputs.len() > 0 {
|
||||
result.push(' ')
|
||||
}
|
||||
}
|
||||
if context.config.spaces_within_parens() && fd.inputs.len() > 0 && result.ends_with('(') {
|
||||
result.push(' ')
|
||||
}
|
||||
|
||||
if multi_line_ret_str {
|
||||
@ -1999,10 +2006,10 @@ fn compute_budgets_for_args(context: &RewriteContext,
|
||||
|
||||
if one_line_budget > 0 {
|
||||
// 4 = "() {".len()
|
||||
let multi_line_budget = try_opt!(context
|
||||
.config
|
||||
.max_width()
|
||||
.checked_sub(indent.width() + result.len() + 4));
|
||||
let multi_line_overhead = indent.width() + result.len() +
|
||||
if newline_brace { 2 } else { 4 };
|
||||
let multi_line_budget =
|
||||
try_opt!(context.config.max_width().checked_sub(multi_line_overhead));
|
||||
|
||||
return Some((one_line_budget, multi_line_budget, indent + result.len() + 1));
|
||||
}
|
||||
@ -2010,14 +2017,10 @@ fn compute_budgets_for_args(context: &RewriteContext,
|
||||
|
||||
// Didn't work. we must force vertical layout and put args on a newline.
|
||||
let new_indent = indent.block_indent(context.config);
|
||||
let used_space = new_indent.width() + 4; // Account for `(` and `)` and possibly ` {`.
|
||||
let max_space = context.config.max_width();
|
||||
if used_space <= max_space {
|
||||
Some((0, max_space - used_space, new_indent))
|
||||
} else {
|
||||
// Whoops! bankrupt.
|
||||
None
|
||||
}
|
||||
// Account for `)` and possibly ` {`.
|
||||
let used_space = new_indent.width() + if ret_str_len == 0 { 1 } else { 3 };
|
||||
let max_space = try_opt!(context.config.max_width().checked_sub(used_space));
|
||||
Some((0, max_space, new_indent))
|
||||
}
|
||||
|
||||
fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause) -> bool {
|
||||
|
@ -52,8 +52,7 @@ impl Foo {
|
||||
'a: 'bbbbbbbbbbbbbbbbbbbbbbbbbbb,
|
||||
TTTTTTTTTTTTT,
|
||||
UUUUUUUUUUUUUUUUUUUU: WWWWWWWWWWWWWWWWWWWWWWWW
|
||||
>
|
||||
(
|
||||
>(
|
||||
a: Aaaaaaaaaaaaaaa,
|
||||
) {
|
||||
bar();
|
||||
|
@ -54,8 +54,7 @@ impl Foo {
|
||||
'a: 'bbbbbbbbbbbbbbbbbbbbbbbbbbb,
|
||||
TTTTTTTTTTTTT,
|
||||
UUUUUUUUUUUUUUUUUUUU: WWWWWWWWWWWWWWWWWWWWWWWW
|
||||
>
|
||||
(
|
||||
>(
|
||||
a: Aaaaaaaaaaaaaaa,
|
||||
) {
|
||||
bar();
|
||||
|
9
tests/target/issue-1624.rs
Normal file
9
tests/target/issue-1624.rs
Normal file
@ -0,0 +1,9 @@
|
||||
// rustfmt-fn_args_layout: Block
|
||||
// rustfmt-fn_args_paren_newline: false
|
||||
|
||||
// #1624
|
||||
pub unsafe fn some_long_function_name(
|
||||
arg1: Type1,
|
||||
arg2: Type2,
|
||||
) -> (SomeLongTypeName, AnotherLongTypeName, AnotherLongTypeName) {
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user