From fdd7baeae92ead4dfae0a367df367bf074e6cfc4 Mon Sep 17 00:00:00 2001 From: Tomasz Dudziak Date: Mon, 25 May 2015 16:02:38 +0200 Subject: [PATCH] Optionally put the opening paren on the previous line for args --- src/config.rs | 1 + src/default.toml | 1 + src/items.rs | 25 ++++++++++++++++++------- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/config.rs b/src/config.rs index 8e6c376209f..5c861e83908 100644 --- a/src/config.rs +++ b/src/config.rs @@ -19,6 +19,7 @@ pub struct Config { pub newline_style: ::NewlineStyle, pub fn_brace_style: ::BraceStyle, pub fn_return_indent: ::ReturnIndent, + pub fn_args_paren_newline: bool, pub struct_trailing_comma: bool, pub struct_lit_trailing_comma: ::lists::SeparatorTactic, } diff --git a/src/default.toml b/src/default.toml index e8c74879b59..1a84f476db8 100644 --- a/src/default.toml +++ b/src/default.toml @@ -5,5 +5,6 @@ tab_spaces = 4 newline_style = "Unix" fn_brace_style = "SameLineWhere" fn_return_indent = "WithArgs" +fn_args_paren_newline = true struct_trailing_comma = true struct_lit_trailing_comma = "Vertical" diff --git a/src/items.rs b/src/items.rs index 55613ab1bb8..eeb05bc9201 100644 --- a/src/items.rs +++ b/src/items.rs @@ -130,13 +130,26 @@ impl<'a> FmtVisitor<'a> { let ret_str = self.rewrite_return(&fd.output); // Args. - let (one_line_budget, multi_line_budget, arg_indent) = - self.compute_budgets_for_args(&mut result, indent, ret_str.len(), newline_brace); + let (one_line_budget, multi_line_budget, mut arg_indent) = + self.compute_budgets_for_args(&result, indent, ret_str.len(), newline_brace); debug!("rewrite_fn: one_line_budget: {}, multi_line_budget: {}, arg_indent: {}", one_line_budget, multi_line_budget, arg_indent); - result.push('('); + if one_line_budget <= 0 { + if config!(fn_args_paren_newline) { + result.push('\n'); + result.push_str(&make_indent(arg_indent)); + arg_indent = arg_indent + 1; + result.push('('); + } else { + result.push_str("(\n"); + result.push_str(&make_indent(arg_indent)); + } + } else { + result.push('('); + } + result.push_str(&self.rewrite_args(&fd.inputs, explicit_self, one_line_budget, @@ -330,7 +343,7 @@ impl<'a> FmtVisitor<'a> { } fn compute_budgets_for_args(&self, - result: &mut String, + result: &String, indent: usize, ret_str_len: usize, newline_brace: bool) @@ -365,8 +378,6 @@ impl<'a> FmtVisitor<'a> { // Didn't work. we must force vertical layout and put args on a newline. if let None = budgets { - result.push('\n'); - result.push_str(&make_indent(indent + 4)); // 6 = new indent + `()` let used_space = indent + 6; let max_space = config!(ideal_width) + config!(leeway); @@ -375,7 +386,7 @@ impl<'a> FmtVisitor<'a> { // TODO take evasive action, perhaps kill the indent or something. } else { // 5 = new indent + `(` - budgets = Some((0, max_space - used_space, indent + 5)); + budgets = Some((0, max_space - used_space, indent + 4)); } }