]> git.lizzy.rs Git - rust.git/blobdiff - src/overflow.rs
discard trailing blank comments
[rust.git] / src / overflow.rs
index aeb528bf2e0ad376a14f201b0696a4958d960b30..e18fe140c8de8249e82a566b11eeffe47d1d2a72 100644 (file)
 
 use closures;
 use codemap::SpanUtils;
-use expr::{is_every_expr_simple, is_nested_call, maybe_get_args_offset, ToExpr};
+use expr::{is_every_expr_simple, is_method_call, is_nested_call, maybe_get_args_offset, ToExpr};
 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
 use rewrite::{Rewrite, RewriteContext};
 use shape::Shape;
 use spanned::Spanned;
-use utils::{
-    count_newlines, extra_offset, first_line_width, last_line_width, mk_sp, paren_overhead,
-};
+use utils::{count_newlines, extra_offset, first_line_width, last_line_width, mk_sp};
 
 use std::cmp::min;
 
@@ -140,26 +138,16 @@ pub fn new(
         force_separator_tactic: Option<SeparatorTactic>,
         custom_delims: Option<(&'a str, &'a str)>,
     ) -> Context<'a, T> {
-        // 2 = `( `, 1 = `(`
-        let paren_overhead = if context.config.spaces_within_parens_and_brackets() {
-            2
-        } else {
-            1
-        };
         let used_width = extra_offset(ident, shape);
-        let one_line_width = shape.width.saturating_sub(used_width + 2 * paren_overhead);
+        // 1 = `()`
+        let one_line_width = shape.width.saturating_sub(used_width + 2);
 
         // 1 = "(" or ")"
         let one_line_shape = shape
             .offset_left(last_line_width(ident) + 1)
             .and_then(|shape| shape.sub_width(1))
             .unwrap_or(Shape { width: 0, ..shape });
-        let nested_shape = shape_from_indent_style(
-            context,
-            shape,
-            used_width + 2 * paren_overhead,
-            used_width + paren_overhead,
-        );
+        let nested_shape = shape_from_indent_style(context, shape, used_width + 2, used_width + 1);
         Context {
             context,
             items,
@@ -243,8 +231,8 @@ fn try_overflow_last_item(&self, list_items: &mut Vec<ListItem>) -> DefinitiveLi
         let placeholder = if overflow_last {
             let old_value = *self.context.force_one_line_chain.borrow();
             if !combine_arg_with_callee {
-                if let Some(expr) = self.last_item().and_then(|item| item.to_expr()) {
-                    if let ast::ExprKind::MethodCall(..) = expr.node {
+                if let Some(ref expr) = self.last_item().and_then(|item| item.to_expr()) {
+                    if is_method_call(expr) {
                         self.context.force_one_line_chain.replace(true);
                     }
                 }
@@ -332,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);
@@ -377,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))
@@ -417,12 +399,13 @@ fn wrap_items(&self, items_str: &str, shape: Shape, is_extendable: bool) -> Stri
             Some((lhs, rhs)) => (lhs, rhs),
             _ => (self.prefix, self.suffix),
         };
-        let paren_overhead = paren_overhead(self.context);
-        let fits_one_line = items_str.len() + paren_overhead <= shape.width;
+
+        // 2 = `()`
+        let fits_one_line = items_str.len() + 2 <= shape.width;
         let extend_width = if items_str.is_empty() {
-            paren_overhead
+            2
         } else {
-            first_line_width(items_str) + (paren_overhead / 2)
+            first_line_width(items_str) + 1
         };
         let nested_indent_str = self
             .nested_shape
@@ -441,13 +424,7 @@ fn wrap_items(&self, items_str: &str, shape: Shape, is_extendable: bool) -> Stri
             || (self.context.inside_macro() && !items_str.contains('\n') && fits_one_line)
             || (is_extendable && extend_width <= shape.width)
         {
-            if self.context.config.spaces_within_parens_and_brackets() && !items_str.is_empty() {
-                result.push(' ');
-                result.push_str(items_str);
-                result.push(' ');
-            } else {
-                result.push_str(items_str);
-            }
+            result.push_str(items_str);
         } else {
             if !items_str.is_empty() {
                 result.push_str(&nested_indent_str);
@@ -527,25 +504,22 @@ 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
     }
 }
 
 fn no_long_items(list: &[ListItem]) -> bool {
     list.iter()
-        .all(|item| !item.has_comment() && item.inner_as_ref().len() <= SHORT_ITEM_THRESHOLD)
+        .all(|item| item.inner_as_ref().len() <= SHORT_ITEM_THRESHOLD)
 }