]> git.lizzy.rs Git - rust.git/commitdiff
Use correct width and tactic for struct literal
authortopecongiro <seuchida@gmail.com>
Mon, 3 Jul 2017 09:53:47 +0000 (18:53 +0900)
committertopecongiro <seuchida@gmail.com>
Mon, 3 Jul 2017 09:53:47 +0000 (18:53 +0900)
src/expr.rs
src/lists.rs
src/patterns.rs

index 35c028d24db4efe77986e8eea07b694a4e3a886f..e6b6c53fa2e41fea44ca62fa7364f5351d59b836 100644 (file)
@@ -2601,32 +2601,35 @@ enum StructLitField<'a> {
         span.hi,
     );
     let item_vec = items.collect::<Vec<_>>();
+    let fields_str = wrap_struct_field(context, &fields_str, shape, v_shape, one_line_width);
+    Some(format!("{} {{{}}}", path_str, fields_str))
 
-    let tactic = struct_lit_tactic(h_shape, context, &item_vec);
-    let nested_shape = shape_for_tactic(tactic, h_shape, v_shape);
-    let fmt = struct_lit_formatting(nested_shape, tactic, context, base.is_some());
+    // FIXME if context.config.struct_lit_style() == Visual, but we run out
+    // of space, we should fall back to BlockIndent.
+}
 
-    let fields_str = try_opt!(write_list(&item_vec, &fmt));
-    let fields_str = if context.config.struct_lit_style() == IndentStyle::Block &&
+pub fn wrap_struct_field(
+    context: &RewriteContext,
+    fields_str: &str,
+    shape: Shape,
+    nested_shape: Shape,
+    one_line_width: usize,
+) -> String {
+    if context.config.struct_lit_style() == IndentStyle::Block &&
         (fields_str.contains('\n') ||
              context.config.struct_lit_multiline_style() == MultilineStyle::ForceMulti ||
-             fields_str.len() > h_shape.map(|s| s.width).unwrap_or(0))
+             fields_str.len() > one_line_width)
     {
         format!(
             "\n{}{}\n{}",
-            v_shape.indent.to_string(context.config),
+            nested_shape.indent.to_string(context.config),
             fields_str,
             shape.indent.to_string(context.config)
         )
     } else {
         // One liner or visual indent.
         format!(" {} ", fields_str)
-    };
-
-    Some(format!("{} {{{}}}", path_str, fields_str))
-
-    // FIXME if context.config.struct_lit_style() == Visual, but we run out
-    // of space, we should fall back to BlockIndent.
+    }
 }
 
 pub fn struct_lit_field_separator(config: &Config) -> &str {
index 1d3be8045e23bc679b1aefb46d93e0a653d22644..d6cb00b55cd3c59590fc72415e4252051dc8fbb3 100644 (file)
@@ -555,8 +555,13 @@ pub fn struct_lit_shape(
             }
         }
     };
-    let h_shape = shape.sub_width(prefix_width + suffix_width);
-    Some((h_shape, v_shape))
+    let shape_width = shape.width.checked_sub(prefix_width + suffix_width);
+    if let Some(w) = shape_width {
+        let shape_width = cmp::min(w, context.config.struct_lit_width());
+        Some((Some(Shape::legacy(shape_width, shape.indent)), v_shape))
+    } else {
+        Some((None, v_shape))
+    }
 }
 
 // Compute the tactic for the internals of a struct-lit-like thing.
@@ -566,16 +571,10 @@ pub fn struct_lit_tactic(
     items: &[ListItem],
 ) -> DefinitiveListTactic {
     if let Some(h_shape) = h_shape {
-        let mut prelim_tactic = match (context.config.struct_lit_style(), items.len()) {
+        let prelim_tactic = match (context.config.struct_lit_style(), items.len()) {
             (IndentStyle::Visual, 1) => ListTactic::HorizontalVertical,
             _ => context.config.struct_lit_multiline_style().to_list_tactic(),
         };
-
-        if prelim_tactic == ListTactic::HorizontalVertical && items.len() > 1 {
-            prelim_tactic =
-                ListTactic::LimitedHorizontalVertical(context.config.struct_lit_width());
-        }
-
         definitive_tactic(items, prelim_tactic, h_shape.width)
     } else {
         DefinitiveListTactic::Vertical
index ed444210081069f0e145698e0b393a0c0b6b9e3a..cda7a108609e5201db3c9f285897a763c990556b 100644 (file)
 
 use Shape;
 use codemap::SpanUtils;
-use config::{IndentStyle, MultilineStyle};
 use rewrite::{Rewrite, RewriteContext};
 use utils::{wrap_str, format_mutability, mk_sp};
 use lists::{DefinitiveListTactic, SeparatorTactic, itemize_list, struct_lit_shape,
             struct_lit_tactic, shape_for_tactic, struct_lit_formatting, write_list};
-use expr::{rewrite_call_inner, rewrite_unary_prefix, rewrite_pair, can_be_overflowed_expr};
+use expr::{rewrite_call_inner, rewrite_unary_prefix, rewrite_pair, can_be_overflowed_expr,
+           wrap_struct_field};
 use types::{rewrite_path, PathContext};
 use super::Spanned;
 use comment::FindUncommented;
@@ -181,9 +181,10 @@ fn rewrite_struct_pat(
     let fmt = struct_lit_formatting(nested_shape, tactic, context, false);
 
     let mut fields_str = try_opt!(write_list(&item_vec, &fmt));
+    let one_line_width = h_shape.map_or(0, |shape| shape.width);
 
     if elipses {
-        if fields_str.contains('\n') {
+        if fields_str.contains('\n') || fields_str.len() > one_line_width {
             // Add a missing trailing comma.
             if fmt.trailing_separator == SeparatorTactic::Never {
                 fields_str.push_str(",");
@@ -205,23 +206,7 @@ fn rewrite_struct_pat(
         }
     }
 
-
-    let fields_str = if context.config.struct_lit_style() == IndentStyle::Block &&
-        (fields_str.contains('\n') ||
-             context.config.struct_lit_multiline_style() == MultilineStyle::ForceMulti ||
-             fields_str.len() > h_shape.map(|s| s.width).unwrap_or(0))
-    {
-        format!(
-            "\n{}{}\n{}",
-            v_shape.indent.to_string(context.config),
-            fields_str,
-            shape.indent.to_string(context.config)
-        )
-    } else {
-        // One liner or visual indent.
-        format!(" {} ", fields_str)
-    };
-
+    let fields_str = wrap_struct_field(context, &fields_str, shape, v_shape, one_line_width);
     Some(format!("{} {{{}}}", path_str, fields_str))
 }