]> git.lizzy.rs Git - rust.git/blobdiff - src/overflow.rs
discard trailing blank comments
[rust.git] / src / overflow.rs
index 62675b4002c45ff2e222ab33c788cf81159ea615..e18fe140c8de8249e82a566b11eeffe47d1d2a72 100644 (file)
@@ -320,15 +320,13 @@ fn try_overflow_last_item(&self, list_items: &mut Vec<ListItem>) -> DefinitiveLi
                                     ListTactic::HorizontalVertical,
                                     Separator::Comma,
                                     self.nested_shape.width,
-                                )
-                                    == DefinitiveListTactic::Horizontal
+                                ) == DefinitiveListTactic::Horizontal
                                 && definitive_tactic(
                                     &list_items[num_args_before + 1..],
                                     ListTactic::HorizontalVertical,
                                     Separator::Comma,
                                     self.nested_shape.width,
-                                )
-                                    == DefinitiveListTactic::Horizontal;
+                                ) == DefinitiveListTactic::Horizontal;
 
                             if one_line {
                                 tactic = DefinitiveListTactic::SpecialMacro(num_args_before);
@@ -365,31 +363,27 @@ fn rewrite_items(&self) -> Option<(bool, String)> {
         // indentation. If its first line fits on one line with the other arguments,
         // we format the function arguments horizontally.
         let tactic = self.try_overflow_last_item(&mut list_items);
-
-        let fmt = ListFormatting {
-            tactic,
-            separator: ",",
-            trailing_separator: if let Some(tactic) = self.force_separator_tactic {
-                tactic
-            } else if !self.context.use_block_indent() {
-                SeparatorTactic::Never
-            } else if tactic == DefinitiveListTactic::Mixed {
-                // We are using mixed layout because everything did not fit within a single line.
-                SeparatorTactic::Always
-            } else {
-                self.context.config.trailing_comma()
-            },
-            separator_place: SeparatorPlace::Back,
-            shape: self.nested_shape,
-            ends_with_newline: match tactic {
-                DefinitiveListTactic::Vertical | DefinitiveListTactic::Mixed => {
-                    self.context.use_block_indent()
-                }
-                _ => false,
-            },
-            preserve_newline: false,
-            config: self.context.config,
+        let trailing_separator = if let Some(tactic) = self.force_separator_tactic {
+            tactic
+        } else if !self.context.use_block_indent() {
+            SeparatorTactic::Never
+        } else if tactic == DefinitiveListTactic::Mixed {
+            // We are using mixed layout because everything did not fit within a single line.
+            SeparatorTactic::Always
+        } else {
+            self.context.config.trailing_comma()
         };
+        let ends_with_newline = match tactic {
+            DefinitiveListTactic::Vertical | DefinitiveListTactic::Mixed => {
+                self.context.use_block_indent()
+            }
+            _ => false,
+        };
+
+        let fmt = ListFormatting::new(self.nested_shape, self.context.config)
+            .tactic(tactic)
+            .trailing_separator(trailing_separator)
+            .ends_with_newline(ends_with_newline);
 
         write_list(&list_items, &fmt)
             .map(|items_str| (tactic == DefinitiveListTactic::Horizontal, items_str))
@@ -510,21 +504,18 @@ fn shape_from_indent_style(
     overhead: usize,
     offset: usize,
 ) -> Shape {
-    if context.use_block_indent() {
-        // 1 = ","
-        shape
+    let (shape, overhead) = if context.use_block_indent() {
+        let shape = shape
             .block()
             .block_indent(context.config.tab_spaces())
-            .with_max_width(context.config)
-            .sub_width(1)
-            .unwrap()
+            .with_max_width(context.config);
+        (shape, 1) // 1 = ","
     } else {
-        let shape = shape.visual_indent(offset);
-        if let Some(shape) = shape.sub_width(overhead) {
-            shape
-        } else {
-            Shape { width: 0, ..shape }
-        }
+        (shape.visual_indent(offset), overhead)
+    };
+    Shape {
+        width: shape.width.saturating_sub(overhead),
+        ..shape
     }
 }