]> git.lizzy.rs Git - rust.git/commitdiff
Add trailing comma when using Mixed indent style with newline
authortopecongiro <seuchida@gmail.com>
Thu, 13 Jul 2017 11:32:46 +0000 (20:32 +0900)
committertopecongiro <seuchida@gmail.com>
Thu, 13 Jul 2017 11:32:46 +0000 (20:32 +0900)
src/expr.rs
src/items.rs
src/lists.rs
src/types.rs
tests/target/configs-fn_args_density-compressed.rs
tests/target/fn-custom.rs
tests/target/fn-simple.rs
tests/target/hard-tabs.rs
tests/target/type.rs

index 88f56cfa25c08f5c920895615ca4ab7c8f8c39e3..09220e9ac80c587d5e6f8b2a4774f36221da8d1c 100644 (file)
@@ -501,10 +501,12 @@ pub fn rewrite_array<'a, I>(
             DefinitiveListTactic::Mixed
         },
     };
+    let mut ends_with_newline = tactic.ends_with_newline(context.config.array_layout());
     if context.config.array_horizontal_layout_threshold() > 0 &&
         items.len() > context.config.array_horizontal_layout_threshold()
     {
         tactic = DefinitiveListTactic::Mixed;
+        ends_with_newline = false;
         if context.config.array_layout() == IndentStyle::Block {
             nested_shape = try_opt!(
                 shape
@@ -525,7 +527,7 @@ pub fn rewrite_array<'a, I>(
             SeparatorTactic::Vertical
         },
         shape: nested_shape,
-        ends_with_newline: false,
+        ends_with_newline: ends_with_newline,
         config: context.config,
     };
     let list_str = try_opt!(write_list(&items, &fmt));
index eb15e95fabb382bd508aa9bb8b9b8e46cb812e29..aa83d9a2b0dcfcd4bc1eda7cd8ba40136e832bce 100644 (file)
@@ -2204,21 +2204,18 @@ enum ArgumentKind<'a> {
         .and_then(|item| item.post_comment.as_ref())
         .map_or(false, |s| s.trim().starts_with("//"));
 
-    let (indent, trailing_comma, end_with_newline) = match context.config.fn_args_layout() {
-        IndentStyle::Block if fits_in_one_line => (
-            indent.block_indent(context.config),
-            SeparatorTactic::Never,
-            true,
-        ),
+    let (indent, trailing_comma) = match context.config.fn_args_layout() {
+        IndentStyle::Block if fits_in_one_line => {
+            (indent.block_indent(context.config), SeparatorTactic::Never)
+        }
         IndentStyle::Block => (
             indent.block_indent(context.config),
             context.config.trailing_comma(),
-            true,
         ),
         IndentStyle::Visual if last_line_ends_with_comment => {
-            (arg_indent, context.config.trailing_comma(), true)
+            (arg_indent, context.config.trailing_comma())
         }
-        IndentStyle::Visual => (arg_indent, SeparatorTactic::Never, false),
+        IndentStyle::Visual => (arg_indent, SeparatorTactic::Never),
     };
 
     let tactic = definitive_tactic(
@@ -2242,7 +2239,7 @@ enum ArgumentKind<'a> {
             trailing_comma
         },
         shape: Shape::legacy(budget, indent),
-        ends_with_newline: end_with_newline,
+        ends_with_newline: tactic.ends_with_newline(context.config.fn_args_layout()),
         config: context.config,
     };
 
@@ -2406,8 +2403,6 @@ pub fn format_generics_item_list<I>(
     let item_vec = items.collect::<Vec<_>>();
 
     let tactic = definitive_tactic(&item_vec, ListTactic::HorizontalVertical, one_line_budget);
-    let ends_with_newline = context.config.generics_indent() == IndentStyle::Block &&
-        tactic == DefinitiveListTactic::Vertical;
     let fmt = ListFormatting {
         tactic: tactic,
         separator: ",",
@@ -2417,7 +2412,7 @@ pub fn format_generics_item_list<I>(
             context.config.trailing_comma()
         },
         shape: shape,
-        ends_with_newline: ends_with_newline,
+        ends_with_newline: tactic.ends_with_newline(context.config.generics_indent()),
         config: context.config,
     };
 
@@ -2631,7 +2626,7 @@ fn rewrite_where_clause(
         separator: ",",
         trailing_separator: comma_tactic,
         shape: Shape::legacy(budget, offset),
-        ends_with_newline: true,
+        ends_with_newline: tactic.ends_with_newline(context.config.where_pred_indent()),
         config: context.config,
     };
     let preds_str = try_opt!(write_list(&item_vec, &fmt));
index e55103d0df81b177580ca89ea8650ea3102bbed1..55db7cd9c0008c2fee11acefe56430812c68e654 100644 (file)
@@ -124,6 +124,15 @@ pub enum DefinitiveListTactic {
     Mixed,
 }
 
+impl DefinitiveListTactic {
+    pub fn ends_with_newline(&self, indent_style: IndentStyle) -> bool {
+        match indent_style {
+            IndentStyle::Block => *self != DefinitiveListTactic::Horizontal,
+            IndentStyle::Visual => false,
+        }
+    }
+}
+
 pub fn definitive_tactic<I, T>(items: I, tactic: ListTactic, width: usize) -> DefinitiveListTactic
 where
     I: IntoIterator<Item = T> + Clone,
@@ -169,7 +178,7 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
 
     // Now that we know how we will layout, we can decide for sure if there
     // will be a trailing separator.
-    let trailing_separator = needs_trailing_separator(formatting.trailing_separator, tactic);
+    let mut trailing_separator = needs_trailing_separator(formatting.trailing_separator, tactic);
     let mut result = String::new();
     let cloned_items = items.clone();
     let mut iter = items.into_iter().enumerate().peekable();
@@ -182,7 +191,7 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
         let inner_item = try_opt!(item.item.as_ref());
         let first = i == 0;
         let last = iter.peek().is_none();
-        let separate = !last || trailing_separator;
+        let mut separate = !last || trailing_separator;
         let item_sep_len = if separate { sep_len } else { 0 };
 
         // Item string may be multi-line. Its length (used for block comment alignment)
@@ -213,6 +222,13 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
                     result.push('\n');
                     result.push_str(indent_str);
                     line_len = 0;
+                    if tactic == DefinitiveListTactic::Mixed && formatting.ends_with_newline {
+                        if last {
+                            separate = true;
+                        } else {
+                            trailing_separator = true;
+                        }
+                    }
                 }
 
                 if line_len > 0 {
index 877502f153c4e4883594e0d11d80cc72c8d754fa..c34a3bfaa6a820116fa703b6971ae1c2af5d05fa 100644 (file)
@@ -360,7 +360,7 @@ enum ArgumentKind<T>
             context.config.trailing_comma()
         },
         shape: list_shape,
-        ends_with_newline: false,
+        ends_with_newline: tactic.ends_with_newline(context.config.fn_call_style()),
         config: context.config,
     };
 
index 7bc949e55180b72a7088a212bdbee7dd1a77aacf..99283722beea1e331e37540693024ab8d9ad1c2a 100644 (file)
@@ -10,12 +10,12 @@ fn lorem(ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet) {
 
     fn lorem(
         ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: onsectetur,
-        adipiscing: Adipiscing, elit: Elit
+        adipiscing: Adipiscing, elit: Elit,
     );
 
     fn lorem(
         ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: onsectetur,
-        adipiscing: Adipiscing, elit: Elit
+        adipiscing: Adipiscing, elit: Elit,
     ) {
         // body
     }
index 7a2ea722bfd599ae02d3dd1b1facd71a7025e68d..bf87553dedcf3902487923b8278976b788b75c78 100644 (file)
@@ -4,7 +4,7 @@
 // Test compressed layout of args.
 fn foo(
     a: Aaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbb, c: Ccccccccccccccccc, d: Ddddddddddddddddddddddddd,
-    e: Eeeeeeeeeeeeeeeeeee
+    e: Eeeeeeeeeeeeeeeeeee,
 ) {
     foo();
 }
@@ -12,7 +12,7 @@ fn foo(
 impl Foo {
     fn foo(
         self, a: Aaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbb, c: Ccccccccccccccccc,
-        d: Ddddddddddddddddddddddddd, e: Eeeeeeeeeeeeeeeeeee
+        d: Ddddddddddddddddddddddddd, e: Eeeeeeeeeeeeeeeeeee,
     ) {
         foo();
     }
index e150973a67d03fbfcb5d279278a28ddcf5a4ec83..e381b6e62314d49a46303ce03d683efc801e03ee 100644 (file)
@@ -42,7 +42,7 @@ fn generic<T>(arg: T) -> &SomeType
         C,
         D,
         // pre comment
-        E, /* last comment */
+        E, // last comment
     ) -> &SomeType,
 {
     arg(a, b, c, d, e)
index c2f8d2522370f19faa86ee1b9a26c44906125543..4ac6df5def85dee9455643d9c575ddc345d0d269 100644 (file)
@@ -67,7 +67,7 @@ fn generic<T>(arg: T) -> &SomeType
                        C,
                        D,
                        // pre comment
-                       E, /* last comment */
+                       E, // last comment
                ) -> &SomeType,
        {
                arg(a, b, c, d, e)
index 80855ee5732a5bce778ca7e265a348349528773c..5a12989c857c668fce6abdb513a9cae15bc03d12 100644 (file)
@@ -25,7 +25,7 @@ struct F {
         y: String, // comment 3
         z: Foo,
         // comment
-        ... /* comment 2 */
+        ... // comment 2
     ),
 }