]> git.lizzy.rs Git - rust.git/commitdiff
Merge pull request #81 from tdudziak/issue_31
authorNick Cameron <nrc@ncameron.org>
Thu, 28 May 2015 23:59:37 +0000 (11:59 +1200)
committerNick Cameron <nrc@ncameron.org>
Thu, 28 May 2015 23:59:37 +0000 (11:59 +1200)
Optionally put the opening paren on the previous line for args (Issue #31)

src/config.rs
src/default.toml
src/items.rs

index 8e6c376209f338f7702d5eb2f29600ae2c8cf2ef..5c861e839080f733325cc2918d9b42e77058aae6 100644 (file)
@@ -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,
 }
index e8c74879b597df5a017be5d3c040a4ad5a556b03..1a84f476db89f2876261a2e45760a0e5ae9178da 100644 (file)
@@ -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"
index 3b174190643f96b3e0af42585500b50595cc07ab..d51ddd4c7a59521fea2a02b26b53d9c7f1a3489d 100644 (file)
@@ -137,13 +137,27 @@ fn rewrite_fn_base(&mut self,
         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('(');
+        // Check if vertical layout was forced by compute_budget_for_args.
+        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; // extra space for `(`
+                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,
@@ -337,7 +351,7 @@ fn make_comments_for_list<T, I, F1, F2>(&self,
     }
 
     fn compute_budgets_for_args(&self,
-                                result: &mut String,
+                                result: &String,
                                 indent: usize,
                                 ret_str_len: usize,
                                 newline_brace: bool)
@@ -372,17 +386,14 @@ fn compute_budgets_for_args(&self,
 
         // 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 new_indent = indent + config!(tab_spaces);
+            let used_space = new_indent + 2; // account for `(` and `)`
             let max_space = config!(ideal_width) + config!(leeway);
             if used_space > max_space {
                 // Whoops! bankrupt.
                 // 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, new_indent));
             }
         }