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 {
}
}
};
- 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.
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
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;
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(",");
}
}
-
- 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))
}