Skip to content

Commit 9049413

Browse files
committed
Merge pull request #81 from tdudziak/issue_31
Optionally put the opening paren on the previous line for args (Issue #31)
2 parents ecd9557 + 1bb3c9e commit 9049413

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct Config {
1919
pub newline_style: ::NewlineStyle,
2020
pub fn_brace_style: ::BraceStyle,
2121
pub fn_return_indent: ::ReturnIndent,
22+
pub fn_args_paren_newline: bool,
2223
pub struct_trailing_comma: bool,
2324
pub struct_lit_trailing_comma: ::lists::SeparatorTactic,
2425
}

src/default.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ tab_spaces = 4
55
newline_style = "Unix"
66
fn_brace_style = "SameLineWhere"
77
fn_return_indent = "WithArgs"
8+
fn_args_paren_newline = true
89
struct_trailing_comma = true
910
struct_lit_trailing_comma = "Vertical"

src/items.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,27 @@ impl<'a> FmtVisitor<'a> {
137137
let ret_str = self.rewrite_return(&fd.output);
138138

139139
// Args.
140-
let (one_line_budget, multi_line_budget, arg_indent) =
141-
self.compute_budgets_for_args(&mut result, indent, ret_str.len(), newline_brace);
140+
let (one_line_budget, multi_line_budget, mut arg_indent) =
141+
self.compute_budgets_for_args(&result, indent, ret_str.len(), newline_brace);
142142

143143
debug!("rewrite_fn: one_line_budget: {}, multi_line_budget: {}, arg_indent: {}",
144144
one_line_budget, multi_line_budget, arg_indent);
145145

146-
result.push('(');
146+
// Check if vertical layout was forced by compute_budget_for_args.
147+
if one_line_budget <= 0 {
148+
if config!(fn_args_paren_newline) {
149+
result.push('\n');
150+
result.push_str(&make_indent(arg_indent));
151+
arg_indent = arg_indent + 1; // extra space for `(`
152+
result.push('(');
153+
} else {
154+
result.push_str("(\n");
155+
result.push_str(&make_indent(arg_indent));
156+
}
157+
} else {
158+
result.push('(');
159+
}
160+
147161
result.push_str(&self.rewrite_args(&fd.inputs,
148162
explicit_self,
149163
one_line_budget,
@@ -337,7 +351,7 @@ impl<'a> FmtVisitor<'a> {
337351
}
338352

339353
fn compute_budgets_for_args(&self,
340-
result: &mut String,
354+
result: &String,
341355
indent: usize,
342356
ret_str_len: usize,
343357
newline_brace: bool)
@@ -372,17 +386,14 @@ impl<'a> FmtVisitor<'a> {
372386

373387
// Didn't work. we must force vertical layout and put args on a newline.
374388
if let None = budgets {
375-
result.push('\n');
376-
result.push_str(&make_indent(indent + 4));
377-
// 6 = new indent + `()`
378-
let used_space = indent + 6;
389+
let new_indent = indent + config!(tab_spaces);
390+
let used_space = new_indent + 2; // account for `(` and `)`
379391
let max_space = config!(ideal_width) + config!(leeway);
380392
if used_space > max_space {
381393
// Whoops! bankrupt.
382394
// TODO take evasive action, perhaps kill the indent or something.
383395
} else {
384-
// 5 = new indent + `(`
385-
budgets = Some((0, max_space - used_space, indent + 5));
396+
budgets = Some((0, max_space - used_space, new_indent));
386397
}
387398
}
388399

0 commit comments

Comments
 (0)